Ejemplo n.º 1
0
  public Polygon scalePolygon(Polygon polygon) {
    int xs[] = new int[polygon.npoints];
    int ys[] = new int[polygon.npoints];
    math.geom2d.Point2D p;
    math.geom2d.Point2D p1;
    int sumX = 0;
    int sumY = 0;

    for (int i = 0; i < polygon.npoints; i++) {
      p = new math.geom2d.Point2D(polygon.xpoints[i], polygon.ypoints[i]);
      p1 = p.scale(0.5);
      sumX += p1.getX();
      sumY += p1.getY();

      xs[i] = (int) p1.getX();
      ys[i] = (int) p1.getY();
      p.clone();
    }

    Polygon poly = new Polygon(xs, ys, polygon.npoints);
    poly.translate(sumX / polygon.npoints, sumY / polygon.npoints);

    Polygon scalePolygon = new Polygon();

    for (int i = 0; i < poly.npoints; i++) {
      p = new math.geom2d.Point2D(poly.xpoints[i], poly.ypoints[i]);
      if (i + 1 < poly.npoints) {
        if (p.distance(poly.xpoints[i + 1], poly.ypoints[i + 1]) > (0.1 * world.getMapWidth())) {
          scalePolygon.addPoint(poly.xpoints[i], poly.ypoints[i]);
        } else {
          continue;
        }
      } else if (i + 1 == poly.npoints) {
        if (p.distance(poly.xpoints[0], poly.ypoints[0]) > (0.1 * world.getMapWidth())) {
          scalePolygon.addPoint(poly.xpoints[i], poly.ypoints[i]);
        } else {
          continue;
        }
      }
    }

    return scalePolygon;
  }
Ejemplo n.º 2
0
  /**
   * This function scales a polygon by the scale coefficient
   *
   * @param sourcePolygon : Is the Polygon that we want to scale
   * @param scale : Is the scale coefficient, It actually multiplies to the points and makes the new
   *     shape
   * @return : returns the scaled polygon which, its center is on the center of the last polygon
   */
  protected Polygon scalePolygon(Polygon sourcePolygon, double scale) {
    Polygon scaledPolygon;

    int xs[] = new int[sourcePolygon.npoints];
    int ys[] = new int[sourcePolygon.npoints];
    Point2D p, p1;
    int sumX = 0;
    int sumY = 0;

    for (int i = 0; i < sourcePolygon.npoints; i++) {
      p = new Point2D(sourcePolygon.xpoints[i], sourcePolygon.ypoints[i]);
      p1 = p.scale(scale);
      sumX += p1.getX();
      sumY += p1.getY();
      xs[i] = (int) p1.getX();
      ys[i] = (int) p1.getY();
      p.clone();
    }

    Polygon preScaledPolygon = new Polygon(xs, ys, sourcePolygon.npoints);
    scaledPolygon = reAllocatePolygon(preScaledPolygon, sourcePolygon);
    if (scaledPolygon == null) scaledPolygon = preScaledPolygon;
    return scaledPolygon;
  }