POLYGON(3L)

NAME
polygon: polyf, ptinpoly - polygon routines

SYNOPSIS
#include <dmd.h>

int polyf (bp, poly, np, t, f)

int ptinpoly (pt, poly, np)

Bitmap *bp;
Point poly[ ];
short np;
Texture16 *t;
Code f;
Point pt;

DESCRIPTION
The polyf function is used to fill a closed polygon defined by the np Points in the array of Points poly. The Points are absolute with respect to the Bitmap bp. The polygon is filled with the Texture16 t using Code f. The polyf call returns 0 if the polygon is filled and -1 if a memory allocation error occurred during processing, in which case the polygon is not filled.

The ptinpoly call determines whether the Point pt is contained in the polygon defined by poly and np. The ptinpoly function returns 1 if the Point is inside the polygon, 0 if the Point is not inside the polygon, and -1 if a memory allocation error occurs.

The polygon can consist of an arbitrary number of filled and unfilled regions. For example, a doughnut shape could be formed without filling the portion of the Bitmap corresponding to the hole of the doughnut. This permits a preservation of any background information previously placed in the Bitmap. Each region, filled or unfilled, is delimited by a Point whose x-coordinate has a value of POLY_F (defined in dmd.h). This Point in poly is ignored and merely serves as a flag indicating the start of a new region. There is always an assumed line connecting the first and last Point of each region.

EXAMPLE
The following code permits interactive drawing of a polygon with interior regions, fills it, and then uses ptinpoly to determine if the mouse is inside the polygon. Button 1 is used to draw the points, and button 2 sets a point to POLY_F. After the polygon fills, clicking button 1 with the mouse inside the polygon will ring the terminal bell. Clicking button 3 will exit the drawing and exit the program.

    #include <dmd.h>
    Point npoly[1000];
    main()
    {
    	register int i, j;
    	register char c;
    	request (MOUSE|KBD);
    	i = 0;
    	for (;;)
    	{
    		while (i < 1000){
    			wait (MOUSE);
    			if( button1 () ){
    				npoly[i++] = mouse.xy;
    				point (&display, mouse.xy,
    				    F_STORE);
    				sleep (10);
    				continue;
    			}
    			if( button2 () ){
    				npoly[i++].x = POLY_F;
    				continue;
    			}
    			if( button3 () ) break;
    		}
    		polyf (&display, npoly, i, &T_black,
    		    F_STORE);
    		wait(MOUSE);
    		while( !button3() ){
    			wait (MOUSE);
    			if( button1() && ptinpoly
    			    (mouse.xy, npoly, i) )
    				ringbell();
    		}
    		break;
    	}
    }