-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCohen Sutherland Line Algorithm.cpp
More file actions
151 lines (130 loc) · 3.69 KB
/
Cohen Sutherland Line Algorithm.cpp
File metadata and controls
151 lines (130 loc) · 3.69 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
#include <stdio.h>
#include <GL/glut.h>
double xmin = 50, ymin = 50, xmax = 100, ymax = 100; //window coordinates
double xvmin = 200, yvmin = 200, xvmax = 300, yvmax = 300; //viewport coordinates
const int LEFT = 1; // code words for LEFT, RIGHT, BOTTOM &TOP.
const int RIGHT = 2;
const int BOTTOM = 4;
const int TOP = 8;
int ComputeOutCode(double x, double y)
{
int code = 0;
if (y > ymax) //above the clip window
code |= TOP;
else if (y < ymin) //below the clip window
code |= BOTTOM;
if (x > xmax) //to the right of clip window
code |= RIGHT;
else if (x < xmin) //to the left of clip window
code |= LEFT;
return code;
}
void CohenSutherland(double x0, double y0, double x1, double y1)
{
int outcode0, outcode1, outcodeOut;
bool accept = false, done = false;
outcode0 = ComputeOutCode(x0, y0); //calculate the region of 1st point
outcode1 = ComputeOutCode(x1, y1); //calculate the region of 2nd point
do
{
if (!(outcode0 | outcode1))
{
accept = true;
done = true;
}
else if (outcode0 & outcode1)
done = true;
else
{
double x, y;
double m = (y1 - y0) / (x1 - x0);
outcodeOut = outcode0 ? outcode0 : outcode1;
if (outcodeOut & TOP)
{
x = x0 + (1 / m) * (ymax - y0);
y = ymax;
}
else if (outcodeOut & BOTTOM)
{
x = x0 + (1 / m) * (ymin - y0);
y = ymin;
}
else if (outcodeOut & RIGHT)
{
y = y0 + m * (xmax - x0);
x = xmax;
}
else
{
y = y0 + m * (xmin - x0);
x = xmin;
}
/* Intersection calculations over */
if (outcodeOut == outcode0)
{
x0 = x;
y0 = y;
outcode0 = ComputeOutCode(x0, y0);
}
else
{
x1 = x;
y1 = y;
outcode1 = ComputeOutCode(x1, y1);
}
}
} while (!done);
if (accept)
{
double sx = (xvmax - xvmin) / (xmax - xmin);
double sy = (yvmax - yvmin) / (ymax - ymin);
double vx0 = xvmin + (x0 - xmin) * sx;
double vy0 = yvmin + (y0 - ymin) * sy;
double vx1 = xvmin + (x1 - xmin) * sx;
double vy1 = yvmin + (y1 - ymin) * sy;
glBegin(GL_LINE_LOOP);
glVertex2f(xvmin, yvmin);
glVertex2f(xvmax, yvmin);
glVertex2f(xvmax, yvmax);
glVertex2f(xvmin, yvmax);
glEnd();
glBegin(GL_LINES);
glVertex2d(vx0, vy0);
glVertex2d(vx1, vy1);
glEnd();
}
}
void display()
{
double x0 = 60, y0 = 20, x1 = 80, y1 = 120;
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1, 1, 1);//white
glBegin(GL_LINES);
glVertex2d(x0, y0);
glVertex2d(x1, y1);
glEnd();
glBegin(GL_LINE_LOOP);
glVertex2f(xmin, ymin);
glVertex2f(xmax, ymin);
glVertex2f(xmax, ymax);
glVertex2f(xmin, ymax);
glEnd();
CohenSutherland(x0, y0, x1, y1);
glFlush();
}
void myinit()
{
glClearColor(0, 0, 0, 1);//black
gluOrtho2D(0, 500, 0, 500);
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(0, 0);
glutCreateWindow("Cohen Sutherland Line Clipping Algorithm");
myinit();
glutDisplayFunc(display);
glutMainLoop();
}