@Override
  public FGEArea intersect(FGEArea area) {
    if (area.containsArea(this)) {
      return this.clone();
    }
    if (containsArea(area)) {
      return area.clone();
    }
    if (area instanceof FGEAbstractLine) {
      return computeLineIntersection((FGEAbstractLine) area);
    }
    if (area instanceof FGERoundRectangle) {
      return computeRectangleIntersection((FGERoundRectangle) area);
    }
    if (area instanceof FGEHalfPlane) {
      return computeHalfPlaneIntersection((FGEHalfPlane) area);
    }

    FGEIntersectionArea returned = new FGEIntersectionArea(this, area);
    if (returned.isDevelopable()) {
      return returned.makeDevelopped();
    } else {
      return returned;
    }
  }
  @Override
  public FGEArea union(FGEArea area) {
    if (containsArea(area)) {
      return clone();
    }
    if (area.containsArea(this)) {
      return area.clone();
    }

    return new FGEUnionArea(this, area);
  }
Example #3
0
 /**
  * Compute point where supplied line intersects with shape outline trying to minimize distance
  * from "from" point
  *
  * <p>Returns null if no intersection was found
  *
  * @param aLine
  * @param from
  * @return
  */
 public final FGEPoint outlineIntersect(FGELine line, FGEPoint from) {
   FGEArea intersection = getShape().intersect(line);
   return intersection.getNearestPoint(from);
 }
  private FGEArea computeLineIntersection(FGEAbstractLine line) {
    FGERectangle rectangle = new FGERectangle(x, y, width, height, _filling);
    FGEArea returned = rectangle.intersect(line);
    if (returned instanceof FGEEmptyArea) {
      return returned;
    } else if (returned instanceof FGEPoint) {
      FGEPoint p = (FGEPoint) returned;
      if (containsPoint(p)) {
        return p;
      }
      if (getNorthEastRoundBounds().containsPoint(p)) {
        return getNorthEastRound().intersect(line);
      }
      if (getSouthEastRoundBounds().containsPoint(p)) {
        return getSouthEastRound().intersect(line);
      }
      if (getNorthWestRoundBounds().containsPoint(p)) {
        return getNorthWestRound().intersect(line);
      }
      if (getSouthWestRoundBounds().containsPoint(p)) {
        return getSouthWestRound().intersect(line);
      }
      return new FGEEmptyArea();
    } else {
      FGEPoint p1;
      FGEPoint p2;
      if (returned instanceof FGEUnionArea && ((FGEUnionArea) returned).isUnionOfPoints()) {
        p1 = (FGEPoint) ((FGEUnionArea) returned).getObjects().firstElement();
        p2 = (FGEPoint) ((FGEUnionArea) returned).getObjects().elementAt(1);
        if (containsPoint(p1) && containsPoint(p2)) {
          return returned;
        }
      } else if (returned instanceof FGESegment) {
        p1 = ((FGESegment) returned).getP1();
        p2 = ((FGESegment) returned).getP2();
        if (containsPoint(p1) && containsPoint(p2)) {
          return returned;
        }
      } else {
        logger.warning("Unexpected " + returned);
        return new FGEEmptyArea();
      }
      FGEArea newP1 = p1;
      FGEArea newP2 = p2;
      if (getNorthEastRoundBounds().containsPoint(p1)) {
        newP1 = getNorthEastRound().intersect(line);
      }
      if (getSouthEastRoundBounds().containsPoint(p1)) {
        newP1 = getSouthEastRound().intersect(line);
      }
      if (getNorthWestRoundBounds().containsPoint(p1)) {
        newP1 = getNorthWestRound().intersect(line);
      }
      if (getSouthWestRoundBounds().containsPoint(p1)) {
        newP1 = getSouthWestRound().intersect(line);
      }
      if (getNorthEastRoundBounds().containsPoint(p2)) {
        newP2 = getNorthEastRound().intersect(line);
      }
      if (getSouthEastRoundBounds().containsPoint(p2)) {
        newP2 = getSouthEastRound().intersect(line);
      }
      if (getNorthWestRoundBounds().containsPoint(p2)) {
        newP2 = getNorthWestRound().intersect(line);
      }
      if (getSouthWestRoundBounds().containsPoint(p2)) {
        newP2 = getSouthWestRound().intersect(line);
      }

      if (newP1 instanceof FGEPoint) {
        if (newP2 instanceof FGEPoint) {
          if (returned instanceof FGEUnionArea) {
            return new FGEUnionArea(newP1, newP2);
          } else if (returned instanceof FGESegment) {
            return new FGESegment((FGEPoint) newP1, (FGEPoint) newP2);
          }
        } else if (newP2 instanceof FGEEmptyArea) {
          return newP1;
        }
      } else if (newP1 instanceof FGEEmptyArea) {
        if (newP2 instanceof FGEPoint) {
          return newP2;
        } else {
          return new FGEEmptyArea();
        }
      } else if (newP1 instanceof FGEUnionArea
          && ((FGEUnionArea) newP1).isUnionOfPoints()
          && newP2 instanceof FGEUnionArea
          && ((FGEUnionArea) newP2).isUnionOfPoints()
          && newP1.equals(newP2)) {
        return newP1;
      }

      logger.warning("Unexpected " + returned + " newP1=" + newP1 + " newP2=" + newP2);
      return new FGEEmptyArea();
    }
  }