Example #1
0
  public int geometryTest(Point2D worldCoord, int tolerance) {
    shapeList = model.getShapes();
    if (selectedShape != null) {
      if (doHandleCheck(worldCoord, selectedShape)) {
        return selectedIndex;
      }
    }

    Point2D.Double objCoord = new Point2D.Double();
    List<Shape> reversed = model.getShapesReversed();

    for (int i = 0; i < reversed.size(); i++) {
      Shape s = reversed.get(i);
      AffineTransform worldToObj =
          new AffineTransform(
              Math.cos(s.getRotation()),
              -Math.sin(s.getRotation()),
              Math.sin(s.getRotation()),
              Math.cos(s.getRotation()),
              0,
              0);
      worldToObj.concatenate(
          new AffineTransform(1, 0, 0, 1, -s.getCenter().getX(), -s.getCenter().getY()));
      worldToObj.transform(worldCoord, objCoord);
      if (s instanceof Line) {
        Line l = (Line) s;
        Point2D.Double d = new Point2D.Double();
        double x1 = l.getEnd().getX() - l.getCenter().getX();
        double y1 = l.getEnd().getY() - l.getCenter().getY();
        double lineLength = Math.sqrt((x1) * (x1) + (y1) * (y1));
        d.setLocation((x1) / lineLength, (y1) / lineLength);
        double t = (objCoord.getX()) * d.getX() + (objCoord.getY()) * d.getY();
        Point2D.Double q = new Point2D.Double();
        q.setLocation(t * d.getX(), t * d.getY());
        double qdist =
            Math.sqrt(
                (objCoord.getX() - q.getX()) * (objCoord.getX() - q.getX())
                    + (objCoord.getY() - q.getY()) * (objCoord.getY() - q.getY()));
        tolerance = (int) (tolerance * this.getScale());
        if (qdist <= tolerance && t >= -tolerance && t <= lineLength + tolerance) {
          selectedShape = l;
          selectedIndex = shapeList.size() - i - 1;
          setChanged();
          notifyObservers();
          return selectedIndex;
        }
      } else if (s instanceof Square) {
        Square sq = (Square) s;
        if (Math.abs(objCoord.getX()) < sq.getSize() / 2
            && Math.abs(objCoord.getY()) < sq.getSize() / 2) {
          selectedShape = sq;
          selectedIndex = shapeList.size() - i - 1;
          setChanged();
          notifyObservers();
          return selectedIndex;
        }
      } else if (s instanceof Rectangle) {
        Rectangle r = (Rectangle) s;
        if (Math.abs(objCoord.getX()) < r.getWidth() / 2
            && Math.abs(objCoord.getY()) < r.getHeight() / 2) {
          selectedShape = r;
          selectedIndex = shapeList.size() - i - 1;
          setChanged();
          notifyObservers();
          return selectedIndex;
        }
      } else if (s instanceof Circle) {
        Circle c = (Circle) s;
        if (objCoord.getX() * objCoord.getX() + objCoord.getY() * objCoord.getY()
            < (c.getRadius() * c.getRadius())) {
          selectedShape = c;
          selectedIndex = shapeList.size() - i - 1;
          setChanged();
          notifyObservers();
          return selectedIndex;
        }
      } else if (s instanceof Ellipse) {
        Ellipse el = (Ellipse) s;
        double a = el.getWidth() / 2;
        double b = el.getHeight() / 2;
        if ((objCoord.getX() * objCoord.getX()) / (a * a)
                + (objCoord.getY() * objCoord.getY()) / (b * b)
            <= 1) {
          selectedShape = el;
          selectedIndex = shapeList.size() - i - 1;
          setChanged();
          notifyObservers();
          return selectedIndex;
        }
      } else if (s instanceof Triangle) {
        Triangle t = (Triangle) s;
        Point2D.Double a =
            new Point2D.Double(
                t.getA().getX() - t.getCenter().getX(), t.getA().getY() - t.getCenter().getY());
        Point2D.Double b =
            new Point2D.Double(
                t.getB().getX() - t.getCenter().getX(), t.getB().getY() - t.getCenter().getY());
        Point2D.Double c =
            new Point2D.Double(
                t.getC().getX() - t.getCenter().getX(), t.getC().getY() - t.getCenter().getY());

        double triArea = calcArea(a, b, c);
        double a1 = calcArea(objCoord, b, c);
        double a2 = calcArea(objCoord, a, c);
        double a3 = calcArea(objCoord, a, b);

        if (a1 + a2 + a3 <= triArea) {
          selectedShape = t;
          selectedIndex = shapeList.size() - i - 1;
          setChanged();
          notifyObservers();
          return selectedIndex;
        }
      } else {
        selectedShape = null;
        selectedIndex = -1;
        setChanged();
        notifyObservers();
      }
    }
    selectedShape = null;
    selectedIndex = -1;
    setChanged();
    notifyObservers();
    return -1;
  }