Results 1 to 25 of 25

Thread: Calculating the angle of a Line in a circle(resolved)

  1. #1

    Thread Starter
    Fanatic Member paralinx's Avatar
    Join Date
    Jun 2005
    Location
    Michigan
    Posts
    987

    Resolved Calculating the angle of a Line in a circle(resolved)

    Hi all,

    I've got some complicated physics equations I need to use in VB, and I need to figure out the angle of a line, from 0° to 180°...


    Here is a screen shot of the line:
    Attached Images Attached Images  
    Last edited by paralinx; Nov 5th, 2005 at 08:36 PM.

  2. #2
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: Calculating the angle of a Line in a circle

    what data are you given? startpoint and end point or line?

  3. #3

    Thread Starter
    Fanatic Member paralinx's Avatar
    Join Date
    Jun 2005
    Location
    Michigan
    Posts
    987

    Re: Calculating the angle of a Line in a circle

    yes? you have the starting point, ending point of the line, it's X1, X2, Y1, and Y2...

  4. #4
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: Calculating the angle of a Line in a circle

    VB Code:
    1. Private Function GetAngle(X1 As Long, Y1 As Long, X2 As Long, Y2 As Long) As Double
    2.     Dim PI As Double
    3.     Dim theta As Double
    4.     PI = Atn(1) * 4
    5.     'get angle of line in radians
    6.     'deltay/deltax = tan(angle)
    7.     theta = Atn((Y2 - Y1) / (X2 - X1))
    8.     'convert to degrees
    9.     'there are 180 degrees per PI radians
    10.     GetAngle = theta * 180 / PI
    11. End Function

  5. #5
    Smitten by reality Harsh Gupta's Avatar
    Join Date
    Feb 2005
    Posts
    2,938

    Re: Calculating the angle of a Line in a circle

    Quote Originally Posted by moeur
    VB Code:
    1. Private Function GetAngle(X1 As Long, Y1 As Long, X2 As Long, Y2 As Long) As Double
    2.     Dim PI As Double
    3.     Dim theta As Double
    4.     PI = Atn(1) * 4
    5.     'get angle of line in radians
    6.     'deltay/deltax = tan(angle)
    7.     theta = Atn((Y2 - Y1) / (X2 - X1))
    8.     'convert to degrees
    9.     'there are 180 degrees per PI radians
    10.     GetAngle = theta * 180 / PI
    11. End Function
    umm, what is Atn function? sorry i forgot.
    Show Appreciation. Rate Posts.

  6. #6
    Smitten by reality Harsh Gupta's Avatar
    Join Date
    Feb 2005
    Posts
    2,938

    Re: Calculating the angle of a Line in a circle

    got it, inverse of TANGENT!!
    am i right??
    Show Appreciation. Rate Posts.

  7. #7
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: Calculating the angle of a Line in a circle

    Atn is called ArcTangent (Gupta is correct)
    Atn(x) is the angle who's Tangent is x

  8. #8
    Smitten by reality Harsh Gupta's Avatar
    Join Date
    Feb 2005
    Posts
    2,938

    Re: Calculating the angle of a Line in a circle

    Quote Originally Posted by moeur
    Atn is called ArcTangent (Gupta is correct)
    Atn(x) is the angle who's Tangent is x
    mouer, sorry this time you confused me. is Atn different from Atn(x)??
    Show Appreciation. Rate Posts.

  9. #9
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Calculating the angle of a Line in a circle

    No, they're the same thing. He was just using it in a sentence.

  10. #10
    Smitten by reality Harsh Gupta's Avatar
    Join Date
    Feb 2005
    Posts
    2,938

    Re: Calculating the angle of a Line in a circle

    Quote Originally Posted by mendhak
    No, they're the same thing. He was just using it in a sentence.
    thnx moeur, mendhak
    Show Appreciation. Rate Posts.

  11. #11

    Thread Starter
    Fanatic Member paralinx's Avatar
    Join Date
    Jun 2005
    Location
    Michigan
    Posts
    987

    Re: Calculating the angle of a Line in a circle

    Moeur,

    I dont think this is working correctly because this happens:

    that should be 180 degrees
    Attached Images Attached Images  

  12. #12
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: Calculating the angle of a Line in a circle

    What are your X1, Y1 and X2,Y2?

  13. #13

    Thread Starter
    Fanatic Member paralinx's Avatar
    Join Date
    Jun 2005
    Location
    Michigan
    Posts
    987

    Re: Calculating the angle of a Line in a circle

    here they are



    they can change when the user clicks and drags the line:

    VB Code:
    1. Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    2.     If Moving Then
    3.         Line1.X2 = X
    4.         Line1.Y2 = Y
    5.         angle = GetAngle(Line1.Y2, Line1.Y1, Line1.X2, Line1.X1)
    6.         lblDegrees.Caption = angle & "°"
    7.     End If
    8. End Sub
    Attached Images Attached Images  

  14. #14
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Calculating the angle of a Line in a circle

    Try putting the parameters in the right order
    VB Code:
    1. angle = GetAngle(Line1.X1, Line1.Y1, Line1.X2, Line1.Y2)

  15. #15

    Thread Starter
    Fanatic Member paralinx's Avatar
    Join Date
    Jun 2005
    Location
    Michigan
    Posts
    987

    Re: Calculating the angle of a Line in a circle

    yeah that'd probably help

  16. #16

    Thread Starter
    Fanatic Member paralinx's Avatar
    Join Date
    Jun 2005
    Location
    Michigan
    Posts
    987

    Re: Calculating the angle of a Line in a circle

    ok now it works better, but on the right side of 90 degrees it goes negatively...
    Attached Images Attached Images  

  17. #17
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Calculating the angle of a Line in a circle

    use the abs() ? even though it shouldnt return negative...

  18. #18
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Calculating the angle of a Line in a circle

    Or swap x2 and x1 of x2>x1, and the same for y

  19. #19

    Thread Starter
    Fanatic Member paralinx's Avatar
    Join Date
    Jun 2005
    Location
    Michigan
    Posts
    987

    Re: Calculating the angle of a Line in a circle

    Quote Originally Posted by |2eM!x
    use the abs() ? even though it shouldnt return negative...
    the negative is not the only problem, when you move the line to the right of the 90 degrees it should return numbers from 91 - 180 degrees...

  20. #20

    Thread Starter
    Fanatic Member paralinx's Avatar
    Join Date
    Jun 2005
    Location
    Michigan
    Posts
    987

    Re: Calculating the angle of a Line in a circle

    Quote Originally Posted by dglienna
    Or swap x2 and x1 of x2>x1, and the same for y
    I tried this also and I get the same exact thing for whichever I try

  21. #21
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: Calculating the angle of a Line in a circle

    VB Code:
    1. Private Function GetAngle(X1 As Long, Y1 As Long, X2 As Long, Y2 As Long) As Double
    2.     Dim PI As Double
    3.     Dim theta As Double
    4.     Dim deltaX As Long
    5.     Dim deltaY As Long
    6.     'get angle of line in radians
    7.     'deltay/deltax = tan(angle)
    8.     deltaY = Y1 - Y2
    9.     deltaX = X2 - X1
    10.     PI = Atn(1) * 4
    11.     'adjust to our coordinate system
    12.         If deltaX = 0 Then
    13.             'fix division by zero errors
    14.             If Y1 > Y2 Then
    15.                 theta = 180
    16.             Else
    17.                 theta = 90
    18.             End If
    19.         ElseIf deltaX > 0 And deltaY >= 0 Then '1st quadrant
    20.             theta = Atn((Y1 - Y2) / (X2 - X1))
    21.         ElseIf deltaX < 0 And deltaY >= 0 Then '2nd quadrant
    22.             theta = Atn((Y1 - Y2) / (X2 - X1)) + PI
    23.         ElseIf deltaX < 0 And deltaY < 0 Then '3rd quadrant
    24.             theta = Atn((Y1 - Y2) / (X2 - X1)) + PI
    25.         ElseIf deltaX > 0 And deltaY < 0 Then '4th quadrant
    26.             theta = Atn((Y1 - Y2) / (X2 - X1)) + 2 * PI
    27.         End If
    28.    
    29.     'convert to degrees
    30.     'there are 180 degrees per PI radians
    31.     'if theta < 0 then theta =
    32.     GetAngle = theta * 180 / PI
    33. End Function

  22. #22

    Thread Starter
    Fanatic Member paralinx's Avatar
    Join Date
    Jun 2005
    Location
    Michigan
    Posts
    987

    Re: Calculating the angle of a Line in a circle

    great thanks moeur

  23. #23
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Calculating the angle of a Line in a circle

    moeur, could you possible explain that? I thought that it had something to do with quadrants, but didn't want to try to guess at the answer. If not, that's ok, also. I was thinking that 0 degrees was at 9'oclock, and that was the issue. Maybe I was close.

  24. #24
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: Calculating the angle of a Line in a circle(resolved)

    The problem is how the ArcTangent is defined
    -90° < Atn(x) < 90°
    for
    -infinity < x < infinity

    But that is not what we wanted so I had to adjust
    Last edited by moeur; Nov 6th, 2005 at 01:09 AM.

  25. #25
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Calculating the angle of a Line in a circle(resolved)

    Well, I wish that I could have seen an explanation, but I found this link, and it didn't help much.

    http://mathworld.wolfram.com/InverseTangent.html

    I looked up tangent, and saw the same images.

    http://mathworld.wolfram.com/Tangent.html

    I guess I don't know what I'm looking at.

    I used to know the formula to draw a circle with COS and SIN, so thought this wouldn't be too hard to grasp

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