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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
| #include<iostream.h>
#include<graphics.h>
#include<conio.h>
/* In Line Clipping:
INPUT()
Enter 1st pt, 2nd pt. (Keep window fixed.)
CHECK()
check each point and MAP 4 3 2 1 CODE
AND()
return if inside or outside
CLIP() & PINSIDE()
Y=MX+C
DRAW()
just the lines and window
*/
double x1,y1,x2,y2;
double Xmin,Ymin,Xmax,Ymax;
int CODE1[4];
int CODE2[4];
void INPUT()
{
cout<<"\n Enter First Point: ";
cin>>x1>>y1;
cout<<"\n Enter Second Point: ";
cin>>x2>>y2;
Xmin=100;
Ymin=100;
Xmax=300;
Ymax=300;
}
void DRAW()
{
setcolor(CYAN);
rectangle(Xmin,Ymin,Xmax,Ymax);
setcolor(WHITE);
line(x1,y1,x2,y2);
}
void CHECK(int x,int y, int CODE[4])
{ //arrays call by reference
if(y > Ymax)
CODE[3]=1;
else
CODE[3]=0;
if(y < Ymin)
CODE[2]=1;
else
CODE[2]=0;
if(x > Xmax)
CODE[1]=1;
else
CODE[1]=0;
if(x < Xmin)
CODE[0]=1;
else
CODE[0]=0;
}
int AND(int code1[4], int code2[4])
{
for(int i=0; i<4; i++)
{
if( code1[i] == 1 && code2[i] == 1 )
return 1;
}
return 0;
}
void PINSIDE(double &x, double &y)
{
if( x < Xmin )
{
y=y + ( ((y2-y1)/(x2-x1)) * (Xmin-x) );
x=Xmin;
}
if( x > Xmax )
{
y=y + ( ((y2-y1)/(x2-x1)) * (Xmax-x) );
x=Xmax;
}
if( y > Ymax )
{
x=x + ( (x2-x1)/(y2-y1) * (Ymax-y) );
y=Ymax;
}
if( y < Ymin )
{
x=x + ( (x2-x1)/(y2-y1) * (Ymin-y) );
y=Ymin;
}
}
void CLIP()
{
CHECK(x1,y1,CODE1);
CHECK(x2,y2,CODE2);
int flag=AND(CODE1,CODE2);
if( flag==1 )
{
cout<<" COMPLETELY OUTSIDE ";
x1=0;
x2=0;
y1=0;
y2=0;
DRAW();
}
else if( flag==0 )
{
if( CODE1[0]==1 || CODE1[1]==1 || CODE1[2]==1 || CODE1[3]==1 )
{
cout<<" PARTIALLY INSIDE ";
PINSIDE(x1,y1);
}
if( CODE2[0]==1 || CODE2[1]==1 || CODE2[2]==1 || CODE2[3]==1 )
{
cout<<" PARTIALLY INSIDE ";
PINSIDE(x2,y2);
}
if( CODE1[0]==0 && CODE1[1]==0 && CODE1[2]==0 && CODE1[3]==0
&& CODE2[0]==0 && CODE2[1]==0 && CODE2[2]==0 && CODE2[3]==0 )
{
cout<<" COMPLETELY INSIDE";
}
DRAW();
}
}
void main()
{
INPUT();
int gdriver=DETECT, gmode;
initgraph(&gdriver,&gmode,"C:/TC/BGI");
DRAW();
getch();
cleardevice();
CLIP();
DRAW();
getch();
closegraph();
}
|