Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Angles between two points (3D)
#1
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]
Reply
#2
Quadrants. :*)
y smiley is 24 bit.
[Image: anya2.jpg]

Genso's Junkyard:
http://rel.betterwebber.com/
Reply
#3
*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]
Reply
#4
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
Reply
#5
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
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]
Reply
#6
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]
Reply
#7
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)
y smiley is 24 bit.
[Image: anya2.jpg]

Genso's Junkyard:
http://rel.betterwebber.com/
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)