Created
December 17, 2020 08:19
-
-
Save meldsza/2bc299c6fde8a5e83a35dc5e4ea30b56 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <GL/glut.h> | |
#include <stdio.h> | |
#include <stdbool.h> | |
const int INSIDE = 0; | |
const int LEFT = 1; | |
const int RIGHT = 2; | |
const int BOTTOM = 4; | |
const int TOP = 8; | |
double xmin = 50, ymin = 50, xmax = 100, ymax = 100; | |
void drawRectangle(float x0, float y0, float x1, float y1) { | |
glColor3f(1.0, 0.0, 0.0); | |
glBegin(GL_LINE_LOOP); | |
glVertex2f(x0, y0); | |
glVertex2f(x1, y0); | |
glVertex2f(x1, y1); | |
glVertex2f(x0, y1); | |
glEnd(); | |
} | |
void drawLine(float x0, float y0, float x1, float y1) { | |
glColor3f(0, 1, 0.0); | |
glBegin(GL_LINES); | |
glVertex2f(x0, y0); | |
glVertex2f(x1, y1); | |
glEnd(); | |
} | |
void init() { | |
glClearColor(0, 0, 0, 0); | |
glClear(GL_COLOR_BUFFER_BIT); | |
gluOrtho2D(0, 500, 0, 500); | |
glFlush(); | |
} | |
int Computecode(double x, double y) { | |
int code = INSIDE; | |
if (x < xmin) | |
code |= LEFT; | |
else if (x > xmax) | |
code |= RIGHT; | |
else if (y < ymin) | |
code |= BOTTOM; | |
else if (y > ymax) | |
code |= TOP; | |
return code; | |
} | |
void CohenSutherlandline(double x0, double a, double x1, double b) { | |
bool accept = false; | |
int outcode0 = Computecode(x0, a); | |
int outcode1 = Computecode(x1, b); | |
double x, y; | |
while (true) { | |
if (!(outcode0 | outcode1)) { | |
accept = true; | |
break; | |
} else if (outcode0 & outcode1) { | |
break; | |
} else { | |
int outcodeout = outcode0 ? outcode0 : outcode1; | |
if (outcodeout & TOP) { | |
x = x0 + (x1 - x0) * (ymax - a) / (b - a); | |
y = ymax; | |
} else if (outcodeout & BOTTOM) { | |
x = x0 + (x1 - x0) * (ymin - a) / (b - a); | |
y = ymin; | |
} else if (outcodeout & RIGHT) { | |
y = a + (b - a) * (xmax - x0) / (x1 - x0); | |
x = xmax; | |
} else if (outcodeout & LEFT) { | |
y = a + (b - a) * (xmin - x0) / (x1 - x0); | |
x = xmin; | |
} | |
if (outcodeout == outcode0) { | |
x0 = x; | |
a = y; | |
outcode0 = Computecode(x0, a); | |
} else { | |
x1 = x; | |
b = y; | |
outcode1 = Computecode(x1, b); | |
} | |
} | |
} | |
if (accept) { | |
drawRectangle(100 + xmin, 100 + ymin, 100 + xmax, 100 + ymax); | |
drawLine(100 + x0, 100 + a, 100 + x1, 100 + b); | |
} | |
} | |
void display() { | |
double x0 = 60, a = 20, x1 = 80, b = 120; | |
drawRectangle(xmin, ymin, xmax, ymax); | |
drawLine(x0, a, x1, b); | |
CohenSutherlandline(x0, a, x1, b); | |
glFlush(); | |
} | |
int main(int argc, char ** argv) { | |
glutInit( & argc, argv); | |
glutCreateWindow("Cohen Sutherland line clipping window"); | |
init(); | |
glutDisplayFunc(display); | |
glutMainLoop(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment