Rotating a line given the angle and a vector

11 次查看(过去 30 天)
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?

采纳的回答

KSSV
KSSV 2016-11-30
编辑:KSSV 2016-11-30
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 个评论
RMH
RMH 2018-12-20
编辑:RMH 2018-12-20
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!

请先登录,再进行评论。

更多回答(1 个)

bio lim
bio lim 2016-11-30
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 个评论
bio lim
bio lim 2016-11-30
编辑:bio lim 2016-11-30
You can't rotate something with respect to an axis. You can rotate it about/around an axis, but you rotate something with respect to something that has a position.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Creating, Deleting, and Querying Graphics Objects 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by