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;
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.
I don't understand why the vector sizes of x and y for the last test case are both 100x15. The problem strictly says that the points are 2-dimensional. Can someone explain this?
I like the readability of this submission. However the logic of the if statement is curious to me: it seems that after setting tf to false, tf can be reset back to true. Would it be more robust to either (i) initialise tf before your loop, or (ii) include a "break" command?
Simply beautiful!
Didn't realize ispolycw was in the Mapping Toolbox, and not Cody friendly.
This method (as well as several others) would fail in a more extensive test suite; for example, when x = [1,1,2] and y = [2,1,1]; or when x = [-2,-2,-4,-4] and y = [1,3,1,-1]; or when x = [-1,-1,3] and y = [-3,1,1].
@yurenchu, Thanks for pointing this out, I have added a few more test cases now
5613 Solvers
1097 Solvers
Determine if a Given Number is a Triangle Number
286 Solvers
Given a window, how many subsets of a vector sum positive
670 Solvers
167 Solvers