Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

New to MATLAB?

angle calculation in 3D space

Asked by Iman Alsharkawi

Iman Alsharkawi (view profile)

on 13 Jul 2011

Let's say I have a point, t1(x1 y1 z1) somewhere in space. And I have another point g1(x2 y2 z2) that lies on a plane somewhere in space. If I have the normal direction associated with that plane,and I want to calculate the angle of point t1 relative to that plane at point g1, is the following code legit? (basically finding the angle between the vector g1-t1 and the normal of the plane)And does my vector from g1 to t1 have to be normalized? Or the normal direction of the plane, for that matter?

t1 = [1 4 3];
g1 = [2 4 3];
% normal associated with g1
n1 = [-1 0 0]; %<-- does this have to be normalized??
% get vector from g1 to t1:
v1 = g1-t1;
angle = atan2(norm(cross(v1,n1)),dot(v1,n1)).*(180/pi)

Thanks!

1 Comment

Iman Alsharkawi

Iman Alsharkawi (view profile)

on 13 Jul 2011

Clearly it's been a long day for me. I think it just dawned on me that the normalization doesn't have to happen because all I care about are the directions of the vectors. But, I'd still like feedback on my method for calculating the angle.

Iman Alsharkawi

Iman Alsharkawi (view profile)

Products

2 Answers

Answer by Sean de Wolski

Sean de Wolski (view profile)

on 13 Jul 2011
Accepted answer

Your equation looks right to me. Well, that is it looks like what Roger recommends frequently:

CSSM: Roger Stafford on Angles

0 Comments

Sean de Wolski

Sean de Wolski (view profile)

Answer by Jan Simon

Jan Simon (view profile)

on 13 Jul 2011

You can try it:

v = [1 4 3];
n = [-1 0 0];
angle1 = atan2(norm(cross(v,n)), dot(v,n)).*(180/pi)
n = [-1000 0 0];
angle2 = atan2(norm(cross(v,n)), dot(v,n)).*(180/pi)

I did not use g1 - t1, because it is [1,0,0] by accident, which might hide problems.

Usually this kind of gunshot debugging is not reliable. But if it helps... ;-)

Take into account, that a normalization can help to control rounding errors, even if the algorithm does not demands for a normalization mathematically, e.g. if n is [1e12, 0, 0] or [1e-12, 0, 0].

1 Comment

Iman Alsharkawi

Iman Alsharkawi (view profile)

on 13 Jul 2011

=) like I said... it's been a long day... Thanks to both of you!

Jan Simon

Jan Simon (view profile)

Contact us