Rotating a line given the angle and a vector

23 visualizaciones (últimos 30 días)
Ravindra P N
Ravindra P N el 30 de Nov. de 2016
Editada: RMH el 20 de Dic. de 2018
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?

Respuesta aceptada

KSSV
KSSV el 30 de Nov. de 2016
Editada: KSSV el 30 de Nov. de 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 comentarios
KSSV
KSSV el 30 de Nov. de 2016
off course you can..
RMH
RMH el 20 de Dic. de 2018
Editada: RMH el 20 de Dic. de 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!

Iniciar sesión para comentar.

Más respuestas (1)

bio lim
bio lim el 30 de Nov. de 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 comentarios
Ravindra P N
Ravindra P N el 30 de Nov. de 2016
rotate With respect to x axis
bio lim
bio lim el 30 de Nov. de 2016
Editada: bio lim el 30 de Nov. de 2016
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.

Iniciar sesión para comentar.

Categorías

Más información sobre Graphics Performance en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by