/* Une classe de stockage de rectangle */ /* a bords horizontaux et verticaux */ /* */ /* Auteur: Nicolas JANEY */ /* nicolas.janey@univ-fcomte.fr */ /* Novembre 2009 */ public class Rectangle { public Position2D ig; public Position2D sd; public Rectangle() { ig = new Position2D(); sd = new Position2D(); } public Rectangle(float xmin,float ymin,float xmax,float ymax) { ig = new Position2D(xmin,ymin); sd = new Position2D(xmax,ymax); } public Rectangle(Position2D ig,Position2D sd) { this.ig = new Position2D(ig); this.sd = new Position2D(sd); } public Rectangle(Rectangle r) { ig = new Position2D(r.ig); sd = new Position2D(r.sd); } private int codeZone(Position2D p) { int cz = ( p.x < ig.x ) ? 0x01 : 0x00; if ( p.x > sd.x ) cz += 0x02; if ( p.y < ig.y ) cz += 0x04; else 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 ) { int c = ci; ci = cf; cf = c; Position2D p = s.pi; s.pi = s.pf; s.pf = p; } if ( (ci & 0x01) != 0 ) { s.pi.y = s.ordonnee(ig.x); s.pi.x = ig.x; } else if ( (ci & 0x02) != 0 ) { s.pi.y = s.ordonnee(sd.x); s.pi.x = sd.x; } else if ( (ci & 0x04) != 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+"]"); } }