Problem 194. Clockwise or Counterclockwise
Given a list of 2-d points defining the vertices of a polygon, determine whether these points are sorted clockwise.
The inputs to this function are two vectors x and y (with equal size) containing the x- and y- coordinates of the polygon vertices, respectively.
The function should return true when sorted clockwise, and false when sorted counterclockwise (the polygon vertices will always be sorted either way).
Example:
x = [-1,-1,1,1]; y = [-1,1,1,-1];
defines a square, the vertices are listed clockwise
d_correct = true;
Solution Stats
Problem Comments
-
3 Comments
added more samples to the test
Hello, Alfonso Nieto-Castanon. This is a very nice problem. It was interesting to see the variety of approaches used to solve it. It was also interesting to notice a few submissions failed your added test cases from circa Dec 2017. Do you allow for self-intersecting polygons, as per https://en.wikipedia.org/wiki/Polygon ?
I suspect a common solution tactic will fail badly when faced with self-intersecting polygons. —DIV
...On second thought, maybe there's no consistent definition of CW/CCW for self-intersecting polygons, such as figure-eight or bow-tie ("cross-quadrilateral") shapes.
Solution Comments
Show commentsProblem Recent Solvers207
Suggested Problems
-
Make the vector [1 2 3 4 5 6 7 8 9 10]
50183 Solvers
-
Given an unsigned integer x, find the largest y by rearranging the bits in x
1858 Solvers
-
233 Solvers
-
Flag largest magnitude swings as they occur
681 Solvers
-
Implement a bubble sort technique and output the number of swaps required
321 Solvers
More from this Author38
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!