Rotating a line given the angle and a vector

38 ビュー (過去 30 日間)
Ravindra P N
Ravindra P N 2016 年 11 月 30 日
編集済み: RMH 2018 年 12 月 20 日
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 件のコメント
KSSV
KSSV 2016 年 11 月 30 日
off course you can..
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 件のコメント
Ravindra P N
Ravindra P N 2016 年 11 月 30 日
rotate With respect to x axis
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 Center および File ExchangeProgramming についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by