-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path7.a.cpp
169 lines (147 loc) · 4.02 KB
/
7.a.cpp
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
#include <GL/glut.h>
#include <iostream>
const char *title = "Cohen-Sutherland";
const GLfloat xmax = 5, ymax = 5, xmin = -5, ymin = -5;
void init()
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glShadeModel(GL_SMOOTH);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-10, 10, -10, 10);
glMatrixMode(GL_MODELVIEW);
}
typedef int BinaryCode;
const int INSIDE = 0; // 0000
const int LEFT = 1; // 0001
const int RIGHT = 2; // 0010
const int BOTTOM = 4; // 0100
const int TOP = 8; // 1000
BinaryCode ComputeOutCode(GLfloat x, GLfloat y)
{
BinaryCode code;
code = INSIDE; // initialised as being inside of [[clip window]]
if (x < xmin) // to the left of clip window
code |= LEFT;
else if (x > xmax) // to the right of clip window
code |= RIGHT;
if (y < ymin) // below the clip window
code |= BOTTOM;
else if (y > ymax) // above the clip window
code |= TOP;
return code;
}
void cohen_sutherland(GLfloat x0, GLfloat y0, GLfloat x1, GLfloat y1,
GLfloat &x2, GLfloat &y2, GLfloat &x3, GLfloat &y3)
{
BinaryCode outcode0 = ComputeOutCode(x0, y0);
BinaryCode outcode1 = ComputeOutCode(x1, y1);
bool accept = false;
while (true)
{
if (!(outcode0 | outcode1))
{
accept = true;
break;
}
else if (outcode0 & outcode1)
{
break;
}
else
{
GLfloat x, y;
BinaryCode outcodeOut = outcode0 ? outcode0 : outcode1;
if (outcodeOut & TOP)
{
x = x0 + (x1 - x0) * (ymax - y0) / (y1 - y0);
y = ymax;
}
else if (outcodeOut & BOTTOM)
{
x = x0 + (x1 - x0) * (ymin - y0) / (y1 - y0);
y = ymin;
}
else if (outcodeOut & RIGHT)
{
y = y0 + (y1 - y0) * (xmax - x0) / (x1 - x0);
x = xmax;
}
else if (outcodeOut & LEFT)
{
y = y0 + (y1 - y0) * (xmin - x0) / (x1 - x0);
x = xmin;
}
if (outcodeOut == outcode0)
{
x0 = x;
y0 = y;
outcode0 = ComputeOutCode(x0, y0);
}
else
{
x1 = x;
y1 = y;
outcode1 = ComputeOutCode(x1, y1);
}
}
}
if (accept)
{
std::cout << "Found\n";
x2 = x0;
y2 = y0;
x3 = x1;
y3 = y1;
}
}
void display()
{
GLfloat x0 = -7, y0 = 6, x1 = 8, y1 = 3, x2, y2, x3, y3;
cohen_sutherland(x0, y0, x1, y1, x2, y2, x3, y3);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);
glBegin(GL_LINE_LOOP);
glVertex2i(xmin, ymin);
glVertex2i(xmin, ymax);
glVertex2i(xmax, ymax);
glVertex2i(xmax, ymin);
glEnd();
glColor3f(0.0, 0.0, 1.0);
glBegin(GL_LINES);
glVertex2f(x0, y0);
glVertex2f(x1, y1);
glEnd();
glColor3f(0.0, 0.0, 1.0);
glBegin(GL_LINES);
glVertex2f(x0, y0);
glVertex2f(x2, y2);
glEnd();
glColor3f(1.0, 0.0, 0.0);
glBegin(GL_LINES);
glVertex2f(x2, y2);
glVertex2f(x3, y3);
glEnd();
glColor3f(0.0, 0.0, 1.0);
glBegin(GL_LINES);
glVertex2f(x1, y1);
glVertex2f(x3, y3);
glEnd();
glFlush();
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(700, 700);
glutInitWindowPosition(7000, 70);
glutCreateWindow(title);
glutDisplayFunc(display);
init();
glutMainLoop();
return 0;
}