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

Mar*_*rco 3 matlab

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

然而,运动不必在90度切割,它可以从89跳到91.然后可能发生大的斜坡跳跃.请问任何想法

谢谢

b3.*_*b3. 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
Run Code Online (Sandbox Code Playgroud)

创建一些随机位置矢量-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度无法区分.这是别名的示例.