Bresnham's Line Algorthim

#include<GL/glu.h>
#include<GL/glut.h>
#include<GL/gl.h>

using namespace std;

void draw(int x0, int y0,int  x1,int y1)
{
     glClear(GL_COLOR_BUFFER_BIT);
      glPointSize(2);
     glColor3f(1.0,0.0,0.0);
     glBegin(GL_POINTS);
     int dx=x1-x0;
     int dy=y1-y0;
     int x=x0,y=y0;
     int p,p0=2*dy-dx;
     p=p0;
     glPointSize(2);
     glVertex2i(x0,y0);
     while(x!=x1&&y!=y1)
     {
            if(p<0)
            {
                x=x+1;
                glVertex2i(x,y);
                p=p+2*dy;
            }
            else
            {
                x+=1;
                y+=1;
                glVertex2i(x,y);
                p=p+2*dy-2*dx;
            }
     }
     glEnd();
     glFlush();

}

void init()
{
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
   gluOrtho2D(-50, 50, -50, 50);
}

void display()
{
     draw(20,10,30,18);
}

int main(int argc,char **argv)
{
     glutInit(&argc,argv);
     glutInitDisplayMode(GLUT_RGB|GLUT_SINGLE);
     glutInitWindowSize(500,500);
     glutCreateWindow("Bresnham's Line Algorthim");
     glutDisplayFunc(display);
init();
     glutMainLoop();
}



Popular posts from this blog

Shutdown Pc

Ellipse using OpenGl

String Comparisons