-
Notifications
You must be signed in to change notification settings - Fork 0
/
cohen sutherland line clipping algorithm in c++
179 lines (153 loc) · 2.94 KB
/
cohen sutherland line clipping algorithm in 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
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
#include <windows.h>
#include<GL/glut.h>
#include<math.h>
#include<stdio.h>
#include<iostream>
using namespace std;
int outcode();
void LineClipping();
float xmin=-100,ymin=-100,xmax=100,ymax=100;
float xd1,yd1,xd2,yd2;
int hh,hhh;
void init(int w, int h)
{
glClearColor(1.0, 1.0, 1.0, 1.0);
glViewport(0,0, (GLsizei)w,(GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D( (GLdouble)w/-2,(GLdouble)w/2, (GLdouble)h/-2, (GLdouble)h/2);
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
int i=0;
glPointSize(1);
glLineWidth(2);
glColor3f(0,0, 0);
glBegin(GL_LINES);
glVertex2f(0, -240);
glVertex2f(0, 240);
glVertex2f(-320, 0);
glVertex2f(320, 0);
glEnd();
int j=-240,k=-320,l=0;
glBegin(GL_POINTS);
while(j<=240){
for( i=-320;i<=320;i++)
glVertex2f(i,j);
j+=20;
}
while(k<=320){
for( l=-240;l<=240;l++)
glVertex2f(k,l);
k+=20;
}
glEnd();
glColor3f(0.000, 0.749, 1.000);
glBegin(GL_LINE_LOOP);
glVertex2i(xmin,ymin);
glVertex2i(xmin,ymax);
glVertex2i(xmax,ymax);
glVertex2i(xmax,ymin);
glEnd();
glColor3f(1.0,0.0,0.0);
glBegin(GL_LINES);
glVertex2i(xd1,yd1);
glVertex2i(xd2,yd2);
glEnd();
glFlush();
}
int outcode(float x,float y)
{
int code=0;
if(y>ymax)
code=8;
if(y<ymin)
code=4;
if(x>xmax)
code=2;
if(x<xmin)
code=1;
return code;
}
void LineClipping(float x1,float y1,float x2,float y2)
{
int c1=outcode(x1,y1);
int c2=outcode(x2,y2);
float m=(y2-y1)/(x2-x1);
while((c1|c2)>0)
{
if((c1 & c2)>0)
{
exit(0);
}
float xi=x1;float yi=y1;
int c=c1;
if(c==0)
{
c=c2;
xi=x2;
yi=y2;
}
float x,y;
if(c==8)
{
y=ymax;
x=xi+ 1/m*(ymax-yi);
}
else
if(c==4)
{
y=ymin;
x=xi+1/m*(ymin-yi);
}
else
if(c==2)
{
x=xmax;
y=yi+m*(xmax-xi);
}
else
if(c==1)
{
x=xmin;
y=yi+m*(xmin-xi);
}
if(c==c1)
{
xd1=x;
yd1=y;
c1=outcode(xd1,yd1);
}
if(c==c2)
{
xd2=x;
yd2=y;
c2=outcode(xd2,yd2);
}
}
display();
}
void mykey(unsigned char key,int x,int y)
{
if(key=='c')
{
LineClipping(xd1,yd1,xd2,yd2);
glFlush();
}
}
int main(int argc,char** argv)
{
printf("Enter line co-ordinates:");
cin>>xd1>>yd1>>xd2>>yd2;
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(640,480);
glutInitWindowPosition(400,200);
glutCreateWindow("Clipping");
glutDisplayFunc(display);
glutKeyboardFunc(mykey);
init(640, 480);
glutMainLoop();
return 0;
}