#include <stdio.h>
#include<conio.h>
#include<iostream.h>
#include <stdlib.h>
#include <graphics.h>
#include<dos.h>
#define MAX 20
typedef struct
{
float x;
float y;
}PT;
int m;//global variable for polygon clipping
/*prototype for sutherland polygon clipping functions*/
void polygon_clipping();
void polygon_info();
void top(PT p1,PT p[20],PT pp[20]);
void bottom (PT p2,PT p[20],PT pp[20]);
void left(PT p1,PT p[20],PT pp[20]);
void right(PT p2,PT p[20],PT pp[20]);
void drawpolygon(PT x[20],int n);
//*prototype of cohensutherland line clipping*/
void line_clipping();
void line_clipping_info();
void cohen_sutherland (double x1, double y1, double x2, double y2,
double xmin, double ymin, double xmax, double ymax);
/*cohen suther land line clipping*/
enum { TOP = 0x1, BOTTOM = 0x2, RIGHT = 0x4, LEFT = 0x8 };
enum { FALSE, TRUE };
typedef unsigned int outcode;
outcode compute_outcode(int x, int y,
int xmin, int ymin, int xmax, int ymax)
{
outcode oc = 0;
if (y > ymax)
oc |= TOP;
else if (y < ymin)
oc |= BOTTOM;
if (x > xmax)
oc |= RIGHT;
else if (x < xmin)
oc |= LEFT;
return oc;
}
/*prototype for Weiler-Atherton Polygon Clipping*/
void weiler_polygon_clipping();
void main()
{
int ch;
int gd = DETECT, gm;
initgraph(&gd,&gm,"C:\\TurboC3\\BGI");
clrscr();
setbkcolor(2);
settextstyle(SMALL_FONT,HORIZ_DIR,6);
outtextxy(100,60,"A PRESENTATION ON CLIPPING TECHNIQUE ");
outtextxy(100,90,"Submitted by Saurabh And Shubham ");
outtextxy(50,130,"Name:Saurabh gupta ");
outtextxy(50,170,"Registration no.:11009096");
outtextxy(50,210,"Rollno:K1003A19");
outtextxy(350,130,"Name:Shubham varshney");
outtextxy(350,170,"Registration no.:11011481");
outtextxy(350,210,"Rollno:K1003A24");
outtextxy(50,350,"submitted to Mr.Gaurav Raj");
outtextxy(50,390,"DEPARTMENT OF COMPUTER SCIENCE");
outtextxy(50,250,"press 1 to start presentation");
settextstyle(SMALL_FONT,HORIZ_DIR,4);
int x;
cin>>x;
if(x==1)
{
label0:
cleardevice();
setbkcolor(3);
settextstyle(SMALL_FONT,HORIZ_DIR,6);
outtextxy(50,30,"[1] cohen suderland line clipping");
outtextxy(50,50,"[2] suderland-hodgeman polygon clipping");
outtextxy(50,70,"[3] wieler-artheton");
outtextxy(50,90,"Enter your choice:");
outtextxy(50,110,"Enter 0 to exit");
settextstyle(SMALL_FONT,HORIZ_DIR,4);
cin>>ch;
clrscr();
switch(ch)
{
case 1:
label1:
cleardevice();
setbkcolor(4); //set background colour
line_clipping_info(); //function of line clipping
line_clipping();
outtextxy(50,50,"press 0 to go back to main menu");
outtextxy(50,60,"press 2 to see polygon clipping");
outtextxy(50,70,"press 3 to see weiler-Atherton Polygon Clipping");
cin>>x;
if(x==0)
goto label0;
if(x==2)
goto label2;
if(x==3)
goto label3;
// break;
case 2:
label2:
cleardevice();
setbkcolor(5);
polygon_info();
outtextxy(50,350,"press 1 to see the example of polygon clipping");
cin>>x;
if(x==1)
polygon_clipping();
outtextxy(50,60,"press 0 to go back to main menu");
outtextxy(50,70,"press 1 to see line clipping");
outtextxy(50,80,"press 3 to see weiler-Atherton Polygon Clipping");
cin>>x;
if(x==0)
goto label0;
if(x==1)
goto label1;
if(x==3)
goto label3;
//break;
case 3:
label3:
cleardevice();
setbkcolor(6);
weiler_polygon_clipping();
outtextxy(50,360,"press 0 to go back to main menu");
outtextxy(50,370,"press 1 to see line clipping");
outtextxy(50,380,"press 2 to see polygon clipping");
cin>>x;
if(x==0)
goto label0;
if(x==1)
goto label1;
if(x==2)
goto label2;
}
}
else
{
cleardevice();
outtextxy(100,100,"wrong choice pressed");
}
cleardevice();
setbkcolor(2);
settextstyle(TRIPLEX_FONT, HORIZ_DIR, 5);
outtextxy(150,150,"THANK YOU");
getch();
closegraph();
}
void cohen_sutherland (double x1, double y1, double x2, double y2,
double xmin, double ymin, double xmax, double ymax)
{
int accept;
int done;
outcode outcode1, outcode2;
accept = FALSE;
done = FALSE;
outcode1 = compute_outcode (x1, y1, xmin, ymin, xmax, ymax);
outcode2 = compute_outcode (x2, y2, xmin, ymin, xmax, ymax);
do
{
if (outcode1 == 0 && outcode2 == 0)
{
accept = TRUE;
done = TRUE;
}
else if (outcode1 & outcode2)
{
done = TRUE;
}
else
{
double x, y;
int outcode_ex = outcode1 ? outcode1 : outcode2;
if (outcode_ex & TOP)
{
x = x1 + (x2 - x1) * (ymax - y1) / (y2 - y1);
y = ymax;
}
else if (outcode_ex & BOTTOM)
{
x = x1 + (x2 - x1) * (ymin - y1) / (y2 - y1);
y = ymin;
}
else if (outcode_ex & RIGHT)
{
y = y1 + (y2 - y1) * (xmax - x1) / (x2 - x1);
x = xmax;
}
else
{
y = y1 + (y2 - y1) * (xmin - x1) / (x2 - x1);
x = xmin;
}
if (outcode_ex == outcode1)
{
x1 = x;
y1 = y;
outcode1 = compute_outcode (x1, y1, xmin, ymin, xmax, ymax);
}
else
{
x2 = x;
y2 = y;
outcode2 = compute_outcode (x2, y2, xmin, ymin, xmax, ymax);
}
}
} while (done == FALSE);
if (accept == TRUE)
line (x1, y1, x2, y2);
}
void left(PT p1,PT p[20],PT pp[20])
{
int i,j=0;
for(i=0;i<m;i++)
{
if(p[i].x < p1.x && p[i+1].x >= p1.x){
if(p[i+1].x-p[i].x!=0)
{
pp[j].y = (p[i+1].y-p[i].y)/(p[i+1].x-p[i].x)* (p1.x-p[i].x)+p[i].y;
}
else
{
pp[j].y = p[i].y;
}
pp[j].x = p1.x;
j++;
pp[j].x=p[i+1].x;
pp[j].y=p[i+1].y;
j++;
}
if(p[i].x > p1.x && p[i+1].x >= p1.x)
{
pp[j].y = p[i+1].y;
pp[j].x = p[i+1].x;
j++;
}
if(p[i].x > p1.x && p[i+1].x <= p1.x)
{
if(p[i+1].x-p[i].x!=0)
{
pp[j].y = (p[i+1].y-p[i].y)/(p[i+1].x-p[i].x)* (p1.x-p[i].x)+p[i].y;
}
else
{
pp[j].y = p[i].y;
}
pp[j].x = p1.x;
j++;
}
}
for(i=0;i<j;i++)
{
p[i].x = pp[i].x;
p[i].y = pp[i].y;
}
p[i].x = pp[0].x;
p[i].y = pp[0].y;
m=j;
}
void right(PT p2,PT p[20],PT pp[20])
{
int i,j=0;
for(i=0;i<m;i++)
{if(p[i].x > p2.x && p[i+1].x <= p2.x)
{
if(p[i+1].x-p[i].x!=0)
{
pp[j].y = (p[i+1].y-p[i].y)/(p[i+1].x-p[i].x)* (p2.x-p[i].x)+p[i].y;
}
else
{
pp[j].y = p[i].y;
}
pp[j].x = p2.x;
j++;
pp[j].x=p[i+1].x;
pp[j].y=p[i+1].y;
j++;
}
if(p[i].x < p2.x && p[i+1].x <= p2.x)
{
pp[j].y = p[i+1].y;
pp[j].x = p[i+1].x;
j++;
}
if(p[i].x < p2.x && p[i+1].x >= p2.x)
{
if(p[i+1].x-p[i].x!=0)
{
pp[j].y = (p[i+1].y-p[i].y)/(p[i+1].x-p[i].x)* (p2.x-p[i].x)+p[i].y;
}
else
{
pp[j].y = p[i].y;
}
pp[j].x = p2.x;
j++;
}
}
for(i=0;i<j;i++)
{
p[i].x = pp[i].x;
p[i].y = pp[i].y;
}
p[i].x = pp[0].x;
p[i].y = pp[0].y;
m=j;
}
void top(PT p1,PT p[20],PT pp[20])
{
int i,j=0;
for(i=0;i<m;i++){
if(p[i].y < p1.y && p[i+1].y >= p1.y)
{
if(p[i+1].y-p[i].y!=0)
{
pp[j].x = (p[i+1].x-p[i].x)/(p[i+1].y-p[i].y)* (p1.y-p[i].y)+p[i].x;
}
else
{
pp[j].x = p[i].x;
}
pp[j].y = p1.y;
j++;
pp[j].x=p[i+1].x;
pp[j].y=p[i+1].y;
j++;
}
if(p[i].y > p1.y && p[i+1].y >= p1.y)
{
pp[j].y = p[i+1].y;
pp[j].x = p[i+1].x;
j++;
}
if(p[i].y > p1.y && p[i+1].y <= p1.y)
{
if(p[i+1].y-p[i].y!=0)
{
pp[j].x = (p[i+1].x-p[i].x)/(p[i+1].y-p[i].y)* (p1.y-p[i].y)+p[i].x;
}
else
{
pp[j].x = p[i].x;
}
pp[j].y = p1.y;
j++;
}
}
for(i=0;i<j;i++)
{
p[i].x = pp[i].x;
p[i].y = pp[i].y;
}
p[i].x = pp[0].x;
p[i].y = pp[0].y;
m=j;
}
void bottom(PT p2,PT p[20],PT pp[20])
{
int i,j=0;for(i=0;i<m;i++)
{
if(p[i].y > p2.y && p[i+1].y <= p2.y)
{
if(p[i+1].y-p[i].y!=0)
{
pp[j].x = (p[i+1].x-p[i].x)/(p[i+1].y-p[i].y)* (p2.y-p[i].y)+p[i].x;
}
else
{
pp[j].x = p[i].x;
}
pp[j].y = p2.y;
j++;
pp[j].x=p[i+1].x;
pp[j].y=p[i+1].y;
j++;
}
if(p[i].y < p2.y && p[i+1].y <= p2.y)
{
pp[j].y = p[i+1].y;
pp[j].x = p[i+1].x;
j++;
}
if(p[i].y < p2.y && p[i+1].y >= p2.y)
{
if(p[i+1].y-p[i].y!=0)
{
pp[j].x = (p[i+1].x-p[i].x)/(p[i+1].y-p[i].y)* (p2.y-p[i].y)+p[i].x;
}
else
{
pp[j].x = p[i].x;
}
pp[j].y = p2.y;
j++;
}
}
for(i=0;i<j;i++)
{
p[i].x = pp[i].x;
p[i].y = pp[i].y;
}
p[i].x = pp[0].x;
p[i].y = pp[0].y;
m=j;
}
void drawpolygon(PT x[20],int m)
{
int i;
for(i=0;i<m-1;i++)
{
line(x[i].x,x[i].y,x[i+1].x,x[i+1].y);
}
line(x[i].x,x[i].y,x[0].x,x[0].y);
}
void line_clipping()
{
int n=3;
int i, j;
int ln[MAX][4];
int clip[4];
outtextxy(370,300,"press enter to see demonstration");
ln[0][0]=30+300;
ln[0][1]=70+300;
ln[0][2]=60+300;
ln[0][3]=70+300;
ln[1][0]=30+300;
ln[1][1]=90+300;
ln[1][2]=60+300;
ln[1][3]=30+300;
ln[2][0]=50+300;
ln[2][1]=100+300;
ln[2][2]=90+300;
ln[2][3]=60+300;
/*for (i=0; i<n; i++)
for (j=0; j<4; j++)
scanf ("%d", &ln[i][j]);
printf ("Enter the x- and y-coordinates of the left-top and right-");
printf ("bottom corners\nof the clip window:\n");
for (i=0; i<4; i++)
scanf ("%d", &clip[i]); */
clip[0]=40+300;
clip[1]=50+300;
clip[2]=80+300;
clip[3]=90+300;
// cleardevice();
rectangle (clip[0], clip[1], clip[2], clip[3]);
for (i=0; i<n; i++)
line (ln[i][0], ln[i][1], ln[i][2], ln[i][3]);
getch();
cleardevice();
rectangle (clip[0], clip[1], clip[2], clip[3]);
for (i=0; i<n; i++)
{
cohen_sutherland (ln[i][0], ln[i][1], ln[i][2], ln[i][3],
clip[0], clip[1], clip[2], clip[3]);
getch();
}
getch();
// closegraph();
}
void polygon_clipping()
{
PT d,p1,p2,p[20],pi1,pi2,pp[20];
m=8;
p1.x=100;
p1.y=100;
p2.x=200;
p2.y=200;
p[0].x=150;
p[0].y=80;
p[1].x=170;
p[1].y=130;
p[2].x=220;
p[2].y=150;
p[3].x=170;
p[3].y=170;
p[4].x=150;
p[4].y=220;
p[5].x=130;
p[5].y=170;
p[6].x=80;
p[6].y=150;
p[7].x=130;
p[7].y=130;
p[8].x = p[0].x;
p[8].y = p[0].y;
cleardevice();
outtextxy(50,30,"before clipping the window is like this");
drawpolygon(p,m);
rectangle(p1.x,p1.y,p2.x,p2.y);
getch();
left(p1,p,pp);
right(p2,p,pp);
top(p1,p,pp);
bottom(p2,p,pp);
cleardevice();
outtextxy(50,30,"after clipping window is clipped like this");
rectangle(p1.x,p1.y,p2.x,p2.y);
drawpolygon(p,m);
getch();
//closegraph();
}
void line_clipping_info()
{
cleardevice();
setbkcolor(4);
outtextxy(50,30,"Cohen-Sutherland Line Clipping");
outtextxy(250,30,".......");
delay(1000);
outtextxy(50,60,"This is one of the oldest and most popular line-clipping procedure.");
outtextxy(50,70,"Generally,the method speed up the processing of the line segments by");
outtextxy(50,80,"performing initial tests that reduce the number of intersections that");
outtextxy(50,90,"must be calculated.Every line end point in a picture is assigned");
outtextxy(50,100,"a four-digit binary code,called a region,that identifies the location");
outtextxy(50,110,"of the point realtive to the boundaries as shown in fig.");
outtextxy(50,140,"bit 1: Left");
outtextxy(50,150,"bit 2: Right");
outtextxy(50,160,"bit 3: Below");
outtextxy(50,170,"bit 4: Above");
rectangle(50,190,200,300);
delay(1000);
line(100,190,100,300);
delay(1000);
line(150,190,150,300);
delay(1000);
line(50,230,200,230);
delay(1000);
line(50,265,200,265);
outtextxy(60,200,"1001");
delay(1000);
outtextxy(110,200,"1000");
delay(1000);
outtextxy(160,200,"1010");
delay(1000);
outtextxy(60,240,"0001");
delay(1000);
outtextxy(110,240,"0000");
delay(1000);
outtextxy(160,240,"0010");
delay(1000);
outtextxy(60,275,"0101");
delay(1000);
outtextxy(110,275,"0100");
delay(1000);
outtextxy(160,275,"0110");
delay(1000);
outtextxy(101,250,"window");
delay(1000);
outtextxy(300,300,"Example:");
delay(1000);
}
void polygon_info()
{
cleardevice();
outtextxy(50,30,"Sytherland-Hodgeman Polygon Clipping");
outtextxy(250,30,".........");
delay(1000);
outtextxy(50,60,"There are possible four cases when procesing vertices in sequence");
outtextxy(50,70,"around the perimeter of a polygon.As each pair of adjacent polygon");
outtextxy(50,80,"vertices are passed to a window boundary clipper,we make the following");
outtextxy(50,90,"[1] if the first vertext is outside the window boundary and the second ");
outtextxy(50,100," vertex is inside, both the intersection point of the polygon edge");
outtextxy(50,110," with the window boundary and second vertex is added to the output");
outtextxy(50,120,"[2] if both input vertices are inside the window boundary.");
outtextxy(50,130," only the second vertex is added to the output vertex list");
outtextxy(50,140,"[3] if the first vetex is inside the window boundary and the second");
outtextxy(50,150," vertex is outside only the edge intersection with the window boundary is");
outtextxy(50,160," added to the output list");
outtextxy(50,170,"[4] if both input vertices are outside the window boundary, nothing is");
outtextxy(50,180," addded to the output list.");
delay(500);
rectangle(50,200,100,300);
delay(1250);
line(30,250,80,250);
delay(1100);
outtextxy(20,257,"v1");
outtextxy(85,257,"v2");
outtextxy(50,253,"v1'");
outtextxy(50,310,"out-in");
outtextxy(50,320,"save v1',v2");
rectangle(150,200,200,300);
delay(1250);
line(170,220,180,280);
delay(1100);
outtextxy(175,220,"v1");
outtextxy(160,280,"v2");
outtextxy(150,310,"in-in");
outtextxy(150,320,"save v2");
rectangle(250,200,300,300);
delay(1250);
line(230,280,280,250);
delay(1100);
outtextxy(230,280,"v2");
outtextxy(280,250,"v1");
outtextxy(240,260,"v1'");
outtextxy(250,310,"in-out");
outtextxy(250,320,"save v1'");
rectangle(350,200,400,300);
delay(1250);
line(330,210,330,280);
delay(1100);
outtextxy(320,210,"v1");
outtextxy(320,280,"v2");
outtextxy(350,310,"out-out");
outtextxy(350,320,"save none");
}
void weiler_polygon_clipping()
{
outtextxy(50,30,"Weiler-Atherton Polygon Clipping");
outtextxy(50,60,"In this clipping method vertex processing procedures");
outtextxy(50,70,"for window boundaries are modified so that concave polygons");
outtextxy(50,80,"are displayed correctly.The basic idea int this algorithm");
outtextxy(50,90,"is that instead of always proceeding around the polygon edges");
outtextxy(50,100,"as vertices are processed,we sometimes want to follow the window");
outtextxy(50,110,"boundaries.Which path follow depends on the polygon-processing");
outtextxy(50,120,"direction (clockwise or counter clockwise) and whether the pair");
outtextxy(50,130,"of polygon vertices currently being processed represents an");
outtextxy(50,140,"outside-to-inside or an inside-to-outside pair.");
outtextxy(50,170,"For clockwise direction processing of polygon vertices, we use");
outtextxy(50,180,"following rules.");
outtextxy(50,190,"[1] For an outside-to-inside pair of vertices,follow the polygon");
outtextxy(50,200," boundary.");
outtextxy(50,210,"[2] For an inside-to-outside pair of vertices,follow the window");
outtextxy(50,220," boundary in a clockwise direction.");
rectangle(70,240,180,360);
delay(1100);
line(30,310,110,270);
delay(1100);
line(110,270,100,295);
delay(1100);
line(100,295,50,330);
delay(1100);
line(50,330,110,340);
delay(1100);
line(110,340,30,350);
delay(1100);
line(30,310,30,350);
delay(1100);
outtextxy(20,310,"v1");
delay(1100);
outtextxy(110,270,"v2");
delay(1100);
outtextxy(105,295,"v3");
delay(1100);
outtextxy(45,330,"v4");
delay(1100);
outtextxy(115,340,"v5");
delay(1100);
outtextxy(20,350,"v6");
delay(1100);
outtextxy(65,285,"v1'");
delay(1100);
outtextxy(65,305,"v3'");
delay(1100);
outtextxy(75,325,"v4'");
delay(1100);
outtextxy(50,350,"v5'");
outtextxy(50,250,"press 1 to start presentation");
cleardevice();
rectangle(70,240,180,360);
setcolor(11);
line(70,290,110,270);
line(110,270,100,295);
line(100,295,70,320);
line(70,290,70,320);
delay(2000);
line(70,330,110,340);
line(70,330,110,340);
line(110,340,70,350);
line(70,330,70,350);
setcolor(15);
}
clipping path service which includes photoshop masking, masked pictures, photo background removal and many more.....
ReplyDelete