Calculating angles between two points in 3D that is measured from positive x-direction
Show older comments

I have multiple points that I need to calculate the angle between two points that respects to x-direction as shown in the image. The angle is measured from positive x direction (counter clockwise).
For 2D case as shown in the image:
% N is number of points
for j=1:N-1
for k=(j+1):N
% difference distance in x-direction
sepx=X(1,k)-X(1,j);
sepy=Y(1,k)-Y(1,j);
r = sqrt(sepx^2+sepy^2);
% use atan2d to return angle in degree between -180 to 180.
% use mod to return angle in degree between 0-360
theta=mod(atan2d(sepy,sepx),360);
end
end
For 3D case, I only changed this part
sepx=X(1,k)-X(1,j);
sepy=Y(1,k)-Y(1,j);
sepz=Z(1,k)-Z(1,j);
r = sqrt(sepx^2+sepy^2 +sepz^2);
theta=mod(atan2d(sepy,sepx),360);
I am really bad at 3D in term of visualization to project thing. Is that correct to find angle of two points in 3D that is measured from positive x-direction (counter clockwise). The counter clockwise for an observer looking from above on the xy-plane. Please helps. Thanks.
6 Comments
Star Strider
on 3 May 2016
I cannot understand what you want to do. Rather than doing your own conversions, use pol2cart, sph2cart and their inverse functions (linked at the end of the page for each).
Note that for spherical coordinates, there are 2 angles and the radius. For polar coordinates, there is 1 angle and the radius.
(I am not listing this as an Answer because it is not one.)
Image Analyst
on 3 May 2016
I don't understand either. I don't know what "with respect to the x-direction" means. Let's say you were to project those points into either the flat x-z plane, the y-z plane, or the x-y plane. Would any of those planes have the angle that you want?
Image Analyst
on 3 May 2016
Then just use the 2-D formula, ignoring your Z values completely.
Laura
on 3 May 2016
Star Strider
on 3 May 2016
Only for 3D (spherical) representation.
Accepted Answer
More Answers (0)
Categories
Find more on Surface and Mesh Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!