-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLineClipping.cpp
More file actions
148 lines (148 loc) · 3.52 KB
/
LineClipping.cpp
File metadata and controls
148 lines (148 loc) · 3.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include <iostream.h>
#include <conio.h>
#include <graphics.h>
#include <dos.h>
class data
{
int gd, gmode, x, y, xmin,ymin,ymax,xmax;
int a1,a2;
float x1, y1,x2,y2,x3,y3;
int xs, ys, xe, ye;
float maxx,maxy;
public:
void getdata ();
void find ();
void clip ();
void display (float, float,float,float);
void checkonof (int);
void showbit (int);
};
void data :: getdata ()
{
cout<<"Enter the minimum and maximum coordinate of window (x, y) ";
cin >>xmin>>ymin>>xmax>>ymax;
cout<<"Enter the end points of the line to be clipped";
cin >>xs>>ys>>xe>>ye;
display (xs, ys, xe,ye);
}
void data :: display (float, xs, float, ys,float xe, float ye)
{
int gd=DETECT;
initgraph (&gd,&gmode, "");
maxx=getmaxx();
maxy=getmaxy();
line (maxx/2,0,maxx/2,maxy);
line (0, maxy/2,maxx,maxy/2);
rectangle (maxx/2+xmin,maxy/2-ymax,maxx/2+xmax,maxy/2-ymin);
line (maxx/2+xs,maxy/2-ys,maxx/2+xe,maxy/2-ye);
getch();
}
void data :: find ()
{
a1=0;
a2=0;
if ((ys-ymax)>0)
a1+=8;
if ((ymin-ys)>0)
a1+=4;
if ((xs-xmax)>0)
a1+=2;
if ((xmin-xs)>0)
a1+=1;
if ((ye-ymax)>0)
a2+=8;
if ((ymin-ye)>0)
a2+=4;
if ((xe-xmax)>0)
a2+=2;
if ((xmin-xe)>0)
a2+=1;
cout<<"\nThe area code of Ist point is ";
showbit (a1);
getch ();
cout <<"\nThe area code of 2nd point is ";
showbit (a2);
getch ();
}
void data :: showbit (int n)
{
int i,k, and;
for (i=3;i>=0;i--)
{
and =1<<i;
k = n?
k ==0?cout<<"0": cout<<"1\"";
}
}
void data ::clip()
{
int j=a1&a2;
if (j==0)
{
cout<<"\nLine is perfect candidate for clipping";
if (a1==0)
{
else
{
checkonof(a1);
x2=x1;y2=y1;
}
if (a2=0)
{
x3=xe; y3=ye;
}
else
{
checkonof (a2);
x3=x1; y3=y1;
}
xs=x2; ys=y2;xe=x3;ye=y3;
cout << endl;
display (xs,ys,xe,ye);
cout<<"Line after clipping";
getch ()
}
else if ((a1==0) && (a2=0))
{
cout <<"\n Line is in the visible region";
getch ();
}
}
void data :: checkonof (int i)
{
int j, k,l,m;
1=i&1;
x1=0;y1=0;
if (1==1)
{
x1=xmin;
y1=ys+ ((x1-xs)/ (xe-xs))*(ye-ys);
}
j=i&8;
if (j>0)
{
y1=ymax;
x1=xs+(y1-ys)/(ye-ys))*(xe-xs);
}
k=i & 4;
if (k==1)
{
y1=ymin;
x1=xs+((y1-ys)/(ye-ys))*(xe-xs);
}
m= i&2;
if (m==1)
{
x1=xmax;
y1=ys+ ((x1-xs)/ (xe-xs))*(ye-ys);
}
main ()
{
data s;
clrscr();
s.getdata();
s.find();
getch();
closegraph ();
return ();
}