/* Cohen-Sutherland-Algorithmus zum Kappen von Linien */
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <graphics.h>
typedef struct OUTCODE outcode;
struct OUTCODE
{ 
unsigned all;     // ist unsigned int
unsigned left:4;
unsigned right:4;
unsigned bottom:4;
unsigned top:4;
};
void CohenSutherlandLineClipAndDraw(double x0,double y0,double x1,double y1,\
	       double xmin,double xmax,double ymin,double ymax, int value) ;
outcode CompOutCode(double x, double y,double xmin, double xmax,\
		    double ymin,double ymax);
void main()
{
int graphdriver,graphmode;
double xmin,ymin,xmax,ymax;
double x0,y0,x1,y1;
int xi0,yi0,xi1,yi1,back,color;
graphdriver=DETECT;
back=0; // Hintergrundfarbe
printf("Bitte xmin,ymin,xmax,ymax des Kappungsfensters:");
scanf("%lf %lf %lf %lf",&xmin,&ymin,&xmax,&ymax);
printf("Bitte x0,y0,x1,y1 der Linie :");
scanf("%lf %lf %lf %lf",&x0,&y0,&x1,&y1);
initgraph(&graphdriver,&graphmode,"C:\\TC\\BGI");
setcolor(10);  // Zeichenfarbe
setbkcolor(back);
/* Kappungsfenster zeichnen */
xi0= (int)xmin;
yi0=(int)ymin;
xi1=(int)xmax;
yi1=(int) ymax;
line(xi0,yi0,xi1,yi0);
line(xi1,yi0,xi1,yi1);
line(xi1,yi1,xi0,yi1);
line(xi0,yi1,xi0,yi0);
getchar();
/* Linie zunchst ungekappt zeichnen */
xi0=(int) x0;
yi0= (int) y0;
xi1= (int) x1;
yi1= (int) y1;
setcolor(12);
line(xi0,yi0,xi1,yi1);
getchar();
getchar();
/* Linie wieder lschen */
setcolor(back);
line(xi0,yi0,xi1,yi1);
getchar();
/* Gekappte Linie zeichnen */
color=14;;
CohenSutherlandLineClipAndDraw(x0,y0,x1,y1,xmin,xmax,ymin,ymax,color);
getchar();
closegraph();
}
/**************************************************************************/
/* Kappungsalgorithmus nach Cohen Sutherland fr die Linie von P0=(x0,y0) */ 
/* nach P1(x1,y1) gekappt am Fenster(xmin,ymin,xmyx,ymax)                 */
void CohenSutherlandLineClipAndDraw(double x0,double y0,double x1,double y1,\
	       double xmin,double xmax,double ymin,double ymax, int value) 
{
typedef unsigned boolean;
boolean TRUE,FALSE;

boolean accept,done;
outcode outcode0,outcode1,outcodeOut;
double x,y;
int x0i,y0i,x1i,y1i;
TRUE=1; FALSE=0;
accept=FALSE;
done=FALSE;
/* Prfung, in welchen Bereichen die Endpunkte der Linie liegen */
/* Vergabe eines Endpunktcodes aus 4 Bit  
   0000 fr im Kappungsfenster liegender Punkt*/
outcode0=CompOutCode(x0,y0,xmin,xmax,ymin,ymax); // fr Punkt P0
outcode1=CompOutCode(x1,y1,xmin,xmax,ymin,ymax); // fr Punkt P1
/* Prfung des Endpunktcodes und notfalls Unterteilung der Linie in
 Teil der im Fenster liegt und Teil, der gekappt wird */
do
  { 
  if ((outcode0.all==0) && (outcode1.all==0))
	{ accept=TRUE;
	  done=TRUE;   
	}
	/* logischer Schnittpunkt,also trivialerweise ablehnen und beenden */
	else if ((outcode0.all !=0) && (outcode1.all !=0)) done=TRUE; 
	else
	  { if (outcode0.all !=0) outcodeOut=outcode0;
	     else outcodeOut=outcode1;
	    if (outcodeOut.top)  /* Linie an der Oberkante des Rechteckes unterteilen*/
	   { x=x0+(x1-x0) *(ymax-y0)/(y1-y0);
	    y=ymax;
	   }
	   else if (outcodeOut.bottom) /* Linie an der Unterkante des Rechteckes unterteilen*/
	    { x=x0+ (x1-x0) * (ymin-y0)/(y1-y0);
	      y=ymin;
	    }
	   else if (outcodeOut.right) /* Linie an der rechten Kante unterteilen */
	   { y=y0 + (y1-y0) * (xmax-x0)/(x1-x0);
	     x=xmax;
	   }
	   else if (outcodeOut.left) /* Linie an der linken Kante unterteilen */
	   { y= y0 +( y1-y0) * (xmin-x0) /(x1-x0);   
	     x=xmin;
	   }
	   if (outcodeOut.all==outcode0.all)
	       { x0=x; y0=y; outcode0=CompOutCode(x0,y0,xmin,xmax,ymin,ymax);}
	    else {x1=x; y1=y;outcode1=CompOutCode(x1,y1,xmin,ymin,xmax,ymax);}
     } /* Unterteilung */
     } while (!done);
     if (accept) { setcolor(value); 
		  x0i=(int) x0;
		  y0i=(int) y0;
		  x1i=(int) x1;
		  y1i=(int) y1;
		  line (x0i,y0i,x1i,y1i); };
     }



outcode CompOutCode(double x, double y,double xmin, double xmax,\
		    double ymin,double ymax)
/* Pruefen, in welchem Bereich ein Endpunkt einer Strecke liegt */
  {
  outcode code;
  code.top=0,code.bottom=0,code.right=0,code.left=0,code.all=0;
  if ( y>ymax) { code.top=8;
		 code.all +=code.top;
	       }
   else if (y<ymin) { code.bottom=4;
		      code.all +=code.bottom;
		    }
   if (x >xmax) { code.right=2; code.all +=code.right;}
     else if (x<xmin) { code.left=1; code.all += code.left; }
    
    return code;
    }

