Mzsmunna
10/16/2019 - 3:13 PM

BrasenhamMidpointLineAlgo


#include <iostream>
//#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <GL/gl.h>
#include <GL/glut.h>

using namespace std;

void plot(int a,int b)
{

    glBegin(GL_POINTS);
     glVertex2i(a, b);
    glEnd();
}

void myDisplay(void)
{

    glClear (GL_COLOR_BUFFER_BIT);
//glColor3f (1.0, 1.0, 0.0);
    glColor3ub (0, 0, 255);
    glPointSize(10.0);

    int x1=100,y1=200,x2=200,y2=300;
    int x=x1; int y=y1;
    int dx=x2-x1;
    int dy=y2-y1;
    int dT=2*(dy-dx);
    int dS=2*dy;
    int d=2*dy-dx;
    plot(x,y);
    while(x<x2){
        x++;
        if(d<0)
        {
            d+=dS;
        }
        else
        {
            y++;
            d+=dT;
        }
        plot(x,y);
    }
    glFlush ();
}

void myInit (void)
{
glClearColor(1.0, 0.0, 0.0, 100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluOrtho2D(0.0, 640.0, 0.0, 600.0);
}
 main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (640, 600);
glutInitWindowPosition (300, 150);
glutCreateWindow ("Bressenhan Algo");
glutDisplayFunc(myDisplay);
myInit ();
glutMainLoop();
}