home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1998 January / PCO0198.ISO / 1&1 / java.z / java_301 / java / awt / Polygon.class (.txt) < prev    next >
Encoding:
Java Class File  |  1996-10-20  |  2.4 KB  |  108 lines

  1. package java.awt;
  2.  
  3. public class Polygon {
  4.    public int npoints;
  5.    public int[] xpoints = new int[4];
  6.    public int[] ypoints = new int[4];
  7.    Rectangle bounds;
  8.  
  9.    public Polygon() {
  10.    }
  11.  
  12.    public Polygon(int[] xpoints, int[] ypoints, int npoints) {
  13.       this.npoints = npoints;
  14.       this.xpoints = new int[npoints];
  15.       this.ypoints = new int[npoints];
  16.       System.arraycopy(xpoints, 0, this.xpoints, 0, npoints);
  17.       System.arraycopy(ypoints, 0, this.ypoints, 0, npoints);
  18.    }
  19.  
  20.    void calculateBounds(int[] xpoints, int[] ypoints, int npoints) {
  21.       int boundsMinX = Integer.MAX_VALUE;
  22.       int boundsMinY = Integer.MAX_VALUE;
  23.       int boundsMaxX = Integer.MIN_VALUE;
  24.       int boundsMaxY = Integer.MIN_VALUE;
  25.  
  26.       for(int i = 0; i < npoints; ++i) {
  27.          int x = xpoints[i];
  28.          boundsMinX = Math.min(boundsMinX, x);
  29.          boundsMaxX = Math.max(boundsMaxX, x);
  30.          int y = ypoints[i];
  31.          boundsMinY = Math.min(boundsMinY, y);
  32.          boundsMaxY = Math.max(boundsMaxY, y);
  33.       }
  34.  
  35.       this.bounds = new Rectangle(boundsMinX, boundsMinY, boundsMaxX - boundsMinX, boundsMaxY - boundsMinY);
  36.    }
  37.  
  38.    void updateBounds(int x, int y) {
  39.       this.bounds.x = Math.min(this.bounds.x, x);
  40.       this.bounds.width = Math.max(this.bounds.width, x - this.bounds.x);
  41.       this.bounds.y = Math.min(this.bounds.y, y);
  42.       this.bounds.height = Math.max(this.bounds.height, y - this.bounds.y);
  43.    }
  44.  
  45.    public void addPoint(int x, int y) {
  46.       if (this.npoints == this.xpoints.length) {
  47.          int[] tmp = new int[this.npoints * 2];
  48.          System.arraycopy(this.xpoints, 0, tmp, 0, this.npoints);
  49.          this.xpoints = tmp;
  50.          tmp = new int[this.npoints * 2];
  51.          System.arraycopy(this.ypoints, 0, tmp, 0, this.npoints);
  52.          this.ypoints = tmp;
  53.       }
  54.  
  55.       this.xpoints[this.npoints] = x;
  56.       this.ypoints[this.npoints] = y;
  57.       ++this.npoints;
  58.       if (this.bounds != null) {
  59.          this.updateBounds(x, y);
  60.       }
  61.  
  62.    }
  63.  
  64.    public Rectangle getBoundingBox() {
  65.       if (this.bounds == null) {
  66.          this.calculateBounds(this.xpoints, this.ypoints, this.npoints);
  67.       }
  68.  
  69.       return this.bounds;
  70.    }
  71.  
  72.    public boolean inside(int x, int y) {
  73.       if (this.getBoundingBox().inside(x, y)) {
  74.          int hits = 0;
  75.  
  76.          for(int i = 0; i < this.npoints; ++i) {
  77.             int j = (i + 1) % this.npoints;
  78.             int dx = this.xpoints[j] - this.xpoints[i];
  79.             int dy = this.ypoints[j] - this.ypoints[i];
  80.             if (dy != 0) {
  81.                int rx = x - this.xpoints[i];
  82.                int ry = y - this.ypoints[i];
  83.                if (this.ypoints[i] == y) {
  84.                   --ry;
  85.                }
  86.  
  87.                if (this.ypoints[j] == y) {
  88.                   ++dy;
  89.                }
  90.  
  91.                float s = (float)ry / (float)dy;
  92.                if ((double)s >= (double)0.0F && (double)s <= (double)1.0F && (int)(s * (float)dx) > rx) {
  93.                   ++hits;
  94.                }
  95.             }
  96.          }
  97.  
  98.          if (hits % 2 == 0) {
  99.             return false;
  100.          } else {
  101.             return true;
  102.          }
  103.       } else {
  104.          return false;
  105.       }
  106.    }
  107. }
  108.