#include <windows.h>   // use as needed for your system
#include <gl/Gl.h>
#include <gl/glut.h>
//<<<<<<<<<<<<<<<<<<<<<<< myInit >>>>>>>>>>>>>>>>>>>>
void myInit(void)
{
	glClearColor(1.0,1.0,1.0,0.0);       // set white background color
	glColor3f(0.0f, 0.0f, 0.0f);          // set the drawing color 
	glPointSize(4.0);		       // a ‘dot’ is 4 by 4 pixels
	glMatrixMode(GL_PROJECTION); 
	glLoadIdentity();
	gluOrtho2D(0.0, 640.0, 0.0, 480.0);
}

void drawLineEquation(int X1, int Y1, int X2, int Y2){
	float slope=(float)(Y2-Y1)/(X2-X1);
	float b=Y1 - (slope*X1);
	if(slope <= 1){
		for(int x=X1; x<X2 ; x++){
			glBegin(GL_POINTS);
				glVertex2f(x , ((slope*x)+b));
			glEnd();
			glFlush();
			Sleep(10);
		}
	}
}

void drawDDA(int X1, int Y1, int X2, int Y2){
	float slope=(float)(Y2-Y1)/(X2-X1);
	if(slope <= 1){
		glBegin(GL_POINTS);
			glVertex2f(X1 , Y1);
		glEnd();
		glFlush();
		Sleep(10);
		float y=Y1;
		for(int x=X1+1; x<X2 ; x++){
			y+=slope;
			glBegin(GL_POINTS);
				glVertex2f(x , y);
			glEnd();
			glFlush();
			Sleep(10);
		}
	}
}

void drawBresenhamAlgorithm(int X1, int Y1, int X2, int Y2){
	float slope=(float)(Y2-Y1)/(X2-X1);
	if(slope <= 1){
		int x=X1 , y=Y1;
		int dx = X2 - X1, dy = Y2 - Y1, dT = 2*(dy - dx) , dS = 2*dy;
		int d = 2*dy - dx;
		glBegin(GL_POINTS);
			glVertex2f(x , y);
		glEnd();
		glFlush();
		Sleep(10);
		while( x < X2 ){
			x++;
			if( d < 0){
				d = d + dS;
			}
			else{
				y++;
				d = d + dT;
			}
			glBegin(GL_POINTS);
				glVertex2f(x , y);
			glEnd();
			glFlush();
			Sleep(10);
		}
	}
}
//<<<<<<<<<<<<<<<<<<<<<<<< myDisplay >>>>>>>>>>>>>>>>>
void myDisplay(void)
{
	glClear(GL_COLOR_BUFFER_BIT);     // clear the screen
	glColor3f(1.0,0.0,0.0);
	drawLineEquation(0,50,640,100);
	glColor3f(0.0,1.0,0.0);
	drawDDA(0,150,640,200);
	glColor3f(0.0,0.0,1.0);
	drawBresenhamAlgorithm(0,250,640,300);
}

//<<<<<<<<<<<<<<<<<<<<<<<< main >>>>>>>>>>>>>>>>>>>>>>
void main(int argc, char** argv)
{
	glutInit(&argc, argv);          // initialize the toolkit
	glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); // set display mode
	glutInitWindowSize(640,480);     // set window size
	glutInitWindowPosition(100, 150); // set window position on screen
	glutCreateWindow("Line Scan Conversion"); // open the screen window
	glutDisplayFunc(myDisplay);     // register redraw function
	myInit(); 
	glutMainLoop(); 		     // go into a perpetual loop
}