## Rotation of a set of points onto the X axis

### Nancy (view profile)

on 2 Mar 2012
Hi Guys,
I know this is a simple problem but I am just not to able to figure out how to do it. I dont know if the code I have written is correct. So my problem. I have a set of 4 points in a 2D plane. The points are linear and can be fit to a line in the form on y=m*x+b. Usually b=0. So these fitted line will pass through the origin. The points are in all quadrants. So the fitted lines look like they radite out of the origin. I want to rotate all these points onto the x axis. I am having trouble finding out the exact angle of rotation since this would depend on the quadrant the points/fitted line lie in.
Can you please tell me if my code is right. It is very crude.
My points are in matrix X. It is a 2 row 4 column matrix. Row 1 has X coordinates and row 2 has corresponding Y coordinates.
%Fit a line to the points
poly=polyfit(X(1,1:4),X(2,1:4),1);
theta=atan(poly(1));
%computation of angle of rotation based on quadrant trajectory %lies. If in first or fourth it is -theta and if in second or third %it is -(pi+theta)
sumx=sum(X(1,:));
sumy=sum(X(2,:));
if (sumx>0)
theta=-theta;
else
theta=-(pi+theta);
end
%computation of rotation matrix. rotation is about X axis
rot = [cos(theta) -sin(theta); sin(theta) cos(theta)];
Xrot=rot*X;
Nancy

### Honglei Chen (view profile)

on 2 Mar 2012

I'm not sure why you need to play with theta, unless you care about the orientation. If you just want the line to become horizontal, rotate an angle of -theta should serve the purpose.
The reason that it does not lie on the x axis is because the rotation matrix is centered on origin. Therefore, once rotated, you need to move it onto the x axis.
I modified your program a little bit and put it below. I just used the -theta and the main change is at the end, where I compensate for the displacement after I did rot*X
% Test data
X = [-2 -1 1 2;0 -1 -3 -4];
%Fit a line to the points
poly=polyfit(X(1,1:4),X(2,1:4),1);
theta0=atan(poly(1));
theta=-theta0;
%computation of rotation matrix. rotation is about X axis
rot = [cos(theta) -sin(theta); sin(theta) cos(theta)];
Xrot=rot*X-poly(2)*cos(theta0); % <- change here
% --- Your program ends ---
% plot
plot(X(1,:),X(2,:),Xrot(1,:),Xrot(2,:))

Honglei Chen

### Honglei Chen (view profile)

on 2 Mar 2012
If poly(2) is most of time 0, then your program should work already.
Nancy

### Nancy (view profile)

on 2 Mar 2012
My data set involves a lot of set of points. It seems to work ok. There are still some points that dont fall on the positive X axis. I want to know if my code is correct or if there is a more elegant way to do this.
Honglei Chen

### Honglei Chen (view profile)

on 3 Mar 2012
Then why not show an example that gives you the unexpected result?