L'exécutable

Le source : TP-Cercles.cpp

/* Auteur: Nicolas JANEY         */
/* nicolas.janey@univ-fcomte.fr  */
/* Avril 2004                    */
/* Illustration du trace de      */
/* cercle par l'algorithme       */
/* de Bresenham (Midpoint)       */

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

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

#include "ModuleCouleurs.h"
#include "ModuleMenus.h"
#include "ModuleReshape.h"

static int aff = 0 ;
static int yy = 0 ;
static int cc = 0 ;
static int c = 1 ;

void circle(int *p1,float r) {
  glColor4fv(couleurBleu()) ;
  glBegin(GL_LINE_LOOP) ;
  for ( int i = 0 ; i < 360 ; i++ ) {
    float angle = i * 3.14159F / 180 ;
    float x = (float) (p1[0] + r*cos(angle)) ;
    float y = (float) (p1[1] + r*sin(angle)) ;
    glVertex2f(x,y) ; }
  glEnd() ;
}

void pixel(int x,int y,float *c) {
  glColor4fv(c) ;
  glBegin(GL_QUADS) ;
  glVertex2f(x-0.5F,y-0.5F) ;
  glVertex2f(x-0.5F,y+0.5F) ;
  glVertex2f(x+0.5F,y+0.5F) ;
  glVertex2f(x+0.5F,y-0.5F) ;
  glEnd() ;
}

void trame(int xi,int xf,int y,float *c) {
  for ( int i = xi ; i <= xf ; i++ )
    pixel(i,y,c);
}

void pixelsSymetriques(int x,int y,float *c) {
  pixel(x,y,c) ;
  pixel(-x,y,c) ;
  pixel(x,-y,c) ;
  pixel(-x,-y,c) ;
  pixel(y,x,c) ;
  pixel(-y,x,c) ;
  pixel(y,-x,c) ;
  pixel(-y,-x,c) ;
}

void cercle(float *c,int r) {
  int x,y,d ;
  x = 0 ;
  y = r ;
  d = 1 - r ;
  pixelsSymetriques(x,y,c) ;
  while ( y > x ) {
    if ( d < 0 )
      d += 2 * x + 3 ;
      else {
      d += 2 * (x - y) + 5 ;
      y-- ; }
    x++ ;
    pixelsSymetriques(x,y,c) ; }
}

void disque(float *c,int r) {
  int x,y,d ;
  x = 0 ;
  y = r ;
  d = 1 - r ;
  trame(-x,x,y,c) ;
  trame(-x,x,-y,c) ;
  trame(-y,y,x,c) ;
  trame(-y,y,-x,c) ;
  while ( y > x ) {
    if ( d < 0 )
      d += 2 * x + 3 ;
      else {
      trame(-x,x,y,c) ;
      trame(-x,x,-y,c) ;
      d += 2 * (x - y) + 5 ;
      y-- ; }
    x++ ;
    trame(-y,y,x,c) ;
    trame(-y,y,-x,c) ; }
}

void cercleIncomplet(float *c,int r,int max,int aff) {
  int x,y,d ;
  x = 0 ;
  y = r ;
  d = 1 - r ;
  pixelsSymetriques(x,y,c) ;
  int cp = 0 ;
  while ( ( y > x ) && ( cp < max ) ) {
    cp++ ;
    if ( d < 0 )
      d += 2 * x + 3 ;
      else {
      d += 2 * (x - y) + 5 ;
      y-- ; }
    x++ ;
    pixelsSymetriques(x,y,c) ; }
}

void disqueIncomplet(float *c,int r,int max,int aff) {
  int x,y,d ;
  x = 0 ;
  y = r ;
  d = 1 - r ;
  trame(-r,r,0,c) ;
  int cp = 0 ;
  while ( ( y > x ) && ( cp < max ) ) {
    cp++ ;
    if ( d < 0 )
      d += 2 * x + 3 ;
      else {
      trame(-x,x,y,c) ;
      trame(-x,x,-y,c) ;
      d += 2 * (x - y) + 5 ;
      y-- ; }
    x++ ;
    trame(-y,y,x,c) ;
    trame(-y,y,-x,c) ; }
}

void display() {
  int p[] = { 0,0 } ;
  glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  glPushMatrix();
  switch (aff) {
    case 0 :
    case 2 :
    case 4 :
    case 6 : if ( ( cc == 1 ) || ( cc == 2 ) )
               disque(couleurVert(),10+(aff/2)*5) ;
             if ( ( cc == 0 ) || ( cc == 2 ) )
               cercle(couleurRouge(),10+(aff/2)*5) ;
             if ( c )
               circle(p,10+(aff/2)*5) ;
             break ;
    case 1 :
    case 3 :
    case 5 :
    case 7 : if ( ( cc == 1 ) || ( cc == 2 ) )
               disqueIncomplet(couleurVert(),10+(aff/2)*5,yy,0) ;
             if ( ( cc == 0 ) || ( cc == 2 ) )
               cercleIncomplet(couleurRouge(),10+(aff/2)*5,yy,0) ;
             if ( c )
               circle(p,10+(aff/2)*5) ;
             break ; }
  glPopMatrix();
  glFlush();
  glutSwapBuffers() ;
}

void myinit() {
  glShadeModel(GL_SMOOTH);
  glClearColor(0.8F,0.8F,0.8F,1.0F);
}

void key(unsigned char key,int x,int y) {
  switch ( key ) {
    case 'c'  :
    case 'C'  : c = !c;
                glutPostRedisplay();
                break;
    case ' '  : cc = (cc+1)%3;
                glutPostRedisplay();
                break;
    case 43   : yy++ ;
                if ( yy == 60 )
                  yy = 0 ;
                glutPostRedisplay();
                break;
    case 45   : yy-- ;
                if ( yy == -1 )
                  yy = 0 ;
                glutPostRedisplay();
                break;
    case 0x0D : yy = 0 ;
                aff = (aff+1)%8 ;
                glutPostRedisplay();
                break;
    case 0x1B : exit(0) ;
                break; }
}

int main(int argc,char **argv) {
  glutInit(&argc,argv);
  glutInitDisplayMode(GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE);
  glutInitWindowSize(300,300); 
  glutInitWindowPosition(50,50); 
  glutCreateWindow("Cercles par l'algorithme du midpoint"); 
  myinit(); 
  creationMenuBasique();
  setParametresOrthoBasique(-30.0,30.0,-30.0,30.0,-40.0,40.0);
  glutReshapeFunc(reshapeOrthoBasique);
  glutKeyboardFunc(key);
  glutDisplayFunc(display);
  glutMainLoop();
  return(0);
}

Les modules utilitaires : Modules.zip

RETOUR