/** * 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; }
@Override public FGEPoint getNearestPoint(FGEPoint aPoint) { if (getIsFilled() && containsPoint(aPoint)) { return aPoint.clone(); } return nearestOutlinePoint(aPoint); }
@Override public FGEPoint nearestOutlinePoint(FGEPoint aPoint) { FGEPoint returned = null; double distSq, minimalDistanceSq = java.lang.Double.POSITIVE_INFINITY; FGESegment north = getNorth(); FGESegment south = getSouth(); FGESegment east = getEast(); FGESegment west = getWest(); FGEPoint p = north.getNearestPointOnSegment(aPoint); distSq = FGEPoint.distanceSq(p, aPoint); if (distSq < minimalDistanceSq) { returned = p; minimalDistanceSq = distSq; } p = south.getNearestPointOnSegment(aPoint); distSq = FGEPoint.distanceSq(p, aPoint); if (distSq < minimalDistanceSq) { returned = p; minimalDistanceSq = distSq; } p = east.getNearestPointOnSegment(aPoint); distSq = FGEPoint.distanceSq(p, aPoint); if (distSq < minimalDistanceSq) { returned = p; minimalDistanceSq = distSq; } p = west.getNearestPointOnSegment(aPoint); distSq = FGEPoint.distanceSq(p, aPoint); if (distSq < minimalDistanceSq) { returned = p; minimalDistanceSq = distSq; } if (returned == null) { returned = new FGEPoint(0, 0); } if (getNorthWestRoundBounds().containsPoint(returned)) { // System.out.println("nearestOutlinePoint() in NW"); return getNorthWestRound().nearestOutlinePoint(aPoint); } if (getSouthWestRoundBounds().containsPoint(returned)) { // System.out.println("nearestOutlinePoint() in SW"); return getSouthWestRound().nearestOutlinePoint(aPoint); } if (getNorthEastRoundBounds().containsPoint(returned)) { // System.out.println("nearestOutlinePoint() in NE"); return getNorthEastRound().nearestOutlinePoint(aPoint); } if (getSouthEastRoundBounds().containsPoint(returned)) { // System.out.println("nearestOutlinePoint() in SE"); return getSouthEastRound().nearestOutlinePoint(aPoint); } return returned; }