corkum@csri.toronto.edu (Brent Thomas Corkum) (10/27/90)
As very many people have pointed out, the code I posted for determining whether a 2D polygon is clockwise or counter-clockwise was about 155 lines of code to many. I humbly acknowledge this and just wanted to post a modified version. I also want to thank the many peoply who pointed this out. Brent This routine and sample program computes whether a 2D polygon is clockwise or counter-clockwise. The function takes vertices passed in an array (x1,y1,x2,y2,...xn,yn where n=#vertices) and the number of vertices. Of course for a 3D polygon, clockwise or counter-clockwise depends on which way you look at it. Brent Corkum corkum@boulder.civ.toronto.edu This function is also available via anonymous ftp at boulder.civ.toronto.edu (128.100.14.11) int the pub/CCW_OR_CW directory. P.S. Feel free to use and distribute this function. /*---------------------------cut here--------------------------------*/ #include <stdio.h> main() { float pl[100]; int numv,i,flag; printf("Enter #numvertices: "); scanf("%d",&numv); for(i=1;i<=numv;++i){ printf("Enter Vertex %d: ",i); scanf("%f%f",&pl[2*(i-1)],&pl[2*(i-1)+1]); } flag=Isclockwise(pl,numv); if(flag) printf("polygon is clockwise\n"); else printf("polygon is counter-clockwise\n"); } /* Function by : Brent Corkum Data Visualization Lab Civil Engineering University of Toronto Toronto Ontario Canada corkum@csri.toronto.edu or corkum@boulder.civ.toronto.edu (128.100.14.11) That determines whether a 2d polygons vertices are orderred clockwise or counter-clockwise. The polygon is passed as an array of vertices (x1,y1,x2,y2,...,xn,yn where n=numv=#vertices or edges in the closed polygon). The function returns 1 if the polygon is clockwise and 0 if the polygon is counterclockwise. */ int Isclockwise(pl,numv) float *pl; int numv; { int i,j; float nz; nz = 0.0; for(i=0;i<numv;++i){ j = (i+1)%numv; nz += (pl[2*i]-pl[2*j])*(pl[2*i+1]+pl[2*j+1]); } if(nz>0.0)return(0); else return(1); }