0% found this document useful (0 votes)
39 views

Clip Line

The document describes a program for line clipping in C++. It takes in the x and y coordinates of the start and end points of a line, draws the line, and checks if it is inside, outside, or intersecting a clipping window defined by xmin, xmax, ymin, and ymax values. If the line intersects the clipping window, it calls a clip function that calculates the intersection points and redraws the clipped line segments.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views

Clip Line

The document describes a program for line clipping in C++. It takes in the x and y coordinates of the start and end points of a line, draws the line, and checks if it is inside, outside, or intersecting a clipping window defined by xmin, xmax, ymin, and ymax values. If the line intersects the clipping window, it calls a clip function that calculates the intersection points and redraws the clipped line segments.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 5

LINE CLIPPING

#include<conio.h>
#include<math.h>
#include<stdlib.h>
#include<graphics.h>
#include<stdio.h>
#include<export.h>
void clip(float x1,float y1,float x2,float y2);
int xmin=200,ymin=200,xmax=350,ymax=350;
void main()
{
float xw[10],yw[10],x1,x2,y1,y2;
int q=0,w=0,e,t1,y,i;
int ch,gx,gdriver = DETECT, gmode, errorcode;
initgraph(&gdriver,&gmode,"C:\\TC\\BGI");
setcolor(50);
rectangle(200,200,350,350);
printf("enter initial points of the line\n ");
printf(" x1: ");
scanf("%f",&x1);
printf(" y1: ");
scanf("%f",&y1);
printf("enter end points of the line \n ");
printf(" xn: ");
scanf("%f",&x2);
printf(" yn: ");
scanf("%f",&y2);
rectangle(200,200,350,350);
setcolor(255);
line(x1,y1,x2,y2);
delay(500);
cleardevice();
if(x1<=xmin)
q=1;
if(x1>=xmax)
q=2;
if(y1<=ymin)
q=4;
if(y1>=ymax)
q=8;
if(x2<=xmin)
w=1;
if(x2>=xmax)
w=2;
if(y2<=ymin)

w=4;
if(y2>=ymax)
w=8;
if((w&q)==1||(w&q)==2||(w&q)==4||(w&q)==8)
printf("line is completely outside the clip window !!");
else if((w&q)==0&&(w|q)==0)
printf("line is completely inside the clip window !!");
else
clip(x1,y1,x2,y2);
getch();
}
void clip(float x1,float y1,float x2,float y2)
{
float m,x,y;
if(x2!=x1)
{
m=(y2-y1)/(x2-x1);
if(x1<xmin||x1>xmax)
{
if(x1<xmin)
x=xmin;
else
x=xmax;
y=y2+((x-x2)*m);
x1=x;y1=y;
}
else if(x2<xmin||x2>xmax)
{
if(x2<xmin)
x=xmin;
else
x=xmax;
y=y1+((x-x1)*m);
x2=x;y2=y;
}
if(y1<ymin||y1>ymax)
{
if(y1<ymin)
y=ymin;
else
y=ymax;
x=x2+((y-y2)/m);
y1=y;x1=x;
}
else if(y2<ymin||y2>ymax)
{
if(y2<ymin)
y=ymin;

else
y=ymax;
x=x1+((y-y1)/m);
y2=y;x2=x;
}
}
else
{
printf("\n vertical line!!");
if(x1<xmin||x1>xmax)
{
if(x1<xmin)
x1=xmin;
else
x1=xmax;
}
else if(x2<xmin||x2>xmax)
{
if(x2<xmin)
x2=xmin;
else
x2=xmax;
}
if(y1<ymin||y1>ymax)
{
if(y1<ymin)
y1=ymin;
else
y1=ymax;
}
else if(y2<ymin||y2>ymax)
{
if(y2<ymin)
y2=ymin;
else
y2=ymax;
}
}
setcolor(50);
rectangle(200,200,350,350);
setcolor(255);
line(x1,y1,x2,y2);
//SaveBMP("image.jpg");
getch();
}

OUTPUT

You might also like