//#include <GL/gl.h>
//#include <GL/glu.h>
#include <GL/glut.h>
#include <thread>
typedef struct{
float x,y;
}TPoint;
TPoint p1,p2;
int refreshMillis = 100; // Refresh period in milliseconds
float ax,ay,x,y,res;
int i;
void DDA(int nothing)
{
p1.x=-10.0; p1.y=-10.0;
p2.x=10.0; p2.y=10.0;
float m;
//calculate the slope and parameter b
m=(p2.y-p1.y)/(p2.x-p1.x);
if(abs(p2.x-p1.x)>=abs(p2.y-p1.y))
//si la variacion en x es mayor o igual que la variacion en y
res=abs(p2.x-p1.x);
else
//si la variacion en y es mayor que la variacion en x
res=abs(p2.y-p1.y);
ax=(p2.x-p1.x)/res;//el valor a aumentar en x
ay=(p2.y-p1.y)/res;//el valor a aumentar en y
//se realiza casteo a float porque los resultados de la división es un real
x=(float)p1.x;
y=(float)p1.y;
}
void Timer(int value)
{
i=0;
if(i<=res)
{
x=x+ax;
y=y+ay;
i++;
}
glutPostRedisplay(); // Post a paint request to activate display()
glutTimerFunc(refreshMillis, Timer, 0); // subsequent timer call at milliseconds
}
void display(void)
{
glClear (GL_COLOR_BUFFER_BIT);
glPushMatrix();
//std::this_thread::sleep_for(std::chrono::milliseconds(100));
glBegin(GL_LINE_LOOP);
glVertex3f(x,y,0.0);
glVertex3f(x+1,y,0.0);
glVertex3f(x+1,y+1,0.0);
glVertex3f(x,y+1,0.0);
glEnd();
glPopMatrix();
glutSwapBuffers();
}
void init (void)
{
glClearColor (0.0, 0.0, 0.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-15.0,15.0,-15.0,15.0,-15.0,15.0);
glMatrixMode(GL_MODELVIEW);
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (400,400);
glutInitWindowPosition (100,100);
glutCreateWindow ("Algoritmo DDA");
DDA(1);
init ();
glutTimerFunc(0, Timer, 0);
glutDisplayFunc(display);
glutMainLoop();
return 0; /* ISO C requires main to return int. */