How to rotate a rectangle until one of the edges is parallel to the x-axis (with unknown rotation angle)?
3 views (last 30 days)
Show older comments
xiuJe
on 28 Sep 2016
Commented: SHREYASI BHATTACHARYA
on 24 Aug 2018
Hello,
Within a (Cartesian) coordinate system, I have many tilted rectangles that I need to rotate so that one edge will be parallel to the x-axis. For simplicity let's just consider one rectangle. For each rectangle drawn in the system, I also have a separate point which I will also have to rotate. So, for example, if I had a tilted rectangle with these coordinates
x = [4 16 14 2];
y = [2 6 12 8];
and a separate point p1 = [5,7], then I need to rotate the rectangle until the edge which connects the vertices [4,2] and [16,6] is parallel to the x-axis, and also map the point p1. I don't mind where the vertices will end up as long as that edge is parallel to the x-axis.
Mapping the rectangle using simple arithmetics (Pythagoras etc) is easy, but then I don't know how to map the point p1 (or at least I've thought about this for so long, I can't think straight anymore!). I don't have a rotation angle, so I couldn't find a way how to calculate new points using a standard rotation matrix. I was thinking about something like this below, but I'm not sure how to implement this, so any advice would be great.
while slopeOfEdge~=0;
%keep rotating;
end
Thanks!!!
1 Comment
dpb
on 28 Sep 2016
I don't follow what "map" means in your context and how point p1 enters into the mix.
But, you can easily derive the angle of the lower edge boundary from the coordinates to derive the rotation matrix.
I guess, is the question to rotate around p1, maybe???
Accepted Answer
Massimo Zanetti
on 28 Sep 2016
Edited: Massimo Zanetti
on 29 Sep 2016
To find the angle between the edge of the rectangle and the x-axis do simply (using your notation for vertex as x,y):
c=[x(1),1;x(2),1]\[y(1);y(2)];
alpha=atan2(c(1),1);
Then you can just build a rotation matrix using this alpha:
R=[cos(-alpha),-sin(-alpha);
sin(-alpha),cos(-alpha)];
To obtain the new coordinates of the aligned rectangle:
W=R*[x;y];
newX=W(1,:);
newY=W(2,:);
plot(x,y,'b',newX,newY,'r'); axis equal
Notice that, you have to adjust the rotatation direction of R accounting for the sign of alpha, that's why in sin and cos the argument is -alpha.
Of course, you can use the same rotation matrix to map the point p.
2 Comments
SHREYASI BHATTACHARYA
on 24 Aug 2018
If I don't know any vertices of rectangle,then how I find rotation angle? Please help me by giving answer.
More Answers (0)
See Also
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!