-
Notifications
You must be signed in to change notification settings - Fork 0
/
CLIP_LIN.C
144 lines (138 loc) · 2.75 KB
/
CLIP_LIN.C
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
/* This is a program in C to demonstrate 2D clipping using Cohen-Sutherland line clipping */
#include<conio.h>
#include<stdio.h>
#include<math.h>
#include<graphics.h>
void main()
{
int rcode_begin[4]={0,0,0,0},rcode_end[4]={0,0,0,0},region_code[4];
int W_xmax,W_ymax,W_xmin,W_ymin,flag=0;
float slope;
int x,y,x1,y1,i, xc,yc;
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
printf("\n****** Cohen Sutherland Line Clipping algorithm ***********");
printf("\n Now, enter XMin, YMin =");
scanf("%d %d",&W_xmin,&W_ymin);
printf("\n First enter XMax, YMax =");
scanf("%d %d",&W_xmax,&W_ymax);
printf("\n Please enter intial point x and y= ");
scanf("%d %d",&x,&y);
printf("\n Now, enter final point x1 and y1= ");
scanf("%d %d",&x1,&y1);
cleardevice();
//before clipping
rectangle(W_xmin,W_ymin,W_xmax,W_ymax);
line(x,y,x1,y1);
if(y>W_ymax)
{
rcode_begin[0]=1; // Top
flag=1 ;
}
if(y<W_ymin)
{
rcode_begin[1]=1; // Bottom
flag=1;
}
if(x>W_xmax)
{
rcode_begin[2]=1; // Right
flag=1;
}
if(x<W_xmin)
{
rcode_begin[3]=1; //Left
flag=1;
}
//end point of Line
if(y1>W_ymax)
{
rcode_end[0]=1; // Top
flag=1;
}
if(y1<W_ymin)
{
rcode_end[1]=1; // Bottom
flag=1;
}
if(x1>W_xmax)
{
rcode_end[2]=1; // Right
flag=1;
}
if(x1<W_xmin)
{
rcode_end[3]=1; //Left
flag=1;
}
if(flag==0)
{
printf("No need of clipping as it is already in window");
}
flag=1;
for(i=0;i<4;i++)
{
region_code[i]= rcode_begin[i] && rcode_end[i] ;
if(region_code[i]==1)
flag=0;
}
if(flag==0)
{
printf("\n Line is completely outside the window");
}
else
{
slope=(float)(y1-y)/(x1-x);
if(rcode_begin[2]==0 && rcode_begin[3]==1) //left
{
y=y+(float) (W_xmin-x)*slope ;
x=W_xmin;
}
if(rcode_begin[2]==1 && rcode_begin[3]==0) // right
{
y=y+(float) (W_xmax-x)*slope ;
x=W_xmax;
}
if(rcode_begin[0]==1 && rcode_begin[1]==0) // top
{
x=x+(float) (W_ymax-y)/slope ;
y=W_ymax;
}
if(rcode_begin[0]==0 && rcode_begin[1]==1) // bottom
{
x=x+(float) (W_ymin-y)/slope ;
y=W_ymin;
}
// end points
if(rcode_end[2]==0 && rcode_end[3]==1) //left
{
y1=y1+(float) (W_xmin-x1)*slope ;
x1=W_xmin;
}
if(rcode_end[2]==1 && rcode_end[3]==0) // right
{
y1=y1+(float) (W_xmax-x1)*slope ;
x1=W_xmax;
}
if(rcode_end[0]==1 && rcode_end[1]==0) // top
{
x1=x1+(float) (W_ymax-y1)/slope ;
y1=W_ymax;
}
if(rcode_end[0]==0 && rcode_end[1]==1) // bottom
{
x1=x1+(float) (W_ymin-y1)/slope ;
y1=W_ymin;
}
}
outtext("Before Clipping");
getch();
clearviewport();
//after clipping
outtext("After clipping");
rectangle(W_xmin,W_ymin,W_xmax,W_ymax);
setcolor(RED);
line(x,y,x1,y1);
getch();
closegraph();
}