Listing 2. testpoly.c

/********************************************
 *                                           *
 * TESTPOLY.C                                *
 * (http://www.gcomm.com/develop/testpoly.c) *
 *                                           *
 * Copyright (C) 1995 GALACTICOMM, Inc.      *
 * Freeware source code.                     *
 *                                           *
 * Please feel free to use this source code  *
 * for any purpose, commercial or otherwise, *
 * as long as you don't restrict anyone      *
 * else's use of this source code.           *
 * Please give credit where credit is due.   *
 *                                           *
 * Test program for INPOLY.C,                *
 * point-in-polygon algorithm.           *
 *                                           *
 *                                           *
 *             7/14/95 - Bob Stein           *
 *                                           *
 ********************************************/

#include "stdlib.h"
#include "stdio.h"
#include "conio.h"
#include "graphics.h"
#include "inpoly.h"

#define NPOINTS 40

unsigned int plgn[NPOINTS][2];

int
main(void)
{
     int gdriver=DETECT,gmode,errorcode;
     int maxx,maxy;
     int pnt,other;
     int ix,iy;

     initgraph(&gdriver,&gmode,"");
     if ((errorcode=graphresult()) != grOk) {
          printf("error=%d\n",errorcode);
          getch();
          return 1;
     }
     maxx=getmaxx();
     maxy=getmaxy();
     do {
          cleardevice();
          for (pnt=0 ; pnt < NPOINTS ; pnt++) {
               plgn[pnt][0]=rand()%maxx;
               plgn[pnt][1]=rand()%maxy;
          }
          other=NPOINTS-1;
          for (pnt=0 ; pnt < NPOINTS ; pnt++) {
               line(plgn[pnt][0],plgn[pnt][1],
               plgn [other][0],plgn[other][1]);
               other=pnt;
          }
          while (!kbhit()) {
               ix=rand()%maxx;
               iy=rand()%maxy;
               putpixel(ix,iy,inpoly(plgn,NPOINTS,ix,iy)
               ? LIGHTRED : CYAN);
               setcolor(WHITE);
          }
     } while (getch() != 0x1B);
     closegraph();
     return 0;