Last active
June 12, 2016 17:56
-
-
Save mimukit/a872c3c33995966e4fa035a46fc7f841 to your computer and use it in GitHub Desktop.
OpenGL Line Generation Algorithms Implementation
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 <stdio.h> | |
#include <math.h> | |
#include <iostream> | |
#include <GL/glut.h> | |
using namespace std; | |
int pntX1, pntY1, pntX2, pntY2, choice = 0; | |
double x, y, length, xinc, yinc; | |
int round(double d) | |
{ | |
return floor(d + 0.5); | |
} | |
void plot(int x, int y) | |
{ | |
glBegin(GL_POINTS); | |
glVertex2i(x, y); | |
glEnd(); | |
} | |
void myInit (void) | |
{ | |
glClearColor(1.0, 1.0, 1.0, 0.0); | |
glColor3f(0.0f, 0.0f, 0.0f); | |
glPointSize(4.0); | |
glMatrixMode(GL_PROJECTION); | |
glLoadIdentity(); | |
gluOrtho2D(0.0, 640.0, 0.0, 480.0); | |
} | |
void ddaAlgo() | |
{ | |
if (abs(pntY2 - pntY1) > abs(pntX2 - pntX1)) | |
{ | |
length = abs(pntY2 - pntY1); | |
} | |
else | |
{ | |
length = abs(pntX2 - pntX1); | |
} | |
xinc = (pntX2 - pntX1) / length; | |
yinc = (pntY2 - pntY1) / length; | |
x = pntX1, y = pntY1; | |
for(int i = 0; i < length; i++) | |
{ | |
plot(round(x), round(y)); | |
x += xinc; | |
y += yinc; | |
} | |
} | |
void bresenhamAlgo() | |
{ | |
int dx = (pntX2 - pntX1), | |
dy = (pntY2 - pntY1); | |
int d = 2 * dy - dx; | |
int incrE = 2 * dy; | |
int incrNE = 2 * (dy - dx); | |
int x = pntX1; y = pntY1; | |
while (x < pntX2) | |
{ | |
if (x % 5 == 0) | |
{ | |
goto pass; | |
} | |
plot(x, y); | |
pass: | |
if (d <= 0) | |
{ | |
d += incrE; | |
} | |
else | |
{ | |
d+= incrNE; | |
y++; | |
} | |
x++; | |
} | |
} | |
void drawLineAlg (void) | |
{ | |
if (choice == 1) | |
{ | |
ddaAlgo(); | |
} | |
else if (choice == 2) | |
{ | |
bresenhamAlgo(); | |
} | |
else | |
{ | |
cout << "Error: Invalid choice." << endl; | |
} | |
} | |
void drawLine() | |
{ | |
glBegin(GL_LINES); | |
glVertex2i(pntX1, pntY1+30); | |
glVertex2i(pntX2, pntY2+30); | |
glEnd(); | |
} | |
void myDisplay(void) | |
{ | |
glClear (GL_COLOR_BUFFER_BIT); | |
glColor3f (0.0, 0.0, 0.0); | |
glPointSize(1.0); | |
drawLineAlg(); | |
drawLine(); | |
glFlush (); | |
} | |
void main(int argc, char** argv) | |
{ | |
cout << "Enter your choice:\n\n" << endl; | |
cout << "1. DDA Algorithm" << endl; | |
cout << "2. Bresenham" << endl; | |
cout << "3. Exit" << endl; | |
cin >> choice; | |
if (choice == 3) { | |
return; | |
} | |
cout << "\n\nFor Line:\n" << endl; | |
cout << "Enter an Initial Point: "; cin >> pntX1; cin >> pntY1; | |
cout << "\nEnter the Final Point: "; cin >> pntX2; cin >> pntY2; | |
//cout << "\n\nPoints:" << pntX1 << ", " << pntY1 << ", " << pntX2 << ", " << pntY2 << endl; | |
glutInit(&argc, argv); | |
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); | |
glutInitWindowSize (640, 480); | |
glutInitWindowPosition (100, 150); | |
glutCreateWindow ("Line Drawing Alogrithms"); | |
glutDisplayFunc(myDisplay); | |
myInit (); | |
glutMainLoop(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment