include iostream include conio include graphics int xl yl xh yh poly 1

  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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#include <iostream.h>
#include <conio.h>
#include <graphics.h>
int xl,yl,xh,yh,poly[100],n;
void left_clip(){
int temp[100],i,j=0,count=0,x1,y1,x2,y2;
for(i=0;i<2*n;i+=2){
x1 = poly[i];
y1 = poly[i+1];
x2 = poly[i+2];
y2 = poly[i+3];
if(x1<xl && x2<xl){
//both points outside. Do not store any vertices
}else if(x1>xl && x2>xl){
//both points inside. Store second vertex
temp[j] = x2;
temp[j+1] = y2;
j+=2;
count++;
}else if(x1<xl && x2>xl){
//outside to inside. Store intersection n second vertex
int x=xl;
int y= y1 + (xl-x1)*(float)(y2-y1)/(x2-x1);
temp[j]=x;
temp[j+1]=y;
temp[j+2]=x2;
temp[j+3]=y2;
j+=4;
count+=2;
}else{
//inside to outside. Store intersection only
int x=xl;
int y= y1 + (xl-x1)*(float)(y2-y1)/(x2-x1);
temp[j] = x;
temp[j+1] = y;
j+=2;
count++;
}
}
n=count;
//store 1st vertex as last
temp[j]=temp[0];
temp[j+1]=temp[1];
for(i=0;i<2*(n+1);i++)
poly[i]=temp[i];
}
void right_clip(){
int temp[100],i,j=0,count=0,x1,y1,x2,y2;
for(i=0;i<2*n;i+=2){
x1 = poly[i];
y1 = poly[i+1];
x2 = poly[i+2];
y2 = poly[i+3];
if(x1>xh && x2>xh){
//both points outside. Do not store any vertices
}else if(x1<xh && x2<xh){
//both points inside. Store second vertex
temp[j] = x2;
temp[j+1] = y2;
j+=2;
count++;
}else if(x1>xh && x2<xh){
//outside to inside. Store intersection n second vertex
int x=xh;
int y= y1 + (xh-x1)*(float)(y2-y1)/(x2-x1);
temp[j]=x;
temp[j+1]=y;
temp[j+2]=x2;
temp[j+3]=y2;
j+=4;
count+=2;
}else{
//inside to outside. Store intersection only
int x=xh;
int y= y1 + (xh-x1)*(float)(y2-y1)/(x2-x1);
temp[j] = x;
temp[j+1] = y;
j+=2;
count++;
}
}
n=count;
//store 1st vertex as last
temp[j]=temp[0];
temp[j+1]=temp[1];
for(i=0;i<2*(n+1);i++)
poly[i]=temp[i];
}
void bottom_clip(){
int temp[100],i,j=0,count=0,x1,y1,x2,y2;
for(i=0;i<2*n;i+=2){
x1 = poly[i];
y1 = poly[i+1];
x2 = poly[i+2];
y2 = poly[i+3];
if(y1>yl && y2>yl){
//both points outside. Do not store any vertices
}else if(y1<yl && y2<yl){
//both points inside. Store second vertex
temp[j] = x2;
temp[j+1] = y2;
j+=2;
count++;
}else if(y1>yl && y2<yl){
//outside to inside. Store intersection n second vertex
int x= x1 + (yl-y1)/((float)(y2-y1)/(x2-x1));
int y= yl;
temp[j]=x;
temp[j+1]=y;
temp[j+2]=x2;
temp[j+3]=y2;
j+=4;
count+=2;
}else{
//inside to outside. Store intersection only
int x= x1 + (yl-y1)/((float)(y2-y1)/(x2-x1));
int y= yl;
temp[j] = x;
temp[j+1] = y;
j+=2;
count++;
}
}
n=count;
//store 1st vertex as last
temp[j]=temp[0];
temp[j+1]=temp[1];
for(i=0;i<2*(n+1);i++)
poly[i]=temp[i];
}
void top_clip(){
int temp[100],i,j=0,count=0,x1,y1,x2,y2;
for(i=0;i<2*n;i+=2){
x1 = poly[i];
y1 = poly[i+1];
x2 = poly[i+2];
y2 = poly[i+3];
if(y1<yh && y2<yh){
//both points outside. Do not store any vertices
}else if(y1>yh && y2>yh){
//both points inside. Store second vertex
temp[j] = x2;
temp[j+1] = y2;
j+=2;
count++;
}else if(y1<yh && y2>yh){
//outside to inside. Store intersection n second vertex
int x= x1 + (yh-y1)/((float)(y2-y1)/(x2-x1));
int y= yh;
temp[j]=x;
temp[j+1]=y;
temp[j+2]=x2;
temp[j+3]=y2;
j+=4;
count+=2;
}else{
//inside to outside. Store intersection only
int x= x1 + (yh-y1)/((float)(y2-y1)/(x2-x1));
int y= yh;
temp[j] = x;
temp[j+1] = y;
j+=2;
count++;
}
}
n=count;
//store 1st vertex as last
temp[j]=temp[0];
temp[j+1]=temp[1];
for(i=0;i<2*(n+1);i++)
poly[i]=temp[i];
}
void main(){
int gdriver = DETECT,gmode;
initgraph(&gdriver,&gmode,C:\TC\BGI);
int i;
setcolor(BLUE);
cout<<Enter bottom left and top right co-ordinates of window: ;
cin>>xl>>yl>>xh>>yh;
rectangle(xl,yl,xh,yh);
cout<<Enter the no. of vertices: ;
cin>>n;
for(i=0;i<2*n;i+=2){
cout<<Enter co-ordinates of vertex <<(i/2+1)<<: ;
cin>>poly[i]>>poly[i+1];
}
//store 1st vertex as last
poly[2*n] = poly[0];
poly[2*n+1] = poly[1];
drawpoly(n+1,poly);
getch();
left_clip();
right_clip();
bottom_clip();
top_clip();
cout<<After clipping:;
setcolor(WHITE);
drawpoly(n+1,poly);
getch();
closegraph();
}