Results 1 to 6 of 6

Thread: What is wrong with this code?

  1. #1

    Thread Starter
    Addicted Member krah's Avatar
    Join Date
    Jan 1999
    Location
    Arkansas, her hyuck!
    Posts
    163

    Cool

    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

    Code:
    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:42 PM]
    Is it tired in here or is it just me?

    Ryan Williams
    -Using Vb6-

  2. #2
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    Well, you will never get accurate math unless you explicitly type your variables. so for example NewAngle should be typed as a Double. (Also as a matter of style, putting all you If statements, etc. on one line makes the program hard to follow.)

  3. #3

    Thread Starter
    Addicted Member krah's Avatar
    Join Date
    Jan 1999
    Location
    Arkansas, her hyuck!
    Posts
    163

    Question

    You mean on one vertical line? I thought you only indented when you start a new loop. Also the math is as accurate as it needs to be, it's just that my code is making the wrong numbers come out.
    Is it tired in here or is it just me?

    Ryan Williams
    -Using Vb6-

  4. #4
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    When you don't explicitly type your variables they are all type Variant by default. You are correct that your math is still accurate when using the Variants (at least the part that I checked), but variants are larger and slower than other data types, so when you use them your program will be larger and slower than it needs to be. Here is how I would code a part of it:
    Code:
        Dim NewAnglePointX As Double
        Dim PivotPoint As Double
        Dim NewAnglePointY As Double
        Dim PivotPointY As Double
        Dim NewAngle As Double
        Dim Point2Y As Double
        Dim PivotPointX As Double
            
        ' Here is one way
    '    If NewAnglePointX - PivotPoint = 0 And NewAnglePointY > PivotPointY Then
    '        NewAngle = "1.57079632679"
    '    End If
    '    If NewAnglePointX - PivotPoint = 0 And NewAnglePointY < PivotPointY Then
    '        NewAngle = "4.71238898038"
    '    End If
    '    If NewAnglePointX - PivotPoint <> 0 Then
    '        NewAngle = Atn((NewAnglePointY - Point2Y) / (NewAnglePointX - PivotPointX))
    '    End If
    
        ' Here is a better way.
        Select Case True
            Case NewAnglePointX - PivotPoint = 0 And NewAnglePointY > PivotPointY
                NewAngle = "1.57079632679"
            Case NewAnglePointX - PivotPoint = 0 And NewAnglePointY < PivotPointY
                NewAngle = "4.71238898038"
            Case NewAnglePointX - PivotPoint <> 0
                NewAngle = Atn((NewAnglePointY - Point2Y) / (NewAnglePointX - PivotPointX))
        End Select
    You also have lines like this which I believe are particularly bad because it's SO easy to miss the "FirstPoint.Top = MouseY" part.

    If ThingMoving = "First" Then FirstPoint.Left = MouseX: FirstPoint.Top = MouseY

  5. #5

    Thread Starter
    Addicted Member krah's Avatar
    Join Date
    Jan 1999
    Location
    Arkansas, her hyuck!
    Posts
    163

    Question

    Okay I get it now. Thank you MartinLiss I do like the suggested formats. As for the variable declaration- yes I get what you're saying but that is not my main problem. I must remind you that what I need to figure out is why my code is returning these strange angles when it should be a consistent 0 to 6.283.
    Is it tired in here or is it just me?

    Ryan Williams
    -Using Vb6-

  6. #6

    Thread Starter
    Addicted Member krah's Avatar
    Join Date
    Jan 1999
    Location
    Arkansas, her hyuck!
    Posts
    163

    Exclamation

    I just remembered that if you copy the code you still won't have the right objects so I'm adding a link to a zip file in my first message and here. It's a zip file so you can just unzip the files, open up the project, and run it. Download it here http://www.flash.net/~krah1/angle.zip
    Is it tired in here or is it just me?

    Ryan Williams
    -Using Vb6-

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width