Rotating a line given the angle and a vector
23 views (last 30 days)
Show older comments
I have 2 points (x1, y1) and (x2, y2). I want to rotate it clockwise by 123 deg and find the co-ordinate (x2', y2') after rotation. Can anyone help?
0 Comments
Accepted Answer
KSSV
on 30 Nov 2016
Edited: KSSV
on 30 Nov 2016
p1 = rand(2,1) ;
p2 = rand(2,1) ;
%%rotation matrix
th = 123*pi/180 ;
R = [cos(th) -sin(th) ;sin(th) cos(th)] ;
%%rotate points
pr2 = R*p2 ;
figure
hold on
plot([p1(1) p2(1)],[p1(2) p2(2)] ,'r') ;
plot([p1(1) pr2(1)],[p1(2) pr2(2)] ,'b') ;
4 Comments
RMH
on 20 Dec 2018
Edited: RMH
on 20 Dec 2018
THIS DOES NOT WORK AS WRITTEN (and yes I'm necroposting, but this was my 1st result in google when I searched this problem). Here's why: You cannot rotate a POINT, you need to rotate a VECTOR. Test with dot product definiton:
rearrange and solve for theta (and use acosd for degrees):
th = acosd(dot(v1,v2)/(norm(v1)*norm(v2)))
Now, create your vectors by subtracting P2x-P1x, P2y-P1y, and do same for v2.
v1 = [p2(1)-p1(1); p2(2)-p1(2)];
v2 = [pr2(1)-p1(1); pr2(2)-p1(2)];
If you now run the code KSSV posted above, you will NOT achieve the same angle of rotation that you put in. That's because you tried to rotate a point. Now try this:
v2_correct = R*v1;
th_correct = acosd(dot(v1,v2_correct)/(norm(v1)*norm(v2_correct)))
You'll see exactly the input that you put in. And to convert from a vector back to X and Y points, just use the new vector, and:
P1 = P1; %P1 is simply your "origin" point.
P2_correct = [P1(1)+v2(1) ; P1(2)+v2(2)]
Note that to do matrix math correctly, R is a 2x2, and v1 needs to be a 2x1 (2 rows 1 column). Else matlab will throw an error.
Again, sorry to comment on an old post, but I was searching for the solution to this problem for myself and this was the first google link I came across. Hope this helps someone!
More Answers (1)
bio lim
on 30 Nov 2016
Rotate it to respect to what? If you are simply rotating points in the XY Cartesian plane counter-clockwise through 123 degrees about the origin around the Z axis, then just use a simple rotation matrix .
2 Comments
See Also
Categories
Find more on Graphics Performance 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!