/**
   * Checks if a point is inside the polygon shape.
   *
   * @param fShape
   * @param fPoint
   * @return true if inside, false if on border or outside
   */
  public static boolean contains(FreedomShape fShape, FreedomPoint fPoint) {
    ArrayList<Float> lx = new ArrayList<Float>();
    ArrayList<Float> ly = new ArrayList<Float>();
    int verticesNum = 0;
    float px = fPoint.getX();
    float py = fPoint.getY();

    if (fShape instanceof FreedomPolygon) {
      FreedomPolygon poly = (FreedomPolygon) fShape;
      verticesNum = poly.getPoints().size();

      for (int i = 0; i < poly.getPoints().size(); i++) {
        lx.add((float) poly.getPoints().get(i).getX());
        ly.add((float) poly.getPoints().get(i).getY());
      }
    }

    // TODO: converting: change this code please
    float[] x = new float[lx.size()];

    for (int i = 0; i < lx.size(); i++) {
      Float f = lx.get(i);
      x[i] = ((f != null) ? f : Float.NaN); // Or whatever default you want.
    }

    // TODO: converting: change this code please
    float[] y = new float[ly.size()];

    for (int i = 0; i < ly.size(); i++) {
      Float f = ly.get(i);
      y[i] = ((f != null) ? f : Float.NaN); // Or whatever default you want.
    }

    // algorithm starts
    if (verticesNum < 3) {
      return false;
    }

    boolean oddNodes = false;
    float x2 = x[verticesNum - 1];
    float y2 = y[verticesNum - 1];
    float x1;
    float y1;

    for (int i = 0; i < verticesNum; x2 = x1, y2 = y1, ++i) {
      x1 = x[i];
      y1 = y[i];

      if (((y1 < py) && (y2 >= py)) || ((y1 >= py) && (y2 < py))) {
        if (((py - y1) / (y2 - y1) * (x2 - x1)) < (px - x1)) {
          oddNodes = !oddNodes;
        }
      }
    }

    return oddNodes;
  }
  private static FreedomPolygon translate(FreedomPolygon input, int xoffset, int yoffset) {
    FreedomPolygon output = new FreedomPolygon();

    for (FreedomPoint point : input.getPoints()) {
      output.append(point.getX() + xoffset, point.getY() + yoffset);
    }

    return output;
  }
  private static Polygon convertToAWT(FreedomPolygon input) {
    FreedomPolygon polygon = (FreedomPolygon) input;
    Polygon output = new Polygon();

    for (FreedomPoint point : polygon.getPoints()) {
      output.addPoint(point.getX(), point.getY());
    }

    return output;
  }
Esempio n. 4
0
  /**
   * @param object
   * @return
   */
  @Override
  public boolean equals(Object object) {
    if (object instanceof FreedomPoint) {
      FreedomPoint point = (FreedomPoint) object;

      if ((point.getX() == this.getX()) && (point.getY() == this.getY())) {
        return true;
      } else {
        return false;
      }
    }

    return false;
  }
  private static FreedomPoint getRectangleCenter(FreedomPolygon input) {
    FreedomPoint min = input.getPoints().get(0);
    FreedomPoint max = input.getPoints().get(2);
    int x = ((max.getX() - min.getX()) / 2) + min.getX();
    int y = ((max.getY() - (min.getY() / 2)) + min.getY());

    return new FreedomPoint(x, y);
  }
  private static FreedomPolygon getBoundingBox(FreedomPolygon input) {
    int minx = Integer.MAX_VALUE;
    int miny = Integer.MAX_VALUE;
    int maxx = Integer.MIN_VALUE;
    int maxy = Integer.MIN_VALUE;

    for (FreedomPoint p : input.getPoints()) {
      minx = Math.min(minx, p.getX());
      miny = Math.min(miny, p.getY());
      maxx = Math.max(maxx, p.getX());
      maxy = Math.max(maxy, p.getY());
    }

    FreedomPolygon poly = new FreedomPolygon();
    poly.append(minx, miny);
    poly.append(maxx, miny);
    poly.append(maxx, maxy);
    poly.append(minx, maxy);

    return poly;
  }
  /**
   * taken from
   * http://stackoverflow.com/questions/10533403/how-to-rotate-a-polygon-around-a-point-with-java
   * all credits to respective authors
   */
  private static FreedomPoint rotatePoint(FreedomPoint pt, FreedomPoint pivot, double degrees) {
    double radians = Math.toRadians(degrees);
    double cosAngle = Math.cos(radians);
    double sinAngle = Math.sin(radians);

    int x =
        (int)
            Math.round(
                pivot.getX()
                    + (double)
                        (((pt.getX() - pivot.getX()) * cosAngle)
                            - ((pt.getY() - pivot.getY()) * sinAngle)));
    int y =
        (int)
            Math.round(
                pivot.getY()
                    + (double)
                        (((pt.getX() - pivot.getX()) * sinAngle)
                            + ((pt.getY() - pivot.getY()) * cosAngle)));

    return new FreedomPoint(x, y);
  }
 private static Point convertToAWT(FreedomPoint fPoint) {
   return new Point(fPoint.getX(), fPoint.getY());
 }