Rotation of a set of points onto the X axis
Show older comments
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)
Honglei Chen
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];
%--- 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
Nancy
on 2 Mar 2012
Nancy
on 2 Mar 2012
Honglei Chen
on 2 Mar 2012
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.
Nancy
on 2 Mar 2012
Honglei Chen
on 2 Mar 2012
If poly(2) is most of time 0, then your program should work already.
Nancy
on 2 Mar 2012
Honglei Chen
on 3 Mar 2012
Then why not show an example that gives you the unexpected result?
Categories
Find more on Graphics Object Properties 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!