Results 1 to 7 of 7

Thread: Angle of two points...

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2001
    Posts
    15

    Talking

    Hi I have two points on 2d plane and need to work out the angle that they create relative to +x axis..

    This is what I have come up with so far but it's too long and I'm sure there is a better way..

    x = DistX - SrcX
    y = DistY - SrcY
    if y>0 and x>0 then Angle = ATN(abs(y)/abs(x))
    if y>0 and x<0 then Angle = ATN(abs(x)/abs(y)) + 90
    if y<0 and x<0 then Angle = ATN(abs(y)/abs(x)) + 180
    if y<0 and x>0 then Angle = ATN(abs(x)/abs(y)) + 270

  2. #2
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    Harry.

    "From one thing, know ten thousand things."

  3. #3
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    It's becoming more and more famous

    although Arktangent(y,x) gives you the angle from 0 to 180, it's still Arctan and won't return angles above 180.

    Here's what you should use for 360 degree angles:
    Code:
    Function AngleByOffset(offsetX As Double, offsetY As Double) As Double
        If offsetX Then
            AngleByOffset = Atn(offsetY / offsetX) - (offsetX > 0) * 3.14159265358979
        Else
            AngleByOffset = 1.5707963267949 + (offsetY > 0) * 3.14159265358979
        End If
    End Function
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  4. #4

    Thread Starter
    New Member
    Join Date
    Feb 2001
    Posts
    15

    Thumbs up Thanks

    Thanks Kedaman,

    Works perfectly..

  5. #5
    Good Ol' Platypus Sastraxi's Avatar
    Join Date
    Jan 2000
    Location
    Ontario, Canada
    Posts
    5,134
    I need to find the angle of two points on a 3d plane... maybe someone could help me there, as I am not too good in maths...
    All contents of the above post that aren't somebody elses are mine, not the property of some media corporation.
    (Just a heads-up)

  6. #6
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    Well I just posted this in your other thread Sastraxi, but I may as well post it again here You can try the geometry tutorial at flipCode for some good geometry info. To get the angle between 2 vectors you use the dot product, that much I remember.
    Harry.

    "From one thing, know ten thousand things."

  7. #7
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    yep, pretty simple actually
    cos(angle) = (a·b) / |a||b|
    where a and b are the vectors.
    arcus cosinus can be substituted with this formula using arcus tangent, in vb5 help files:
    Arccos(X) = Atn(-X / Sqr(-X * X + 1)) + 2 * Atn(1)
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

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