Posts: 729
Threads: 105
Joined: Feb 2003
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?
size=9]"To announce that there must be no criticism of the president, or that we are to stand by the president, right or wrong, is not only unpatriotic and servile, but is morally treasonable to the American public." -- Theodore Roosevelt[/size]
Posts: 3,288
Threads: 167
Joined: Nov 2001
Posts: 729
Threads: 105
Joined: Feb 2003
*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...
size=9]"To announce that there must be no criticism of the president, or that we are to stand by the president, right or wrong, is not only unpatriotic and servile, but is morally treasonable to the American public." -- Theodore Roosevelt[/size]
Posts: 788
Threads: 53
Joined: Nov 2002
the inverse cosine of the dot product returns the angle, or something like that.
oship me and i will give you lots of guurrls and beeea
Posts: 729
Threads: 105
Joined: Feb 2003
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
[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
size=9]"To announce that there must be no criticism of the president, or that we are to stand by the president, right or wrong, is not only unpatriotic and servile, but is morally treasonable to the American public." -- Theodore Roosevelt[/size]
Posts: 729
Threads: 105
Joined: Feb 2003
Nope, scratch that. apparently the quadrants thing is still an issue.
Hrm.
size=9]"To announce that there must be no criticism of the president, or that we are to stand by the president, right or wrong, is not only unpatriotic and servile, but is morally treasonable to the American public." -- Theodore Roosevelt[/size]
Posts: 3,288
Threads: 167
Joined: Nov 2001
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)