Exemplo n.º 1
0
  /**
   * Return nearest point from point "from" following supplied orientation
   *
   * <p>Returns null if no intersection was found
   *
   * @param from point from which we are coming to area
   * @param orientation orientation we are coming from
   * @return
   */
  @Override
  public FGEPoint nearestPointFrom(FGEPoint from, SimplifiedCardinalDirection orientation) {
    FGEHalfLine hl = FGEHalfLine.makeHalfLine(from, orientation);

    FGEArea intersection = computeLineIntersection(hl);
    if (intersection instanceof FGEEmptyArea) {
      return null;
    } else if (intersection instanceof FGEPoint) {
      return (FGEPoint) intersection;
    } else if (intersection instanceof FGEUnionArea) {
      double minimalDistanceSq = java.lang.Double.POSITIVE_INFINITY;
      FGEPoint returned = null;
      for (FGEArea a : ((FGEUnionArea) intersection).getObjects()) {
        if (a instanceof FGEPoint) {
          double distSq = FGEPoint.distanceSq(from, (FGEPoint) a);
          if (distSq < minimalDistanceSq) {
            returned = (FGEPoint) a;
            minimalDistanceSq = distSq;
          }
        }
      }
      return returned;
    } else if (intersection instanceof FGESegment) {
      FGEPoint p1, p2;
      p1 = ((FGESegment) intersection).getP1();
      p2 = ((FGESegment) intersection).getP2();
      if (FGEPoint.distanceSq(from, p1) < FGEPoint.distanceSq(from, p2)) {
        return p1;
      } else {
        return p2;
      }
    } else {
      logger.warning("Unexpected area: " + intersection);
      return null;
    }

    /*if (getNorthWestRoundBounds().contains(returned)) {
    	//System.out.println("outlineIntersect() in NW");
    	return getNorthWestRound().nearestPointFrom(returned,orientation);
    }

    if (getSouthWestRoundBounds().contains(returned)) {
    	//System.out.println("outlineIntersect() in SW");
    	return getSouthWestRound().nearestPointFrom(returned,orientation);
    }

    if (getNorthEastRoundBounds().contains(returned)) {
    	//System.out.println("outlineIntersect() in NE");
    	return getNorthEastRound().nearestPointFrom(returned,orientation);
    }


    if (getSouthEastRoundBounds().contains(returned)) {
    	//System.out.println("outlineIntersect() in SE");
    	return getSouthEastRound().nearestPointFrom(returned,orientation);
    }*/

    // return returned;
  }