
Fichier source : ClippingCohenSutherland.cpp
Tous les fichiers sources: ClippingCohenSutherland.zip
/* Segment de droite en 2D                      */
    /*                                              */
    /* Auteur: Nicolas JANEY                        */
    /* nicolas.janey@univ-fcomte.fr                 */
    /* Novembre 2012                                */
    
    #ifndef ____SEGMENT2D____
    #define ____SEGMENT2D____
    
    class Position2D;
    class Rectangle2D;
    
    class Segment2D {
    
      public :
        Position2D *p1;
        Position2D *p2;
    
      public :
    
        /* Constructeurs                            */
        Segment2D(void);
        Segment2D(Position2D *p1,Position2D *p2);
        Segment2D(Segment2D *s);
    
        /* Destructeur                              */
        ~Segment2D(void);
    
        /* Dessin OpenGL                            */
        void draw(void);
    
        /* Abscisse correspondant a une ordonnee    */
        double abscisse(double ordonnee);
    
        /* Ordonnee correspondant a une abscisse    */
        double ordonnee(double abscisse);
    
        /* Clipping de Cohen-Sutherland             */
        int clip(Rectangle2D *r);
    };
    
    #endif
    
Fichier source : Segment2D.cpp
/* Abscisse correspondant a une ordonnee        */
    
    double Segment2D::abscisse(double ordonnee) {
      if ( p1->c[0] == p2->c[0] )
        return(p1->c[0]);
      double a = (p1->c[1]-p2->c[1])/(p1->c[0]-p2->c[0]);
      double b = p1->c[1]-a*p1->c[0];
      return((ordonnee-b)/a);
    }
    
    /* Ordonnee correspondant a une abscisse        */
    
    double Segment2D::ordonnee(double abscisse) {
      double a = (p1->c[1]-p2->c[1])/(p1->c[0]-p2->c[0]);
      double b = p1->c[1]-a*p1->c[0];
      return(a*abscisse+b);
    }
    
    /* Clipping de Cohen-Sutherland                 */
    
    int Segment2D::clip(Rectangle2D *r){
      int c1 = r->code(p1);
      int c2 = r->code(p2);
      while ( ( ( c1 != 0 ) || ( c2 != 0 ) ) &&
              ( (c1&c2) == 0 ) ) {
        if ( c1 == 0 ) {
          { Position2D *aux = p1;
            p1 = p2;
            p2 = aux; }
          { int aux = c1;
            c1 = c2;
            c2 = aux; } }
        if ( (c1&1) != 0 ) {
          p1->c[1] = ordonnee(r->ig->c[0]);
          p1->c[0] = r->ig->c[0]; }
          else
          if ( (c1&2) != 0 ) {
            p1->c[1] = ordonnee(r->sd->c[0]);
            p1->c[0] = r->sd->c[0]; }
            else
            if ( (c1&4) != 0 ) {
              p1->c[0] = abscisse(r->ig->c[1]);
              p1->c[1] = r->ig->c[1]; }
              else {
              p1->c[0] = abscisse(r->sd->c[1]);
              p1->c[1] = r->sd->c[1]; }
        c1 = r->code(p1); }
      return(( c1 == 0 ) && ( c2 == 0 ));
    }
Fichier source : Rectangle2D.h
/* Rectangle en 2D                              */
    /*                                              */
    /* Auteur: Nicolas JANEY                        */
    /* nicolas.janey@univ-fcomte.fr                 */
    /* Novembre 2012                                */
    
    #ifndef ____RECTANGLE2D____
    #define ____RECTANGLE2D____
    
    class Position2D;
    
    class Rectangle2D {
    
      public :
        Position2D *ig;
        Position2D *sd;
    
      public :
    
        /* Constructeurs                            */
        Rectangle2D(void);
        Rectangle2D(Position2D *p1,Position2D *p2);
        Rectangle2D(Rectangle2D *r);
    
        /* Destructeur                              */
        ~Rectangle2D(void);
    
        /* Dessin OpenGL                            */
        void draw(void);
    
        /* Code de Cohen-Sutherland                 */
        /* d'une Position2D                         */
        int code(Position2D *p);
    };
    
    #endif
    
Fichier source : Rectangle2D.cpp
/* Code de Cohen-Sutherland d'une Position2D    */
    
    int Rectangle2D::code(Position2D *p) {
      int cd = 0;
      if ( p->c[0] < ig->c[0] )
        cd += 1;
      if ( p->c[0] > sd->c[0] )
        cd += 2;
      if ( p->c[1] < ig->c[1] )
        cd += 4;
      if ( p->c[1] > sd->c[1] )
        cd += 8;
      return(cd);
    }