Qbasicnews.com
Angle between two points - Printable Version

+- Qbasicnews.com (http://qbasicnews.com/newforum)
+-- Forum: Qbasic "like" compilers/interpreters (http://qbasicnews.com/newforum/forum-5.html)
+--- Forum: FB Discussion & Programming Help (http://qbasicnews.com/newforum/forum-15.html)
+--- Thread: Angle between two points (/thread-7260.html)

Pages: 1 2


Angle between two points - KiZ - 05-14-2005

I used to use this old big-ass homebrew routine for figuring out the angle between two points, but for my current project I really want something a big sleeker.

So is there a nice simple formula or alg for working it out?


Angle between two points - keeling - 05-14-2005

Relative to what? There isn't an angle between two points... there is a line. You have to have at least 3 points to get an angle.


Angle between two points - shiftLynx - 05-14-2005

Use the dot product:

a . b = |a||b|cos(theta)

Thus cos(theta) = a . b / |a||b|

Since what you actually want to find is the angle between the vector A->B relative to A->North where North is 0i - 1j. So, for finding the angle of B relative to A and North:-

Let p = b - a
Let q = 0i - 1j - a

|p| = sqrt(p . p)
|q| = sqrt(q . q)

theta = arccos( (p . q) / (sqrt(p . p) * sqrt(q . q)) )

Remember that this will not give a bearing; it will return either an acute or obtuse angle. If you draw this on a diagram it makes a lot more sense... draw a dot and label it A. Draw a vertical line up from the dot -- this represents the North vector. Now, for the angle to be theta, the angle can be measured to either the left or right of the North line. If you really want, you can do some more processing to determine which side the angle is on... if you do want to do that, reply and I will write about it.


Angle between two points - Jofers - 05-14-2005

You people is nuts. Dot products? You don't need that unless you're working with vectors. For two simple 2d cartesian points:

Code:
' To calculate an angle from two points.
dy = y2 - y1
dx = x2 - x1
radians = atan2(dy, dx)

' To convert radians to degrees
degrees = radians * 180 / pi

' To calculate PI
pi = atn(1) * 4

If you do want to work with vectors, and find the angle between two vectors, you can find (a . b) like this:
Code:
'calculate a dot product:
a_mag = sqrt((ax2 - ax1)^2 + (ay2 - ay1)^2)
b_mag = sqrt((bx2 - bx1)^2 + (by2 - by1)^2)
dot_product = a_mag * b_mag * cos(b_ang - a_ang)
where a_ang and b_ang would be found using the method above.


Angle between two points - KiZ - 05-14-2005

Thanks, jofers, thats just what I was looking for, but It doesnt work

e.g. It puts the angle between (100,100) and (150,150) as 57 degrees. Surely this would be 45 degrees...

Perhaps I might just go back and use my hombrew version, because it seems that retrieving an angle can be odd in certain circumstances.

But any light to shed on that?

*edit* aha! needs to be (radians! * 180) / 3.14, simply needed some brackets. and to compliment it and produce a nice little routine, add 90 to the result. Works great! Ta.


Angle between two points - Z!re - 05-14-2005

Do a search.. in the QB programming help subforum.. I asked about it for MOORPG, back in the day..


Angle between two points - Jofers - 05-14-2005

Huh? Those brackets shouldn't be needed...

Just a note about adding 90... in trigonometry, or any math/science zero degrees always points east. It's only on your compass that it will ever point north, for obvious reasons Smile


Angle between two points - Dr_Davenstein - 05-14-2005

Here ya go.

Code:
FUNCTION Find_Angle(X1 as Single, Y1 as Single, X2 as Single, Y2 as Single) as Integer
   Dim Temp_Angle as Integer
   Temp_Angle = -atan2(y2-y1, x2-x1) * 57.29577951308232
   If Temp_Angle<0 then Temp_Angle= 360 + Temp_Angle
   Find_Angle = Temp_Angle
End Function



Angle between two points - keeling - 05-14-2005

Then you mean realitive to the origin.... there are math teachers somewhere crying. :-)


Angle between two points - Jofers - 05-14-2005

If the origin is at point A, I guess. You could call it the angle of a vector. Or the angle of point b relative to point a.