How to detect a strict clockwise/ counter clockwse motion in MATLAB
I need to write a small program which to test whether a line (position vector) does strictly clockwise or CCLW movement. I tried to use atand to find the angle, but it could jump from negative to positive value when it pass thought 90 deg, it will have the same thing if I use slope method.
H开发者_StackOverflowowever, the motion does not have to cut at 90 deg, it could jump from 89 to 91. Then a big slope jump could happen. Any idea please
Thanks
One way to do this would be to calculate the cross-product of consecutive position vectors. If all the cross-products are positive then the line moved with a strictly clockwise. Similarly, if they are all negative then the line moved counter-clockwise. If the signs are mixed then the line did not move strictly in one angular direction:
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
Create some random position vectors on -10<=x<=10
and -10<=y<=10
and test rotation:
>> 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
Create position vectors that move only CCW and test:
>> theta = 0:15:180; >> pos = [cosd(theta)' sind(theta)']; >> checkRotation(pos) Rotation was counter-clockwise
and similarly for CW rotation:
>> theta = 180:-15:0; >> pos = [cosd(theta)' sind(theta)']; >> checkRotation(pos) Rotation was clockwise
Note that the success of rotation detection is limited by your sampling rate. If the line rotates counter-clockwise by more than 180 degrees in successive samplings of the line position, it is indistinguishable from a rotation less than 180 degrees in the clockwise direction. This is an example of aliasing.
精彩评论