Back to index

wims  3.65+svn20090927
Polygone.java
Go to the documentation of this file.
00001 // Polygone.java - 12/12/98
00002 
00003 package geo ;
00004 
00005 import java.awt.Graphics ;
00006 import java.awt.Polygon ;
00007 
00012 public class Polygone extends ObjetRepere
00013 {
00014 
00015   public double xi [] ;
00016   public double yi [] ;
00017 
00022   public boolean plein ;
00023 
00024   public Polygon p ;
00025 
00030   public Polygone (String Nom, Repere R)
00031   { super (Nom, R) ; plein = false ; }
00032 
00037   public Polygone (pt Ai [], int n, boolean type, String Nom, Repere R)
00038   { super (Nom, R) ; Polygone (Ai, n, type) ; }
00039 
00044   public Polygone (double xi [], double yi [], int n, boolean type, String Nom, Repere R)
00045   { super (Nom, R) ; Polygone (xi, yi, n, type) ; }
00046 
00051   public void trace (Graphics g)
00052   { if (defini) if (plein) g.fillPolygon (p) ; else g.drawPolygon (p) ;
00053         if ((defini) && (Nom.length () != 0) && (p.npoints > 0))
00054           g.drawString (Nom, p.xpoints [0] + 2 , p.ypoints [0] + 2) ;
00055   }
00056 
00061    public boolean zone (int X, int Y)
00062    { boolean resultat = false ;
00063      if (defini) for (int i = 0 ; i < p.npoints ; i ++)
00064        { int AX = p.xpoints [i] ;
00065          int AY = p.xpoints [i] ;
00066          int BX = (i == p.npoints - 1) ? p.xpoints [0] : p.xpoints [i + 1] ;
00067          int BY = (i == p.npoints - 1) ? p.ypoints [0] : p.ypoints [i + 1] ;
00068          int A = BY - AY ; int B = AX - BX ;
00069          double d = ((double) Math.abs (A * X + B * Y + AY * BX - AX * BY)) / Math.sqrt ((double) (A * A + B * B)) ;
00070          if (resultat = ((d <= 4.0) && ((X - AX) * (X - BX) <= 0) && ((Y - AY) * (Y - BY) <= 0))) break ;
00071        }
00072      return resultat ;
00073    }
00074 
00079   public void Polygone (pt Ai [], int n, boolean type)
00080   { defini = true ;
00081     for (int i = 0 ; (i < n) && defini ; i ++) defini = defini && Ai [i].defini ;
00082     if (defini)
00083       { if ((xi == null) || (n != xi.length))
00084           { xi = new double [n] ;
00085             yi = new double [n] ;
00086           }
00087         for (int i = 0 ; i < n ; i ++)
00088           { xi [i] = Ai [i].x ;
00089             yi [i] = Ai [i].y ;
00090           }
00091         maj (n) ;
00092         plein = type ;
00093       }
00094   }
00095 
00096   private void maj (int n)
00097   { if ((p == null) || (p.npoints != n + 1))
00098       { p = new Polygon () ;
00099         for (int i = 0 ; i < n ; i ++) p.addPoint (R.Iabs (xi [i]), R.Iord (yi [i])) ;
00100         p.addPoint (R.Iabs (xi [0]), R.Iord (yi [0])) ;
00101       }
00102     else 
00103       { for (int i = 0 ; i < n ; i ++)
00104           { p.xpoints [i] = R.Iabs (xi [i]) ;
00105             p.ypoints [i] = R.Iord (yi [i]) ;
00106           }
00107         p.xpoints [n] = R.Iabs (xi [0]) ;
00108         p.ypoints [n] = R.Iord (yi [0]) ;
00109       }
00110   }
00111 
00116   public void Polygone (double xi [], double yi [], int n, boolean type)
00117   { defini = true ;
00118     if ((this.xi == null) || (n != xi.length))
00119       { this.xi = new double [n] ;
00120         this.yi = new double [n] ;
00121       }
00122     for (int i = 0 ; i < n ; i ++)
00123       { this.xi [i] = xi [i] ;
00124         this.yi [i] = yi [i] ;
00125       }
00126     maj (n) ;
00127     plein = type ;
00128   }
00129 
00130 }