PDA

Click to See Complete Forum and Search --> : What is wrong with this code?


krah
Dec 16th, 2000, 07:14 PM
Yes, I know, it is a lame subject title. I really need to know what I'm doing wrong here, though. All I want is the angle to be positive (or negative but not a mix of both) between 0 and 2pi, 6.283.

To help you narrow it down, I believe the problem is in the Form_Click section after it says "finding the angle-"

Download the project in zip form, unzip it and open it up. Download it from http://www.flash.net/~krah1/angle.zip

Option Explicit
Dim PivotPointX
Dim PivotPointY
Dim Point1X
Dim Point1Y
Dim Point2X
Dim Point2Y
Dim ConstantAnglePointX
Dim ConstantAnglePointY
Dim NewAnglePointX
Dim NewAnglePointY
Dim ConstantAngle
Dim NewAngle
Dim FullAngle
Dim ThingMoving
Dim MouseX
Dim MouseY
Private Sub Combo1_Click()
If Combo1.ListIndex = 0 Then ThingMoving = "First"
If Combo1.ListIndex = 1 Then ThingMoving = "Second"
If Combo1.ListIndex = 2 Then ThingMoving = "Pivot"
End Sub


Private Sub Form_Click()
'figuring out which one gets moved-
If ThingMoving = "First" Then FirstPoint.Left = MouseX: FirstPoint.Top = MouseY
If ThingMoving = "Second" Then SecondPoint.Left = MouseX: SecondPoint.Top = MouseY
If ThingMoving = "Pivot" Then PivotPoint.Left = MouseX: PivotPoint.Top = MouseY

'making the points where the shapes are-
Point1X = FirstPoint.Left
Point1Y = FirstPoint.Top
Point2X = SecondPoint.Left
Point2Y = SecondPoint.Top
PivotPointX = PivotPoint.Left
PivotPointY = PivotPoint.Top

'making the lines to connect the shapes-
Line1.X1 = Point1X + FirstPoint.Width / 2
Line1.Y1 = Point1Y + FirstPoint.Height / 2
Line1.X2 = PivotPointX + PivotPoint.Width / 2
Line1.Y2 = PivotPointY + PivotPoint.Height / 2
Line2.X1 = PivotPointX + PivotPoint.Width / 2
Line2.Y1 = PivotPointY + PivotPoint.Height / 2
Line2.X2 = Point2X + SecondPoint.Width / 2
Line2.Y2 = Point2Y + SecondPoint.Height / 2

'moving the labels-
FirstLabel.Left = Point1X - FirstLabel.Width / 2
FirstLabel.Top = Point1Y - 20
SecondLabel.Left = Point2X - SecondLabel.Width / 2
SecondLabel.Top = Point2Y - 20
PivotLabel.Left = PivotPointX - PivotLabel.Width / 2
PivotLabel.Top = PivotPointY - 20

'displaying the coordinates on the labels-
FirstLabel.Caption = "First Point (" & Point1X & ", " & Point1Y & ")"
SecondLabel.Caption = "Second Point (" & Point2X & ", " & Point2Y & ")"
PivotLabel.Caption = "Pivot Point (" & PivotPointX & ", " & PivotPointY & ")"

'finding the angle-
ConstantAnglePointX = Point1X
ConstantAnglePointY = PivotPointY
If ConstantAnglePointX - PivotPointX = 0 And ConstantAnglePointY > PivotPointY Then ConstantAngle = "1.57079632679"
If ConstantAnglePointX - PivotPointX = 0 And ConstantAnglePointY < PivotPointY Then ConstantAngle = "4.71238898038"
If ConstantAnglePointX - PivotPointX <> 0 Then ConstantAngle = Atn((ConstantAnglePointY - Point1Y) / (ConstantAnglePointX - PivotPointX))
NewAnglePointX = Point2X
NewAnglePointY = PivotPointY
If NewAnglePointX - PivotPointX = 0 And NewAnglePointY > PivotPointY Then NewAngle = "1.57079632679"
If NewAnglePointX - PivotPointX = 0 And NewAnglePointY < PivotPointY Then NewAngle = "4.71238898038"
If NewAnglePointX - PivotPointX <> 0 Then NewAngle = Atn((NewAnglePointY - Point2Y) / (NewAnglePointX - PivotPointX))
NewAngle = -NewAngle
FullAngle = -(ConstantAngle + NewAngle)

'displaying the angle-
AngleLabel.Caption = "ANGLE = " & FullAngle
End Sub

Private Sub Form_Load()
'Algorithm for finding an angle of three points on a 2D plane.
'By Brad Thompson and Ryan Williams 12/16/2000

'Note: There are three points: the first point, the second point, and the pivot point. The line formed by the first point and the pivot point is zero radians.

'1. Take X coordinate from first point and Y coordinate from pivot point and make a new point.
'2. Find tan^-1 (change in Y from the new point to the first point / change in X from the new point to the pivot point) to get the first angle.
'3. Take X coordinate from second point and Y coodinate from the pivot point and make a new point.
'4. Find tan^-1 (change in Y from the new point to the second point / change in X from new point to the pivot point) to get the second angle.
'5. Get the negative of the two angles added together to get the total angle measurement.

'This prog. finds the angle of the second point with the line
'formed by the pivot point and the first point as the zero.
'Measurements are in radians. ATN = arctan or tan^-1.

Point1X = FirstPoint.Left
Point1Y = FirstPoint.Top
Point2X = SecondPoint.Left
Point2Y = SecondPoint.Top
PivotPointX = PivotPoint.Left
PivotPointY = PivotPoint.Top
ThingMoving = "First"
Combo1.AddItem "The First Point", 0
Combo1.AddItem "The Second Point", 1
Combo1.AddItem "The Pivot Point", 2
Line1.X1 = Point1X + 7
Line1.Y1 = Point1Y + 7
Line1.X2 = PivotPointX + 7
Line1.Y2 = PivotPointY + 7
Line2.X1 = PivotPointX + 7
Line2.Y1 = PivotPointY + 7
Line2.X2 = Point2X + 7
Line2.Y2 = Point2Y + 7
FirstLabel.Caption = "First Point (" & Point1X & ", " & Point1Y & ")"
SecondLabel.Caption = "Second Point (" & Point2X & ", " & Point2Y & ")"
PivotLabel.Caption = "Pivot Point (" & PivotPointX & ", " & PivotPointY & ")"
ConstantAnglePointX = Point1X
ConstantAnglePointY = PivotPointY
If ConstantAnglePointX - PivotPointX = 0 And ConstantAnglePointY > PivotPointY Then ConstantAngle = "1.57079632679"
If ConstantAnglePointX - PivotPointX = 0 And ConstantAnglePointY < PivotPointY Then ConstantAngle = "4.71238898038"
If ConstantAnglePointX - PivotPointX <> 0 Then ConstantAngle = Atn((ConstantAnglePointY - Point1Y) / (ConstantAnglePointX - PivotPointX))
NewAnglePointX = Point2X
NewAnglePointY = PivotPointY
If NewAnglePointX - PivotPointX = 0 And NewAnglePointY > PivotPointY Then NewAngle = "1.57079632679"
If NewAnglePointX - PivotPointX = 0 And NewAnglePointY < PivotPointY Then NewAngle = "4.71238898038"
If NewAnglePointX - PivotPointX <> 0 Then NewAngle = Atn((NewAnglePointY - Point2Y) / (NewAnglePointX - PivotPointX))
NewAngle = -NewAngle
FullAngle = -(ConstantAngle + NewAngle)

'displaying the angle-
AngleLabel.Caption = "ANGLE = " & FullAngle
End Sub

Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
MouseX = X
MouseY = Y
End Sub


P.S. I am very glad to see my "HOW does vb make a circle?" post is on fire. To all of the people out there who learned trig 3 years before me- wait until I'm about 35 and you can come to my house and kick my ass and call me stupid.

[Edited by krah on 12-17-2000 at 10:43 PM]