Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.
C function by Paul Bourke
/*
Return the clockwise status of a curve, clockwise or counterclockwise
n vertices making up curve p
return 0 for incomputables eg: colinear points
CLOCKWISE == 1
COUNTERCLOCKWISE == -1
It is assumed that
- the polygon is closed
- the last point is not repeated.
- the polygon is simple (does not intersect itself or have holes)
*/
int ClockWise(XY *p,int n)
{
int i,j,k;
int count = 0;
double z;
if (n < 3)
return(0);
for (i=0;i<n;i++) {
j = (i + 1) % n;
k = (i + 2) % n;
z = (p[j].x - p[i].x) * (p[k].y - p[j].y);
z -= (p[j].y - p[i].y) * (p[k].x - p[j].x);
if (z < 0)
count--;
else if (z > 0)
count++;
}
if (count > 0)
return(COUNTERCLOCKWISE);
else if (count < 0)
return(CLOCKWISE);
else
return(0);
}
But I don't remember much C
I have tried to rewrite to C#:
private static int ClockWise(double[] y, double[]x, int n)
{
int i,j,k;
int count = 0;
double z;
if (n < 3)//not a polygon
return(0);
for (i=0;i<n-1;i++)
{
j = (i + 1) % n;
k = (i + 2) % n;
z = (x[j] - x[i]) * (y[k] - y[j]);
z -= (y[j] - y[i]) * (x[k] - x[j]);
if (z < 0)
count--;
else if (z > 0)
count++;
}
if (count > 0)//counterclockwise
return(1);
else if (count < 0)//clockwise
return(-1);
else
return(0);
}
class Point
{
public double x;
public double y;
}
private int ClockWise (List<Point> p, int n)
{
int i, j, k;
int count = 0;
double z;
if (n < 3)
return (0);
for (i = 0; i < n; i++)
{
j = (i + 1) % n;
k = (i + 2) % n;
z = (p[j].x - p[i].x) * (p[k].y - p[j].y);
z -= (p[j].y - p[i].y) * (p[k].x - p[j].x);
if (z < 0)
count--;
else if (z > 0)
count++;
}
if (count > 0)
return (-1);
else if (count < 0)
return (1);
else
return (0);
}
If you are experiencing a similar issue, please ask a related question
Join the community of 500,000 technology professionals and ask your questions.