根据方向向量旋转矩形

时间:2019-06-05 09:19:45

标签: matlab geometry coordinates matlab-figure rectangles

我正在基于具有长度和宽度参数的点构造一个矩形。而且我想尝试根据计算出的方向向量旋转该矩形

mysql

代码:

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password'

我知道旋转矩形存在一些错误,因为矩形在另一侧旋转。创建新坐标时,我尝试了所有可能的组合。

enter image description here

关于我要去哪里的任何建议都会有所帮助

1 个答案:

答案 0 :(得分:2)

您可以使用旋转矩阵。旋转将立即应用于每个坐标:

%    Rectangle coordinate
%    x  y
R = [2, 3
     5, 3
     5, 8
     2, 8]

%Anonymous function that will create the rotation matrix (input in radian)
romat = @(a)[cos(a), -sin(a); sin(a), cos(a)];

%Rotation of the Rectangle, with A = atan2(d(1), d(2));
Rrot = ((R-mean(R))*romat(A))+mean(R)

在操作过程中,我们必须减去矩形的质心,以便围绕[0,0]点进行旋转。

结果:

hold on
%Rectangle
patch(R(:,1),R(:,2),'green')
%Rotated rectangle
patch(Rrot(:,1),Rrot(:,2),'red','facealpha',0.5)
%Euclidian vector
quiver(mean(R(:,1)),mean(R(:,2)),d(1),d(2),...
       'ro','MarkerFaceColor','red','linewidth',3)
axis equal

enter image description here

您的代码有什么问题:

您应用以下旋转矩阵:

M = [cos(a) sin(a)
    -sin(a) cos(a)]

代替

M = [cos(a) -sin(a)
     sin(a)  cos(a)]