Results 1 to 6 of 6

Thread: HELP! calculating vector components [RESOLVED]

  1. #1

    Thread Starter
    Hyperactive Member Arachnid13's Avatar
    Join Date
    Jan 2003
    Location
    England
    Posts
    327

    Resolved HELP! calculating vector components [RESOLVED]

    hi... i know this has been asked before but i haev spent the last half an hour searchin the forums for the answer.. basically does anyone have some code which you can use to turn two points (x1,y1),(x2,y2) into a vector, basically i want to find the direction from object 1 to object 2 in a vector form (assume the magnitude=1) and i know you need a different equation depending on which quadrant its in (0-90,90-180 etc) but i cant figure it out any help would b much appreciated
    Last edited by Arachnid13; Nov 15th, 2004 at 09:53 AM.
    Do you wake up in the morning feeling sleepy and grumpy? Then you must be Snow White

  2. #2
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: HELP! calculating vector components

    Hope this is what you want.



    Vector from A to B

    = ( 9 - (-12) , 20 - (-10) )

    In other words...

    (Destination.X - Start.X, Destination.Y - Start.Y)

    This example would give you (21, 30).

    To get your unit vector, divide both x and y by the magnitude...

    Mag = Sqrt(21^2 + 30^2)

    x = 21/mag
    y = 30/mag

    Giving you a unit vector of (0.57346, 0.81923), new magnitude = 1.

    (It doesn't matter what quadrants the points are in )





    Gurus: If I have got this wrong, please let me know because i'm not a math god
    Attached Images Attached Images  
    I don't live here any more.

  3. #3
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863
    Hi Arachnid13, did you get what you need? Or do you also need the direction in degrees? Let us know
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  4. #4
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863
    Found the routine I'm using
    VB Code:
    1. Public Const Pi = 3.141592654
    2. Public Const PiDurch180 = Pi / 180
    3. Public Const A180DurchPi = 180 / Pi
    4. Public Function Bearing(x1, y1, x2, y2) As Single
    5. 'Input X1, Y1, X2, Y2
    6. Calculates Bearing(0-360)
    7. 'Calculates from X1,Y1 to X2,Y2  in 360degree-system
    8. 'North, Y , Lat gets bigger!!!!
    9. 'East X, Long gets bigger!!!!!
    10. Dim dx As Single
    11. Dim dy As Single
    12. dx = x2 - x1
    13. dy = y2 - y1
    14. If dy <> 0 Then
    15.         Bearing = (Atn(dx / dy) * A180DurchPi)
    16.         If x2 > x1 Then
    17.                 If y2 > y1 Then
    18.                         Bearing = Bearing
    19.                 Else
    20.                         Bearing = 180 + Bearing
    21.                 End If
    22.         Else
    23.                 If y2 > y1 Then
    24.                         Bearing = 360 + Bearing
    25.                 Else
    26.                         Bearing = 180 + Bearing
    27.                 End If
    28.         End If
    29. Else
    30.         If x1 > x2 Then
    31.                 Bearing = 270
    32.         Else
    33.                 Bearing = 90
    34.         End If
    35. End If
    36. End Function
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  5. #5

    Thread Starter
    Hyperactive Member Arachnid13's Avatar
    Join Date
    Jan 2003
    Location
    England
    Posts
    327
    thanks guys, both are very useful, the second post was what i meant but ive just realised i can do what i needed with the first one and its much simpler... should have thought of it before... thanks!
    Do you wake up in the morning feeling sleepy and grumpy? Then you must be Snow White

  6. #6
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682
    No worries

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