The winding number is the integer number of net counterclockwise revolutions an observer must have made to follow a given closed path. Note that any clockwise revolutions count negative towards the winding number. The path is allowed to self intersect.
Some examples (shamelessly taken from Wikipedia) are given below:
Your goal is to compute the winding number for a given path.
Input
The observer is assumed be at the origin (0,0)
.
The input is a finite sequence of points (pair-like of integer numbers) from any desired input source which describes the piece-wise linear path. You may flatten this into a 1D sequence of integer numbers if desired, and may also swizzle the input to take all x coordinates before all y coordinates/vise-versa. You may also take the input as a complex number a+b i
. The path may self intersect and may contain zero-length segments. The first point is the start of the path and is assumed to lie somewhere on the positive x axis.
No part of the path will intersect the origin. The path will always be closed (i.e. the first and lost point are the same). Your code may either imply the last point or require it to be included.
For example, depending on your preference both inputs specify the same square:
implied end point
1,0
1,1
-1,1
-1,-1
1,-1
explicit end point
1,0
1,1
-1,1
-1,-1
1,-1
1,0
Output
The output is a single integer for the winding number. This may be to any source (return value, stdout, file, etc.).
Examples
All examples have the end point explicitly defined and are given as x,y pairs. Incidentally, you should be able to also directly feed these examples into any codes assuming implicitly defined end points and the outputs should be the same.
1. Basic test
1,0
1,1
-1,1
-1,-1
1,-1
1,0
Output
1
2. Repeated point test
1,0
1,0
1,1
1,1
-1,1
-1,1
-1,-1
-1,-1
1,-1
1,-1
1,0
Output
1
3. Clockwise test
1,0
1,-1
-1,-1
-1,1
1,1
1,0
Output
-1
4. Outside test
1,0
1,1
2,1
1,0
Output
0
5. Mixed winding
1,0
1,1
-1,1
-1,-1
1,-1
1,0
1,-1
-1,-1
-1,1
1,1
1,0
1,1
-1,1
-1,-1
1,-1
1,0
1,1
-1,1
-1,-1
1,-1
1,0
Output
2
Scoring
This is code golf; shortest code wins. Standard loopholes apply. You may use any builtin functions so long as they were not specifically designed to compute the winding number.
"1-i"
or"1-1i"
?) \$\endgroup\$