如何在MATLAB中检测严格的顺时针/计数器clockwse运动

时间:2011-10-03 22:02:22

标签: matlab

我需要编写一个小程序来测试一条线(位置矢量)是否严格顺时针或CCLW运动。我尝试使用atand来找到角度,但是当它通过90度时,它可以从负值跳到正值,如果我使用斜率法,它将具有相同的效果。

然而,动作不必在90度切割,它可以从89跳到91.然后可能发生大的斜坡跳跃。请知道

谢谢

1 个答案:

答案 0 :(得分:5)

这样做的一种方法是计算连续位置向量的叉积。如果所有的交叉产品都是正数,则该线以严格的顺时针方向移动。同样,如果它们都是负数,则该线逆时针移动。如果符号混合,则线条不会严格地沿一个角度方向移动:

function checkRotation(pos)

pos(:,3) = 0;
pos = unique(sum(sign(cross(pos(1:end-1,:), pos(2:end,:))), 2));
if isequal(pos, 1)
    disp('Rotation was counter-clockwise');
elseif isequal(pos, -1)
    disp('Rotation was clockwise');
else
    disp('No strict rotation direction');
end

-10<=x<=10-10<=y<=10上创建一些随机位置向量并测试轮换:

>> pos = 20 * rand([10, 2]) - 10

pos =

         -8.28968405819912          9.26177078573826
         -4.75035530603335         0.936114374779359
          6.02029245539477         0.422716616080031
         -9.41559444875707         -5.36811226582952
          8.57708278956089        -0.222045121596661
          4.60661725710906          2.48120176347379
        -0.227820523928417          3.58271081731495
          1.57050122046878         -2.08969568662814
         -5.25432840456957         -2.65126702911047
        -0.823023436401378          9.75964006323266

>> checkRotation(pos)
No strict rotation direction

创建仅移动CCW并测试的位置向量:

>> theta = 0:15:180;
>> pos = [cosd(theta)' sind(theta)'];
>> checkRotation(pos)
Rotation was counter-clockwise

,类似于CW轮换:

>> theta = 180:-15:0;
>> pos = [cosd(theta)' sind(theta)'];
>> checkRotation(pos)
Rotation was clockwise

请注意,旋转检测的成功受到采样率的限制。如果线在线位置的连续采样中逆时针旋转超过180度,则与顺时针方向上的旋转小于180度无法区分。这是aliasing的一个例子。