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

Thread Subject:
How to rotate a point about an axis formed by 2 points

Subject: How to rotate a point about an axis formed by 2 points

From: jjspierx Spiering

Date: 31 May, 2010 17:15:21

Message: 1 of 8

Hello all, I have searched for an answer to this question for quite some time but am unable to find a solution that works for me. Here is what I am trying to do...

I have 3 points that are within a 3D cartesian coordinate system. I want 2 of the points to form an axis and have the 3rd point rotate about this axis. I have been reading about rotation matrices but I am having trouble understanding how to create a rotation matrix and then use that matrix to transform the 3D coordinates of the 3rd point that is to rotate about the axis formed by the other 2 points. If anybody has some ideas on how to do this I would be most greatful. Thank you.

Subject: How to rotate a point about an axis formed by 2 points

From: James Tursa

Date: 31 May, 2010 17:59:05

Message: 2 of 8

"jjspierx Spiering" <jjspierx@gmail.com> wrote in message <hu0qr9$eb0$1@fred.mathworks.com>...
> Hello all, I have searched for an answer to this question for quite some time but am unable to find a solution that works for me. Here is what I am trying to do...
>
> I have 3 points that are within a 3D cartesian coordinate system. I want 2 of the points to form an axis and have the 3rd point rotate about this axis. I have been reading about rotation matrices but I am having trouble understanding how to create a rotation matrix and then use that matrix to transform the 3D coordinates of the 3rd point that is to rotate about the axis formed by the other 2 points. If anybody has some ideas on how to do this I would be most greatful. Thank you.

There are several methods available. One way is to form a quaternion from your two points as an eigen-axis and then pick an angle for the rotation. Then you can do a quaternion multiply to rotate the third point. Or if you need a rotation matrix instead you can easily convert the quaternion to a direction cosine matrix first. There are submissions on the FEX to do quaternion calculations. e.g., these submissions by John Fuller for tutorials and conversions:

http://www.mathworks.com/matlabcentral/fileexchange/27653-euler-angle-dcm-quaternion-and-euler-vector-conversionteaching-gui

http://www.mathworks.com/matlabcentral/fileexchange/20696-function-to-convert-between-dcm-euler-angles-quaternions-and-euler-vectors

And other FEX submissions appear to be available to do the quaternion multiply itself.

James Tursa

Subject: How to rotate a point about an axis formed by 2 points

From: Matt J

Date: 31 May, 2010 19:16:04

Message: 3 of 8

Here's what I use:


function R=R3d(deg,u)
%R3D - 3D Rotation matrix counter-clockwise about an axis.
%
%R=R3d(deg,axis)
%
%Input is in degrees.
%
%See also Rx,Ry,Rz,R3d,M2d,M3d

R=eye(3);
u=u(:)/norm(u);
x=deg; %abbreviation

for ii=1:3
   
    v=R(:,ii);
    
    R(:,ii)=v*cosd(x) + cross(u,v)*sind(x) + (u.'*v)*(1-cosd(x))*u;
      %Rodrigues' formula
      
end

Subject: How to rotate a point about an axis formed by 2 points

From: Matt J

Date: 31 May, 2010 19:39:04

Message: 4 of 8

"jjspierx Spiering" <jjspierx@gmail.com> wrote in message <hu0qr9$eb0$1@fred.mathworks.com>...
> Hello all, I have searched for an answer to this question for quite some time but am unable to find a solution that works for me. Here is what I am trying to do...
>
> I have 3 points that are within a 3D cartesian coordinate system. I want 2 of the points to form an axis and have the 3rd point rotate about this axis. I have been reading about rotation matrices but I am having trouble understanding how to create a rotation matrix and then use that matrix to transform the 3D coordinates of the 3rd point that is to rotate about the axis formed by the other 2 points.
===========

Come to think of it, since the axis does not pass through the origin (if it did you would only need one point, not 2, to define it), it will be insufficient to perform a rotation only. You will need to do a translation as well.

The code below will generate a 4x4 matrix M expressing the rototranslation in homogeneous coordinates. You would use it as follows

M=M3d(AngleDegrees, FirstPoint-SecondPoint, SecondPoint); %transformation matrix

NewPoint=M(1:3,:) * [ThirdPoint(:);1];




function M=M3d(deg,u,x0)
%Generate roto-translation matrix for the rotation around an arbitrary line in 3D.
%
% M=M3d(deg,u,x0)
%
%in:
%
% deg: The counter-clockwise rotation about the line in degrees.
% u,x0: 3D vectors specifying the line in parametric form x(t)=x0+t*u
% Default for x0=0 (pure rotation).
%out:
%
% M: A 4x4 homogenous coordinate transform matrix representing
% the roto-translation.
%
%See also: Rx,Ry,Rz, R2d, R3d, M2d


if nargin<2, x0=[0;0;0]; end

x0=x0(:); u=u(:)/norm(u);

AxisShift=x0-(x0.'*u).*u;




Mshift=mkaff(eye(3),-AxisShift);

Mroto=mkaff(R3d(deg,u));

M=inv(Mshift)*Mroto*Mshift;

function M=mkaff(R,t)

if nargin<2, t=[0;0;0]; end

nn=size(R,1);


M=eye(nn+1);

M(1:end-1,1:end-1)=R;
M(1:end-1,end)=t(:);


function R=R3d(deg,u)
%R3D - 3D Rotation matrix counter-clockwise about an axis.
%
%R=R3d(deg,axis)
%
% deg: The counter-clockwise rotation about the axis in degrees.
%
%See also Rx,Ry,Rz,R3d,M2d,M3d

R=eye(3);
u=u(:)/norm(u);
x=deg; %abbreviation

for ii=1:3
   
    v=R(:,ii);
    
    R(:,ii)=v*cosd(x) + cross(u,v)*sind(x) + (u.'*v)*(1-cosd(x))*u;
      %Rodrigues' formula
      
end

Subject: How to rotate a point about an axis formed by 2 points

From: James Tursa

Date: 31 May, 2010 19:58:03

Message: 5 of 8

"Matt J " <mattjacREMOVE@THISieee.spam> wrote in message <hu11tk$g2p$1@fred.mathworks.com>...
> Here's what I use:
>
>
> function R=R3d(deg,u)
> %R3D - 3D Rotation matrix counter-clockwise about an axis.
> %
> %R=R3d(deg,axis)
> %
> %Input is in degrees.
> %
> %See also Rx,Ry,Rz,R3d,M2d,M3d
>
> R=eye(3);
> u=u(:)/norm(u);
> x=deg; %abbreviation
>
> for ii=1:3
>
> v=R(:,ii);
>
> R(:,ii)=v*cosd(x) + cross(u,v)*sind(x) + (u.'*v)*(1-cosd(x))*u;
> %Rodrigues' formula
>
> end

This assumes that the rotation vector intersects the origin, so any necessary translation will have to be done by the user in addition to using this function. e.g., take this example:

>> p1 = [1 0 1]';
>> p2 = [1 0 0]';
>> p3 = [2 0 1]';
>> R = R3d(180,p1-p2)
R =
    -1 0 0
     0 -1 0
     0 0 1
>> R*p3
ans =
    -2
     0
     1

Based on the original post as I interpret it the desired result is likely [0 0 1]', i.e. rotating p3 about an axis in space that does not intersect the origin. So to use R3d for this case one needs to pre-apply a translation and then post-apply the reverse translation, or use a form of rotation that includes the translation.

James Tursa

Subject: How to rotate a point about an axis formed by 2 points

From: jjspierx Spiering

Date: 31 May, 2010 21:34:04

Message: 6 of 8

Thank you very much Matt and James. M3D turned out to work perfectly for my purposes!

Subject: How to rotate a point about an axis formed by 2 points

From: jjspierx Spiering

Date: 31 May, 2010 22:08:04

Message: 7 of 8

I have another question related to this same topic...

Assuming now that I have 2 sets of the 3 points used previously to form an axis and then rotate about it. So now I have 2 axis and 2 points rotating about their respective axis. 2 planes are therefore formed, one between each set of 3 points. I am trying to figure out a way to find the axis formed by the intersection of those 2 planes. In 2-space this would be easy, I could just determine the slope of the planes (or lines in 2-space) and find the intersection point, ie the instant center. But in 3-space finding the equivalent instant axis is proving to be very difficult for me. Thanks in advance.

Subject: How to rotate a point about an axis formed by 2 points

From: Matt J

Date: 1 Jun, 2010 04:59:04

Message: 8 of 8

"jjspierx Spiering" <jjspierx@gmail.com> wrote in message <hu1c03$qtu$1@fred.mathworks.com>...
> I have another question related to this same topic...
>
> Assuming now that I have 2 sets of the 3 points used previously to form an axis and then rotate about it. So now I have 2 axis and 2 points rotating about their respective axis. 2 planes are therefore formed, one between each set of 3 points. I am trying to figure out a way to find the axis formed by the intersection of those 2 planes. In 2-space this would be easy, I could just determine the slope of the planes (or lines in 2-space) and find the intersection point, ie the instant center. But in 3-space finding the equivalent instant axis is proving to be very difficult for me. Thanks in advance.
===================

Hints:

(1) Suppose you have 3 points a,b,c. A normal N to the plane containing these points is N=cross(b-a,c-a) and the equation for this plane is

dot(N,[x,y,z])=dot(N,a)


(2) If 2 planes with normals N1 and N2 intersect in a line, a vector parallel to this line is cross(N1,N2)

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us