#include <GL/glut.h>
#include <stdlib.h>
#include <math.h>
#include <iostream>
float round_value(float v) {
return floor(v + 0.5);
}
void LineDDA(void) {
//set line coordinates
double X1, Y1, X2, Y2;
X1 = Y1 = 50;
X2 = 300;
Y2 = 200;
double dx=(X2-X1), dy = (Y2-Y1), steps;
float xInc, yInc, x=X1, y=Y1;
/* Find out whether to increment x or y */
steps=(abs(dx)>abs(dy)) ? (abs(dx)) : (abs(dy));
xInc=dx/(float)steps;
yInc=dy/(float)steps;
/* Clears buffers to preset values */
glClear(GL_COLOR_BUFFER_BIT);
/* Plot the points */
glBegin(GL_POINTS);
/* Plot the first point */
glVertex2d(x,y);
int k;
/* For every step, find an intermediate vertex */
for(k=0; k<steps; k++) {
x += xInc;
y += yInc;
glVertex2d(round_value(x), round_value(y));
}
glEnd();
glFlush();
}
int main(int argc,char *argv[]) {
glutInit(&argc,argv); //prepare glut
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); //set the display mode
glutInitWindowSize (500, 500); //the size of the program window
glutInitWindowPosition (100, 100); //initial position for the window
glutCreateWindow ("CS334-lab11"); //create window with a title
glClearColor(0,0,0,0.0); //background color
glMatrixMode (GL_PROJECTION); //2d envyronment
gluOrtho2D (0, 350.0, 0, 250.0); //canvas area (LRBT)
glutDisplayFunc(LineDDA); //show thee actual program
glutMainLoop();
return EXIT_SUCCESS; //exit the program
}
Comments