/* Trace d'un segment de droite en 2D           */
/*                                              */
/* Auteur: Nicolas JANEY                        */
/* nicolas.janey@univ-fcomte.fr                 */
/* Novembre 2012                                */

#include <stdlib.h>
#include <stdio.h>
#include <math.h>

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

#include "Position2D.h"

/* Dessin OpenGL                                */

void trace(int xi,int yi,int xf,int yf) {
  glPointSize(9.0F);
  glBegin(GL_POINTS);
  int dx,dy,i,xinc,yinc,cumul,x,y;
  x = xi;
  y = yi;
  dx = xf - xi;
  dy = yf - yi;
  xinc = ( dx > 0 ) ? 1 : -1;
  yinc = ( dy > 0 ) ? 1 : -1;
  dx = abs(dx);
  dy = abs(dy);
  glVertex2i(x*11+4,y*11+4);
  if ( dx > dy ) {
    cumul = dx / 2;
    for ( i = 1 ; i <= dx ; i++ ) {
      x += xinc;
      cumul += dy;
      if ( cumul >= dx ) {
        cumul -= dx;
        y += yinc; }
      glVertex2i(x*11+4,y*11+4); } }
    else {
    cumul = dy / 2;
    for ( i = 1 ; i <= dy ; i++ ) {
      y += yinc;
      cumul += dx;
      if ( cumul >= dy ) {
        cumul -= dy;
        x += xinc; }
      glVertex2i(x*11+4,y*11+4); } }
  glEnd();
  glPointSize(1.0F);
}

void trace(double xi,double yi,double xf,double yf) {
  trace((int) (xi+0.49),(int) (yi+0.49),(int) (xf+0.49),(int) (yf+0.49));
}

void trace(Position2D *pi,Position2D *pf) {
  trace(pi->c[0],pi->c[1],pf->c[0],pf->c[1]);
}
