/* Auteur: Nicolas JANEY */ /* nicolas.janey@univ-fcomte.fr */ /* Novembre 2006 */ public class Rectangle { public Position ig; public Position sd; public Rectangle() { ig = new Position(); sd = new Position(); } public Rectangle(float xmin,float ymin,float xmax,float ymax) { ig = new Position(xmin,ymin); sd = new Position(xmax,ymax); } public Rectangle(Position ig,Position sd) { this.ig = new Position(ig); this.sd = new Position(sd); } public Rectangle(Rectangle r) { ig = new Position(r.ig); sd = new Position(r.sd); } private int codeZone(Position p) { int cz = ( p.x < ig.x ) ? 0x01 : 0x00; if ( p.x > sd.x ) cz += 0x02; if ( p.y < ig.y ) cz += 0x04; if ( p.y > sd.y ) cz += 0x08; return(cz); } public Segment clip(Segment s) { s = new Segment(s); int ci = codeZone(s.pi); int cf = codeZone(s.pf); while ( ( ( ci != 0 ) || ( cf !=0 ) ) && ( (ci&cf) == 0 ) ) { if ( ci == 0 ) { ci = cf; cf = 0; Position p = s.pi; s.pi = s.pf; s.pf = p; } if ( (ci & 0x0001) != 0 ) { s.pi.y = s.ordonnee(ig.x); s.pi.x = ig.x; } else if ( (ci & 0x0002) != 0 ) { s.pi.y = s.ordonnee(sd.x); s.pi.x = sd.x; } else if ( (ci & 0x0004) != 0 ) { s.pi.x = s.abscisse(ig.y); s.pi.y = ig.y; } else { s.pi.x = s.abscisse(sd.y); s.pi.y = sd.y; } ci = codeZone(s.pi); } return((ci == 0 ) ? s : null); } public String toString() { return("["+ig+","+sd+"]"); } }