Debian Codegraph @

benchmarks.larceny/src/pnpoly.c

raw
1 /* PNPOLY - Test if a point is contained in a 2D polygon. */
2
3 #include <stdio.h>
4
5 /*
6 The code below is from Wm. Randolph Franklin <[email protected]>
7 with some minor modifications for speed and style.
8 comp.graphics.algorithms FAQ
9 References:
10 [Gems IV] pp. 24-46
11 [O'Rourke] pp. 233-238
12 [Glassner:RayTracing]
13 */
14
15 static int pnpoly(int npol, double *xp, double *yp, double x, double y)
16 {
17 int i, j, c = 0;
18 for (i = 0, j = npol-1; i < npol; j = i++)
19 {
20 if ((((yp[i]<=y) && (y<yp[j])) ||
21 ((yp[j]<=y) && (y<yp[i]))) &&
22 (x < (xp[j] - xp[i]) * (y - yp[i]) / (yp[j] - yp[i]) + xp[i]))
23 c = !c;
24 }
25 return c;
26 }
27
28 static double xp[20] = {0.0,1.0,1.0,0.0,0.0,1.0,-.5,-1.0,-1.0,-2.0,
29 -2.5,-2.0,-1.5,-.5,1.0,1.0,0.0,-.5,-1.0,-.5};
30 static double yp[20] = {0.0,0.0,1.0,1.0,2.0,3.0,2.0,3.0,0.0,-.5,
31 -1.0,-1.5,-2.0,-2.0,-1.5,-1.0,-.5,-1.0,-1.0,-.5};
32
33 static int run ()
34 {
35 int npol=20, count=0;
36 if (pnpoly(npol,xp,yp,0.5,0.5)) count++;
37 if (pnpoly(npol,xp,yp,0.5,1.5)) count++;
38 if (pnpoly(npol,xp,yp,-.5,1.5)) count++;
39 if (pnpoly(npol,xp,yp,0.75,2.25)) count++;
40 if (pnpoly(npol,xp,yp,0,2.01)) count++;
41 if (pnpoly(npol,xp,yp,-.5,2.5)) count++;
42 if (pnpoly(npol,xp,yp,-1.0,-.5)) count++;
43 if (pnpoly(npol,xp,yp,-1.5,.5)) count++;
44 if (pnpoly(npol,xp,yp,-2.25,-1.0)) count++;
45 if (pnpoly(npol,xp,yp,0.5,-.25)) count++;
46 if (pnpoly(npol,xp,yp,0.5,-1.25)) count++;
47 if (pnpoly(npol,xp,yp,-.5,-2.5)) count++;
48 return count;
49 }
50
51 int main (int argc, char *argv[])
52 {
53 int i;
54 int result;
55
56 for (i=0; i<100000; i++)
57 result = run ();
58
59 if (result != 6)
60 printf ("*** wrong result ***\n");
61
62 return 0;
63 }