Rotating a line given the angle and a vector
조회 수: 28 (최근 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?
댓글 수: 0
채택된 답변
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
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
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
참고 항목
카테고리
Help Center 및 File Exchange에서 Graphics Performance에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!