Created
November 20, 2009 13:57
-
-
Save abuiles/239517 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 <iostream> | |
#include <cstdio> | |
#include <GL/glut.h> | |
using namespace std; | |
int x1,x2,y1,y2 = 0; | |
void drawLine ( int x1 , int y1 , int x2 , int y2 ) | |
{ | |
int dx = x2 - x1 ; | |
int dy = y2 - y1 ; | |
float m = dy / dx; | |
if ( m>=0 && m <= 1 ) | |
{ | |
int y = y1; | |
int p = 2*dx - dy ; | |
glBegin ( GL_POINTS ); | |
for (int x = x1 ; x <= x2 ; x++ ) | |
{ | |
glVertex2f ( (GLint) x , (GLint) y ); | |
if ( p < 0 ) | |
{ | |
p = p + 2*dy; | |
}else | |
{ | |
p = p + 2 * (dy - dx); | |
y = y + 1; | |
} | |
} | |
glEnd (); | |
}else | |
{ | |
int x = x1; | |
int p = 2*dy - dx ; | |
glBegin ( GL_POINTS ); | |
for (int y = y1 ; y <= y2 ; y++ ) | |
{ | |
glVertex2f ( (GLint) x , (GLint) y ); | |
if ( p < 0 ) | |
{ | |
p = p + 2*dx; | |
}else | |
{ | |
p = p + 2 * (dx - dy); | |
x = x + 1; | |
} | |
} | |
glEnd (); | |
} | |
} | |
void Display( void ) | |
{ | |
glClear ( GL_COLOR_BUFFER_BIT); | |
drawLine ( x1, y1, x2 ,y2); | |
glFlush (); | |
} | |
void reshape ( int w , int h ) | |
{ | |
glViewport (0,0,(GLsizei) w, (GLsizei) h) ; | |
glMatrixMode (GL_PROJECTION); | |
glLoadIdentity (); | |
gluOrtho2D ( 0.0 , 160.0 , 0.0 , 120.0 ); | |
} | |
void init ( void ) | |
{ | |
glClearColor ( 0.0 , 0.8 , 0.0 , 1.0 ); | |
glPointSize ( 5.0 ); | |
} | |
int main (int argc, char *argv[] ) | |
{ | |
x1 = 0 ; | |
y1 = 0; | |
x2 = 1 ; | |
y2 = 100; | |
glutInit (&argc , argv ); | |
glutInitDisplayMode ( GLUT_SINGLE | GLUT_RGB ); | |
glutInitWindowSize ( 320 , 240 ) ; | |
glutCreateWindow ( " Bresenham's Algorithm " ) ; | |
init (); | |
glutDisplayFunc ( Display ); | |
glutReshapeFunc ( reshape ) ; | |
glutMainLoop (); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment