typedef unsigned int outcode; enum { TOP=0x1, BOTTOM=0x2, RIGHT=0x4, LEFT=0x8 }; void CohenSutherlandLineClipAndDraw( double x0, double y0, double x1, double y1, double xmin, double xmax, double ymin, duble ymax, int value) /** Cohen-Sutherland clipping algorithm for line P0=(x0, y0) to P1 = (x1, y1) and clip rectangle with diagonal from (xmin, ymin) to (xmax, ymax) **/ { /** Outcode for P0, P1, and whatever point lies outside the clip rectabgle **/ outcode outcode0, outcode1, outcodeOut; boolean accept=FALSE, done=FALSE; outcode0 = CompOutCode( x0, y0, xmin, ymin, xmax, ymax ); outcode1 = CompOutCode( x1, y1, xmin, ymin, xmax, ymax ); do { if( !(outcode0|outcode1) ) { /* Trivial accept and exit */ accept=TRUE; done=TRUE; } else if( outcode0&outcode1) /* Logical AND is true, so trivial */ done = TRUE; /* reject and exit */ else { /* Failed both tests, so calculate the line segment to clip from an outside point to an intersection with clip edge */ double x, y; /* At least one endpoint is outside the clip rectangle; pick it */ outcodeOut = outcode0 ? outcode0 : outcode1; /* Now find the intersecion point */ if ( outcodeOut & TOP ) { x = x0 + (x1-x0)*(ymax - y0)/(y1 - y0); y = ymax; } else if (outcodeOut & BOTTOM) { x = x0 + (x1-x0)*(ymin - y0)/(y1 - y0); y = ymin; } else if (outcodeOut & RIGHT) { y = y0 + (y1-y0)*(xmax - x0)/(x1 - x0); x = xmax; } else { y = y0 + (y1-y0)*(xmin - x0)/(x1 - x0); x = xmin; } if ( outcodeOut == outcode0 ) { x0 = x; y0 = y; outcode0 = CompOutCode( x0, y0, xmin,xmax,ymin,ymax); } else { x1 = x; y1 = y; outcode1 = CompOutCode( x1, y1, xmin,xmax,ymin,ymax); } } } while( done==FALSE ); if ( accept ) { /** We draw the line **/ /** Function version for double coordinates **/ MidPointLineReal( x0, y0, x1, y1, value ); } } outcode CompOutCode ( double x, double y, double xmin, double xmax, double ymin, double ymax ) { outcode code=0; if ( y > ymax ) code |= TOP; else if( y < ymin ) code |= BOTTOM; if ( x > xmax ) code |= RIGHT; else if ( x < xmin ) code |= LEFT; return code; }