Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine whether the direction of a line segment is clockwise or anti clockwise

I have a list of 2D points (x1,y1),(x2,y2)......(Xn,Yn) representing a curved segment, is there any formula to determine whether the direction of drawing that segment is clockwise or anti clockwise ?

any help is appreciated

like image 923
Ahmed Abobakr Avatar asked Dec 07 '22 00:12

Ahmed Abobakr


2 Answers

Alternately, you can use a bit of linear algebra. If you have three points a, b, and c, in that order, then do the following:

 1)  create the vectors u = (b-a) = (b.x-a.x,b.y-a.y) and v = (c-b) ...
 2) calculate the cross product uxv = u.x*v.y-u.y*v.x
 3) if uxv is -ve then a-b-c is curving in clockwise direction (and vice-versa).

by following a longer curve along in the same manner, you can even detect when as 's'-shaped curve changes from clockwise to anticlockwise, if that is useful.

like image 53
Penguino Avatar answered Jan 12 '23 00:01

Penguino


One possible approach. It should work reasonably well if the sampling of the line represented by your list of points is uniform and smooth enough, and if the line is sufficiently simple.

  1. Subtract the mean to "center" the line.
  2. Convert to polar coordinates to get the angle.
  3. Unwrap the angle, to make sure its increments are meaningful.
  4. Check if total increment is possitive or negative.

I'm assuming you have the data in x and y vectors.

theta = cart2pol(x-mean(x), y-mean(y)); %// steps 1 and 2
theta = unwrap(theta); %// step 3
clockwise = theta(end)<theta(1); %// step 4. Gives 1 if CW, 0 if ACW

This only considers the integrated effect of all points. It doesn't tell you if there are "kinks" or sections with different directions of turn along the way.

A possible improvement would be to replace the average of x and y by some kind of integral. The reason is: if sampling is denser in a region the average will be biased towards that, whereas the integral wouldn't.

like image 38
Luis Mendo Avatar answered Jan 11 '23 23:01

Luis Mendo