write a program to implement liang bersky line clipping algorithm

Solutions on MaxInterview for write a program to implement liang bersky line clipping algorithm by the best coders in the world

showing results for - "write a program to implement liang bersky line clipping algorithm"
Noam
14 Jan 2017
1#include<iostream.h>
2#include<graphics.h>
3#include<math.h>
4#include<dos.h>
5 
6void main()
7{
8	int i,gd=DETECT,gm;
9	int x1,y1,x2,y2,xmin,xmax,ymin,ymax,xx1,xx2,yy1,yy2,dx,dy;
10	float t1,t2,p[4],q[4],temp;
11	
12	x1=120;
13	y1=120;
14	x2=300;
15	y2=300;
16	
17	xmin=100;
18	ymin=100;
19	xmax=250;
20	ymax=250;
21	
22	initgraph(&gd,&gm,"c:\\turboc3\\bgi");
23	rectangle(xmin,ymin,xmax,ymax);
24	dx=x2-x1;
25	dy=y2-y1;
26	
27	p[0]=-dx;
28	p[1]=dx;
29	p[2]=-dy;
30	p[3]=dy;
31	
32	q[0]=x1-xmin;
33	q[1]=xmax-x1;
34	q[2]=y1-ymin;
35	q[3]=ymax-y1;
36	
37	for(i=0;i<4;i++)
38	{
39		if(p[i]==0)
40		{
41			cout<<"line is parallel to one of the clipping boundary";
42			if(q[i]>=0)
43			{
44				if(i<2)
45				{
46					if(y1<ymin)
47					{
48						y1=ymin;
49					}
50				
51					if(y2>ymax)
52					{
53						y2=ymax;
54					}
55				
56					line(x1,y1,x2,y2);
57				}
58				
59				if(i>1)
60				{
61					if(x1<xmin)
62					{
63						x1=xmin;
64					}
65					
66					if(x2>xmax)
67					{
68						x2=xmax;
69					}
70					
71					line(x1,y1,x2,y2);
72				}
73			}
74		}
75	}
76	
77	t1=0;
78	t2=1;
79	
80	for(i=0;i<4;i++)
81	{
82		temp=q[i]/p[i];
83		
84		if(p[i]<0)
85		{
86			if(t1<=temp)
87				t1=temp;
88		}
89		else
90		{
91			if(t2>temp)
92				t2=temp;
93		}
94	}
95	
96	if(t1<t2)
97	{
98		xx1 = x1 + t1 * p[1];
99		xx2 = x1 + t2 * p[1];
100		yy1 = y1 + t1 * p[3];
101		yy2 = y1 + t2 * p[3];
102		line(xx1,yy1,xx2,yy2);
103	}
104	
105	delay(5000);
106	closegraph();
107}
108
similar questions