
Program to implement Cohen-Sutherland Line clipping algorithm. -MASTER GUIDE
Program to implement Cohen-Sutherland Line clipping algorithm.
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
unsigned int computecode(float x, float y, float xmin, float ymin, float xmax, float ymax);
void clipping(float x1, float y1, float x2, float y2, float xmin,float ymin, float xmax, float ymax);
enum{TOP=0x8, BOTTOM=0x4, RIGHT=0x2, LEFT=0x1};
enum{FALSE,TRUE};
unsigned int computecode(float x, float y, float xmin, float ymin, float xmax, float ymax)
{
unsigned int code=0;
if(y>ymax)
code= code | TOP;
else if(y<ymin)
code= code | BOTTOM;
if(x>xmax)
code= code | RIGHT;
else if (x<xmin)
code =code | LEFT;
return code;
}
void clipping(float x1, float y1, float x2, float y2, float xmin,float ymin, float xmax, float ymax)
{
unsigned int code1, code2, outcode;
float x,y;
int accept=FALSE, done=FALSE;
code1=computecode(x1,y1,xmin,ymin,xmax,ymax);
code2=computecode(x2,y2,xmin,ymin,xmax,ymax);
do
{
if(code1 == 0 & code2== 0)
{
accept=TRUE;
done=TRUE;
}
else if (code1 & code2)
done=TRUE;
else
{
if(code1!=0)
outcode =code1;
else
outcode=code2;
if(outcode & TOP)
{
x=x1+(ymax-y1) * (x2-x1) / (y2-y1);
y=ymax;
}
else if(outcode & BOTTOM)
{
x=x1+ (ymin-y1) * (x2-x1) / (y2-y1);
y=ymin;
}
else if(outcode & RIGHT)
{
y=y1+ (xmax-x1) * (y2-y1) / (x2-x1);
x=xmax;
}
else
{
y=y1+ (xmin-x1) * (y2-y1) / (x2-x1);
x=xmin;
}
if(outcode==code1)
{
x1=x;
y1=y;
code1=computecode(x1,y1,xmin,ymin,xmax,ymax);
}
else
{
x2=x;
y2=y;
code2=computecode(x2,y2,xmin,ymin,xmax,ymax);
}
}
}while(done==FALSE);
clrscr();
cleardevice();
printf("After clipping \n");
if(accept)
{
setcolor(BLUE);
rectangle(xmin,ymin,xmax,ymax);
line(x1,y1,x2,y2);
}
else
{
rectangle(xmin,ymin,xmax,ymax);
printf("Line is invisble");
}
}
void main()
{
int gd=DETECT,gm;
float xmax , ymax , xmin , ymin;
float x1,y1,x2,y2;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
printf("enter the clip window coordinates(xmin,ymin,xmax,ymax):");
scanf("%f%f%f%f",&xmin,&ymin,&xmax,&ymax);
printf("\n enter line endpoints (x1,y1,x2,y2):");
scanf("%f%f%f%f",&x1,&y1,&x2,&y2);
clrscr();
cleardevice();
printf("Before Clipping");
setcolor(RED);
rectangle(xmin,ymin,xmax,ymax);
line(x1,y1,x2,y2);
getch();
clipping(x1,y1,x2,y2,xmin,ymin,xmax,ymax);
getch();
closegraph();
}
OUTPUT:
Baca Juga
- Program to implement DDA Line generation algorithm for all 4 quadrants of a plane (x and y axis should meet at centre(320,240) i.e., Origin O(0,0) of user coordinate system)-MASTER GUIDE
- Program to transform object ( Tringle or rectangle or Polygon) from window to view port. - MASTER GUIDE
- Non Numerically Indexed Array
0 Response to "Program to implement Cohen-Sutherland Line clipping algorithm. -MASTER GUIDE"
Post a Comment