Created
December 2, 2012 19:41
-
-
Save liuerfire/4190610 to your computer and use it in GitHub Desktop.
generate a line with DDA algorithm
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 <GL/glu.h> | |
#include <cmath> | |
void init() | |
{ | |
glClearColor(0.0, 0.0, 0.0, 0.0); | |
glMatrixMode(GL_PROJECTION); | |
glLoadIdentity(); | |
gluOrtho2D(0.0, 500.0, 0.0, 500.0); | |
} | |
void lineWithDDA(int x0, int y0, int x1, int y1) | |
{ | |
int i, steps; | |
float deltaX, deltaY, x, y; | |
if (abs(x1 - x0) > abs(y1 - y0)) { | |
steps = abs(x1 - x0); | |
} | |
else { | |
steps = abs(y1 - y0); | |
} | |
deltaX = (x1 - x0) / steps; | |
deltaY = (y1 - y0) / steps; | |
x = x0; | |
y = y0; | |
glColor3f(1.0, 1.0, 1.0); | |
glBegin(GL_POINTS); | |
for (i = 0; i < steps; i++) { | |
glVertex2i(x, y); | |
x = x + deltaX; | |
y = y + deltaY; | |
} | |
glEnd(); | |
} | |
void display() | |
{ | |
glClear(GL_COLOR_BUFFER_BIT); | |
lineWithDDA(200, 0, 100, 100); | |
lineWithDDA(0, 0, 100, 100); | |
glFlush(); | |
} | |
int main(int argc, char **argv) | |
{ | |
glutInit(&argc, argv); | |
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); | |
glutInitWindowSize(500, 500); | |
glutInitWindowPosition(100, 100); | |
glutCreateWindow("Generate Line"); | |
init(); | |
glutDisplayFunc(display); | |
glutMainLoop(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is very helpful! Thanks