Qbasicnews.com

Full Version: Angles between two points (3D)
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
OK... so I have this sub to find the angles between two points in 3D space... note that theta and phi are the output variables that contain the two angles.

[syntax="qbasic"]SUB angle (x1, y1, z1, x2, y2, z2, theta AS INTEGER, phi AS INTEGER)
x = x1 - x2
y = y1 - y2
z = z1 - z2

d = SQR(x ^ 2 + y ^ 2)

IF x <> 0 THEN
theta = ATN(y / x) / (pi! / 180)
END IF

IF d <> 0 THEN
phi = ATN(z / d) / (pi! / 180)
END IF
END SUB[/syntax]

..But sometimes, it doesn't work correctly. When the angle for theta should be 91, it becomes 271. Can anyone help?
Quadrants. :*)
*mutters something about cryptic answers*

So... when the points are in different quadrants, I have to change the equation? Now that I think about it, the last time I used this equation was on a 2D plane, and my coordinates never went below (0,0)...

Hmm...
the inverse cosine of the dot product returns the angle, or something like that.
Aha! found the problem(s)-- first, my x and y should have been switched, so it's ATN(x / y). Second, I needed to add 180 whenever the y difference is less than zero. That way, when it jumps to 271, after adding 180 it becomes 451... which is really just 91 degrees Big Grin

[syntax="qbasic"]SUB angle (x1, y1, z1, x2, y2, z2, theta AS INTEGER, phi AS INTEGER)
x = x1 - x2
y = y1 - y2
z = z1 - z2

d = SQR(x ^ 2 + y ^ 2)

IF y > 0 THEN
theta = ATN(x / y) / (pi180!)
ELSEIF y < 0 THEN
theta = ATN(x / y) / (pi180!) + 180
END IF


IF d > 0 THEN
phi = ATN(z / d) / (pi180!)
ELSEIF d < 0 THEN
phi = ATN(z / d) / (pi180!) + 180
END IF
END SUB[/syntax]

Thanks for the help Smile
Nope, scratch that. apparently the quadrants thing is still an issue.

Hrm.
Quote:Nope, scratch that. apparently the quadrants thing is still an issue.

Hrm.

Your formula for Phi is wrong:

Not atn but ARcos


ie.

Angle= Arcos(z/Distance)