Skip to content

Instantly share code, notes, and snippets.

@SohanChy
Created February 20, 2018 06:55
Show Gist options
  • Save SohanChy/d4d0d10b798c56c81f0209e94aa7a9bb to your computer and use it in GitHub Desktop.
Save SohanChy/d4d0d10b798c56c81f0209e94aa7a9bb to your computer and use it in GitHub Desktop.
linealgo
#include <iostream>
#include <stdlib.h>
#include <math.h>
#include<GL/gl.h>
#include <GL/glut.h>
using namespace std;
double pointX1, pointY1, pointX2, pointY2;
float roundVal(float d)
{
return floor(d + 0.5);
}
void LineDDA()
{
double dx=(pointX2-pointX1);
double dy=(pointY2-pointY1);
double stepsNeeded;
float xInc,yInc,x=pointX1,y=pointY1;
if(abs(dx)>abs(dy))
{
stepsNeeded= (abs(dx));
}
else
{
stepsNeeded = (abs(dy));
}
xInc=dx/(float)stepsNeeded;
yInc=dy/(float)stepsNeeded;
glClear(GL_COLOR_BUFFER_BIT);
glPointSize(1.0);
glBegin(GL_POINTS);
glVertex2d(x,y);
int i;
for(i=0; i<stepsNeeded; i++)
{
x+=xInc;
y+=yInc;
glVertex2d(roundVal(x), roundVal(y));
}
glEnd();
glFlush();
}
void Bresenham()
{
double dx=(pointX2-pointX1);
double dy=(pointY2-pointY1);
float X=pointX1,Y=pointY1,Xend=pointX2;
double d;
double de;
double dne;
d=2*dy-dx;
de=2*dy;
dne=2*(dy-dx);
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POINTS);
while(X<Xend)
{
glVertex2d(X,Y);
X++;
if(de<=0)
{
d=d+de;
}
else
{
d=d+dne;
Y++;
}
}
glEnd();
glFlush();
}
void myInit()
{
glClearColor(1,1,1,0);
glColor3f(0.0,0.0,0.0);
gluOrtho2D(-320, 320, -240, 240);
}
void inputPoints()
{
cout<<"Enter start points: "<<endl;
cin>>pointX1;
cin>>pointY1;
cout<<"Enter end points:"<<endl;
cin>>pointX2;
cin>>pointY2;
}
int main(int argc, char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowPosition(100,30);
glutInitWindowSize(640,480);
glutCreateWindow("Line Algorithms ");
myInit();
while(true)
{
cout<<"Choose option: "<<endl;
cout<<"1.DDA Line"<<endl<<"2.Bresenham Algo"<<endl<<"3.Quit"<<endl;
int c;
cin>>c;
if(c==1)
{
inputPoints();
glutDisplayFunc(LineDDA);
glutMainLoop();
}
else if(c==2)
{
inputPoints();
glutDisplayFunc(Bresenham);
glutMainLoop();
}
else if(c== 3)
{
cout<<"Exit";
return 0;
}
else
{
continue;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment