-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLineClipping.c
More file actions
235 lines (200 loc) · 5.14 KB
/
Copy pathLineClipping.c
File metadata and controls
235 lines (200 loc) · 5.14 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
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/*Code to show line clipping using Cyrus Beck Algorithm (Liang Barsky Algorithm)
Copyright - Prateek Narendra, Gokul Shanth, Jashan Shetty (2014)
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#if defined(__APPLE__)
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
GLdouble width, height; /* window width and height */
int wd; /* GLUT window handle */
float a,b,c,d,e,f,g,h; /* array of 2D points */
void takevalues() /*Takes values of coordinates of rectangle and line*/
{
printf("Enter center coordinates of rectangle : ");
scanf("%f%f",&a,&b);
printf("Enter length and breadth of rectangle : ");
scanf("%f%f",&c,&d);
printf("Enter the coordinates of point 1 : ");
scanf("%f%f",&e,&f);
printf("Enter the coordinates of point 2 : ");
scanf("%f%f",&g,&h);
}
float minimum(float a1,float a2,float a3) /*Calculates the minimum parameter for exit point*/
{
if(a1<a2)
{
if(a1<a3)
{
return a1;
}
else
{
return a3;
}
}
else
{
if(a2<a3)
{
return a2;
}
else
{
return a3;
}
}
}
float maximum(float a1,float a2,float a3) /*Calculates the maximum parameter for entry point*/
{
if(a1>a2)
{
if(a1>a3)
{
return a1;
}
else
{
return a3;
}
}
else
{
if(a2>a3)
{
return a2;
}
else
{
return a3;
}
}
}
void init(void)
{
width = 1366.0; /* initial window width and height,*/
height = 768.0; /* within which we draw. */
}
/* Callback functions for GLUT */
/* Draw the window - this is where all the GL actions are */
void
display(void)
{
float centre[2]={a,b},lb[2]={c,d},rect1[2]={(centre[0]-(lb[0]/2)),(centre[1]-(lb[1]/2))},rect2[2]={(centre[0]-(lb[0]/2)),(centre[1]+(lb[1]/2))},rect3[2]={(centre[0]+(lb[0]/2)),(centre[1]+(lb[1]/2))},rect4[2]={(centre[0]+(lb[0]/2)),(centre[1]-(lb[1]/2))},line1[2]={e,f},line2[2]={g,h};
float p1,p2,p3,p4,t1,t2,t3,t4;//Above line - calculated the 4 coordinates of rectangle and 2 coordinates of line
float dx=line2[0]-line1[0];
float dy=line2[1]-line1[1];
int flags[2];
if(dx==0||dy==0)
{
line1[0]=line1[0]+1;
line1[1]=line1[1]+1;
dx=line2[0]-line1[0];
dy=line2[1]-line1[1];
}
if(dx<0)
{
p1=(rect3[0]-line1[0])/(line2[0]-line1[0]);
p2=(line1[0]-rect1[0])/(line1[0]-line2[0]);//Parameter calculation using x coordinates
flags[0]=1;
}
else
{
p2=(rect3[0]-line1[0])/(line2[0]-line1[0]);
p1=(line1[0]-rect1[0])/(line1[0]-line2[0]);
flags[0]=0;
}
if(dy<0)
{
p3=(rect3[1]-line1[1])/(line2[1]-line1[1]);
p4=(line1[1]-rect1[1])/(line1[1]-line2[1]);//Parameter calculation using y coordinates
flags[1]=1;
}
else
{
p4=(rect3[1]-line1[1])/(line2[1]-line1[1]);
p3=(line1[1]-rect1[1])/(line1[1]-line2[1]);
flags[1]=0;
}
float t_entry=maximum(0.0,p1,p3);//calculates the maximum t for entry point for all dx or dy < 0
float t_exit=minimum(1.0,p2,p4);//Calculates the minimum t for exit point for all dx or dy > 0
float newline10=line1[0]+(dx*t_entry);
float newline11=line1[1]+(dy*t_entry);//Calculation on new end points
float newline20=line1[0]+(dx*t_exit);
float newline21=line1[1]+(dy*t_exit);
printf("%f %f %f %f",newline10,newline11,newline20,newline21);
printf("\nExit : %f",t_exit);
printf("\nEntry : %f",t_entry);
if(t_entry>t_exit)
{
glClearColor(1.0, 1.0, 1.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT);
/* draw a black line */
glColor3f(0.0, 0.0, 0.0);
glLineWidth(3.0);
glBegin(GL_LINES);
glVertex2f(rect1[0],rect1[1]);
glVertex2f(rect2[0],rect2[1]);
glVertex2f(rect2[0],rect2[1]);
glVertex2f(rect3[0],rect3[1]);//Drawing only rectangle if t_entry>t_exit
glVertex2f(rect3[0],rect3[1]);
glVertex2f(rect4[0],rect4[1]);
glVertex2f(rect4[0],rect4[1]);
glVertex2f(rect1[0],rect1[1]);
glEnd();
}
//clear the screen to white
else
{
glClearColor(1.0, 1.0, 1.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT);
/* draw a black line */
glColor3f(0.0, 0.0, 0.0);
glLineWidth(3.0);
glBegin(GL_LINES);
glVertex2f(rect1[0],rect1[1]);
glVertex2f(rect2[0],rect2[1]);
glVertex2f(rect2[0],rect2[1]);
glVertex2f(rect3[0],rect3[1]);//Drawing new line alongwith clipping window rectangle if t_entry<t_exit
glVertex2f(rect3[0],rect3[1]);
glVertex2f(rect4[0],rect4[1]);
glVertex2f(rect4[0],rect4[1]);
glVertex2f(rect1[0],rect1[1]);
glVertex2f(newline10,newline11);
glVertex2f(newline20,newline21);
glEnd();
}
glFlush();
}
/* Called when window is resized,
also when window is first created,
before the first call to display(). */
void reshape(int w, int h)
{
/* save new screen dimensions */
width = (GLdouble) w;
height = (GLdouble) h;
/* tell OpenGL to use the whole window for drawing */
glViewport(0, 0, (GLsizei) width, (GLsizei) height);
/* do an orthographic parallel projection with the coordinate
system set to first quadrant, limited by screen/window size */
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, width, 0.0, height);
}
int main(int argc, char *argv[])
{
takevalues();
init();
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA);
glutInitWindowSize((int) width, (int) height);
wd = glutCreateWindow("Liang Barsky Line Clipping");
glutReshapeFunc(reshape);
glutDisplayFunc(display);
glutMainLoop();
return 0;
}