/* Affichage des coordonnees des pixels */
/* allumes lors du trace d'un segment de droite */
/* par algorithme de Bresenham */
static void affichageSegment(int *px,int *py) {
int dx,dy,i,xinc,yinc,cumul,x,y;
x = px[0];
y = py[0];
dx = px[1] - px[0];
dy = py[1] - py[0];
xinc = ( dx > 0 ) ? 1 : -1;
yinc = ( dy > 0 ) ? 1 : -1;
dx = abs(dx);
dy = abs(dy);
printf("%3d %3d\n",x,y);
if ( dx > dy ) {
cumul = dx / 2;
for ( i = 1 ; i <= dx ; i++ ) {
x += xinc;
cumul += dy;
if ( cumul >= dx ) {
cumul -= dx;
y += yinc; }
printf("%3d %3d\n",x,y); } }
else {
cumul = dy / 2;
for ( i = 1 ; i <= dy ; i++ ) {
y += yinc;
cumul += dx;
if ( cumul >= dy ) {
cumul -= dy;
x += xinc; }
printf("%3d %3d\n",x,y); } }
}
static int mx[2] = { -1,-1 };
static int my[2] = { -1,-1 };
static int nb = 0;
static int bt = 0;
/* Fonction appelee lors d'un clic de souris */
static void mouse(int bouton,int etat,int x,int y) {
if ( bouton == GLUT_LEFT_BUTTON ) {
mx[nb%2] = x;
my[nb%2] = y;
bt = 1;
if ( etat == GLUT_UP ) {
printf("%4d %4d\n",x,y);
bt = 0;
nb++; }
glutPostRedisplay(); }
if ( bouton == GLUT_RIGHT_BUTTON ) {
if ( etat == GLUT_UP ) {
if ( mx[1] != -1 )
affichageSegment(mx,my); } }
}
/* Fonction appelee lors d'un deplacement */
/* de souris bouton appuye */
static void motion(int x,int y) {
if ( bt ) {
mx[nb%2] = x;
my[nb%2] = y;
printf("%4d %4d\n",x,y);
glutPostRedisplay(); }
}
/* Scene dessinee */
static int aff = 1;
static void scene(void) {
glPushMatrix();
if ( mx[0] != -1 ) {
glPushMatrix();
glTranslatef(mx[0],my[0],0.0F);
glutSolidSphere(10.0,36,36);
glPopMatrix(); }
if ( mx[1] != -1 ) {
glPushMatrix();
glTranslatef(mx[1],my[1],0.0F);
glutSolidSphere(10.0,36,36);
glPopMatrix();
if ( aff ) {
glBegin(GL_LINES);
glVertex2f(mx[0],my[0]);
glVertex2f(mx[1],my[1]);
glEnd(); } }
glPopMatrix();
}