L'exécutable

Algo23.gif (9617 octets)

Algo23.gif (9617 octets)

Algo23.gif (9617 octets)

Le source:Clipping.cpp

#include <windows.h>

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

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

struct point {
  int x ;
  int y ; } ;

struct segment {
  point pi ;
  point pf ; } ;

struct rectangle {
  point sg ;
  point id ; } ;

static int aff = 0 ;
static segment s = { -5,0,5,0 } ;
static int p1 = 1 ;

void traceSegment(int xi,int yi,int xf,int yf,float *c) {
  glColor3fv(c) ;
  glBegin(GL_LINES) ;
  glVertex2f((float) xi,(float) yi) ;
  glVertex2f((float) xf,(float) yf) ;
  glEnd() ;
}

void pixel(int x,int y,float *c) {
  glColor3fv(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 line(int xi,int yi,int xf,int yf,float *c) {
  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) ;
  pixel(x,y,c) ;
  if ( dx > dy ) {
    cumul = dx / 2 ;
    for ( i = 1 ; i <= dx ; i++ ) {
      x += xinc ;
      cumul += dy ;
      if (cumul >= dx) {
        cumul -= dx ;
        y += yinc ; }
      pixel(x,y,c) ; } }
    else {
    cumul = dy / 2 ;
    for ( i = 1 ; i <= dy ; i++ ) {
      y += yinc ;
      cumul += dx ;
      if ( cumul >= dy ) {
        cumul -= dy ;
        x += xinc ; }
      pixel(x,y,c) ; } }
}

void traceRectangle(rectangle *r,float *c) {
  line(r->id.x,r->id.y,r->id.x,r->sg.y,c) ;
  line(r->id.x,r->sg.y,r->sg.x,r->sg.y,c) ;
  line(r->id.x,r->id.y,r->sg.x,r->id.y,c) ;
  line(r->sg.x,r->id.y,r->sg.x,r->sg.y,c) ;
}

int code(point *p,rectangle *r) { 
  int c ;
  if ( p->x < r->sg.x )
    c = 1 ;
    else
    c = 0 ;
  if ( p->x > r->id.x )
    c += 2 ;
  if ( p->y < r->sg.y )
    c += 4 ;
  if ( p->y > r->id.y )
    c += 8 ;
  return(c) ;
}

int code_nul(int c) { 
  return(c == 0) ;
}

int pas1commun(int c1,int c2) { 
  return((c1&c2) == 0) ;
}

int code0(int c) { 
  return(c&1) ;
}

int code1(int c) { 
  return(c&2) ;
}

int code2(int c) { 
  return(c&4) ;
}

int code3(int c) { 
  return(c&8) ;
}

int intersection(int ai,int bi,int af,int bf,int val) {
  int res = 0 ;
  if ( af-ai != 0 )
    res =(int) (bi +(double) (val-ai) / (af-ai) * (bf-bi)) ;
    else
    res = 32767 ;
  return(res) ;
}

void clipperSegment(rectangle *r,segment *s,float *c) {
  segment se = *s ;
  int c1,c2 ;
  point p ;
  c1 = code(&s->pi,r) ;
  c2 = code(&s->pf,r) ;
  while ( ( !code_nul(c1) || !code_nul(c2) ) && ( pas1commun(c1,c2)  ) ) {
    if ( code_nul(c1) ) {
      p = s->pi ;
      s->pi = s->pf ;
      s->pf = p ;
      c1 = c2 ; }
    if ( code0(c1) ) {
      s->pi.y = intersection(s->pi.x,s->pi.y,s->pf.x,s->pf.y,r->sg.x) ;
      s->pi.x = r->sg.x ; }
      else
      if ( code1(c1) ) {
        s->pi.y = intersection(s->pi.x,s->pi.y,s->pf.x,s->pf.y,r->id.x) ;
        s->pi.x = r->id.x ; }
        else
        if ( code2(c1) ) {
          s->pi.x = intersection(s->pi.y,s->pi.x,s->pf.y,s->pf.x,r->sg.y) ;
          s->pi.y = r->sg.y ; }
          else
          if ( code3(c1) ) {
            s->pi.x = intersection(s->pi.y,s->pi.x,s->pf.y,s->pf.x,r->id.y) ;
            s->pi.y = r->id.y ; }
    c1 = code(&s->pi,r) ;
    c2 = code(&s->pf,r) ; }
  if ( code_nul(c1) && code_nul(c2) )
    line(s->pi.x,s->pi.y,s->pf.x,s->pf.y,c) ;
  *s = se ;
}

void CALLBACK display() {
  float noir[] = { 0.0F,0.0F,0.0F,1.0F };
  float rouge[] = { 1.0F,0.0F,0.0F,1.0F };
  float vert[] = { 0.0F,1.0F,0.0F,1.0F };
  float bleu[] = { 0.0F,0.0F,1.0F,1.0F } ;
  float jaune[] = { 1.0F,1.0F,0.0F,1.0F };
  float cyan[] = { 0.0F,1.0F,1.0F,1.0F };
  float magenta[] = { 1.0F,0.0F,1.0F,1.0F };
  float blanc[] = { 1.0F,1.0F,1.0F,1.0F };
  rectangle r = { -17,-13,16,11 } ;
  segment s1 = { -20,-18,6,1 } ;
  segment s2 = { -25,3,13,18 } ;
  segment s3 = { 23,13,27,-17 } ;
  segment s4 = { 13,8,-11,3 } ;
  segment s5 = { 23,-9,11,-18 } ;
  segment s6 = { 16,-7,16,19 } ;
  segment s7 = { -6,-13,9,-13 } ;
  glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  glPushMatrix();
  switch ( aff) {
    case 0 : traceRectangle(&r,bleu) ;
             line(s1.pi.x,s1.pi.y,s1.pf.x,s1.pf.y,vert) ;
             line(s2.pi.x,s2.pi.y,s2.pf.x,s2.pf.y,rouge) ;
             line(s3.pi.x,s3.pi.y,s3.pf.x,s3.pf.y,jaune) ;
             line(s4.pi.x,s4.pi.y,s4.pf.x,s4.pf.y,noir) ;
             line(s5.pi.x,s5.pi.y,s5.pf.x,s5.pf.y,blanc) ;
             line(s6.pi.x,s6.pi.y,s6.pf.x,s6.pf.y,cyan) ;
             line(s7.pi.x,s7.pi.y,s7.pf.x,s7.pf.y,magenta) ;
             break ;
    case 1 : traceRectangle(&r,bleu) ;
             clipperSegment(&r,&s1,vert) ;
             clipperSegment(&r,&s2,rouge) ;
             clipperSegment(&r,&s3,jaune) ;
             clipperSegment(&r,&s4,noir) ;
             clipperSegment(&r,&s5,blanc) ;
             clipperSegment(&r,&s6,cyan) ;
             clipperSegment(&r,&s7,magenta) ;
             break ;
    case 2 : traceRectangle(&r,bleu) ;
             clipperSegment(&r,&s,rouge) ;
             pixel(s.pi.x,s.pi.y,jaune) ;
             pixel(s.pf.x,s.pf.y,jaune) ;
             break ; }
  glPopMatrix();
  glFlush();
  auxSwapBuffers() ;
}

void CALLBACK reshape(int w,int h) {
  glViewport(0,0,w,h);
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  glOrtho(-30.0,30.0,-20.0,20.0,-40.0,40.0);
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
}

void CALLBACK auxKeyReturn() {
  aff++ ;
  if ( aff == 3 )
    aff = 0 ;
}

void CALLBACK auxKeyEspace() {
  p1 = !p1 ;
}

void CALLBACK auxKeyUp() {
  if ( p1 ) {
    s.pi.y++ ;
    if ( s.pi.y > 20 )
      s.pi.y = 20 ; } 
    else {
    s.pf.y++ ;
    if ( s.pf.y > 20 )
      s.pf.y = 20 ; } 
}

void CALLBACK auxKeyDown() {
  if ( p1 ) {
    s.pi.y-- ;
    if ( s.pi.y < -20 )
      s.pi.y = -20 ; } 
    else {
    s.pf.y-- ;
    if ( s.pf.y < -20 )
      s.pf.y = -20 ; } 
}

void CALLBACK auxKeyRight() {
  if ( p1 ) {
    s.pi.x++ ;
    if ( s.pi.x > 30 )
      s.pi.x = 30 ; } 
    else {
    s.pf.x++ ;
    if ( s.pf.x > 30 )
      s.pf.x = 30 ; } 
}

void CALLBACK auxKeyLeft() {
  if ( p1 ) {
    s.pi.x-- ;
    if ( s.pi.x < -30 )
      s.pi.x = -30 ; } 
    else {
    s.pf.x-- ;
    if ( s.pf.x < -30 )
      s.pf.x = -30 ; } 
}

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

void main(void) {
  auxInitDisplayMode(AUX_DOUBLE|AUX_RGBA|AUX_DEPTH);
  auxInitPosition (0,0,300,200);
  auxInitWindow("Clipping par Cohen-Sutherland") ;
  init();
  auxKeyFunc(AUX_RETURN,auxKeyReturn) ;
  auxKeyFunc(AUX_SPACE,auxKeyEspace) ;
  auxKeyFunc(AUX_UP,auxKeyUp) ;
  auxKeyFunc(AUX_DOWN,auxKeyDown) ;
  auxKeyFunc(AUX_LEFT,auxKeyLeft) ;
  auxKeyFunc(AUX_RIGHT,auxKeyRight) ;
  auxReshapeFunc(reshape);
  auxMainLoop(display);
}
WB01624_.gif (281 octets) RETOUR