Rotation of a set of points onto the X axis

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;
Your help is greatly appreciated.
Nancy

Answers (1)

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];
%--- Your program starts ---
%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,:))

7 Comments

I am playing with theta because the range of atan is only -pi/2 to pi/2. So if the points are in the second or third quadrant, wont that give a erroneous result.
I want the points to be oriented on the positive X axis.
I mean I want the fitted line to lie on the positive X axis.
If the slope is positive, the line is always in quadrant I and III, and the theta is positive. You rotate -theta to make it horizontal.
If the slope is negative, the line is always in quadrant II and IV, and the theta is negative. Again you rotate -theta to make it horizontal.
So I don't see the need to play with theta. But I guess you mean to have the upper half of the line toward the positive x axis? In any case, your original code already make the line horizontal. All you need to do is to displace it on to the x axis. The change I made still applies, you just may need to use '+' instead of '-' when you rotate it with pi+theta.
If the points are in quadrant II or III, rotation with a angle theta that lies between -pi/2 to pi/2 wont bring the line onto the X axis.
You can try running the code with X = [-4 -3 -2 0;4 3 2 0]; The rotated line lies on the negative X axis.
In case of the intercept poly(2) 99% of the times is 0.
I just want to know if my code is correct, it looks too crude. :)
Thanks for your help anyways.
If poly(2) is most of time 0, then your program should work already.
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.
Then why not show an example that gives you the unexpected result?

Sign in to comment.

Categories

Find more on Graphics Object Properties in Help Center and File Exchange

Asked:

on 2 Mar 2012

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!