Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8090994

Shape intersection support

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Unresolved
    • Icon: P4 P4
    • tbd
    • None
    • javafx
    • None

      Currently we have Node.intersects(Bounds) which does not take the Shape geometry into account as it only accepts a Bounds object and the comparison is done against the bounds of the Node only. For Shape's this means we only get rectangular bounds intersection which is not overly useful.

      We also have a static Shape.intersect() method that merges two shapes into one, which is more useful but has a more cumbersome API and doesn't seem to work with Line. It is also less obvious when looking at the API docco, as the Node.intersect method looks like the obvious candidate.

      So a few requests:

      1. Can we add a boolean intersect(Shape, Shape) method that just returns true if the two geometries intersect

      2. Can we add some more info to Node.intersect(Node) about the existence of Shape.intersect()

      3. Can we get Shape.intersects(Shape, Shape) fixed so that calling it with a Line results in a genuine intersection.

      For reference here's the forum conversation: https://forums.oracle.com/forums/thread.jspa?threadID=2317355&tstart=0

      Here's some code that doesn't work for Line (change the hitShape to a circle and it works). Click on the screen to move the line around:

      public void start(final Stage stage) throws Exception
      {
          Group root = new Group();

          // force the group to a fixed size (I've not found a better way to do this yet?)
          Rectangle area = new Rectangle(0, 0, 800, 600);
          root.getChildren().add(area);

          final Circle circle = new Circle(200, 150, 40, Color.BLUE);
          final Rectangle circleBoundsDisplay = new Rectangle();
          circleBoundsDisplay.setFill(Color.TRANSPARENT);
          circleBoundsDisplay.setStroke(Color.GRAY);
          root.getChildren().addAll(circle, circleBoundsDisplay);

          final Circle hitShape = new Circle(10, 10, 20);
          hitShape.setStroke(Color.RED);
          final Rectangle hitShapeBoundsDisplay = new Rectangle();
          hitShapeBoundsDisplay.setFill(Color.TRANSPARENT);
          hitShapeBoundsDisplay.setStroke(Color.GRAY);
          root.getChildren().addAll(hitShape, hitShapeBoundsDisplay);

          root.setOnMouseClicked(new EventHandler<MouseEvent>()
          {
              public void handle(MouseEvent event)
              {
                  hitShape.setCenterX(event.getX());
                  hitShape.setCenterY(event.getY());

                  // show circle bounds
                  Bounds circleBounds = circle.getBoundsInParent();
                  circleBoundsDisplay.setLayoutX(circleBounds.getMinX());
                  circleBoundsDisplay.setLayoutY(circleBounds.getMinY());
                  circleBoundsDisplay.setHeight(circleBounds.getHeight());
                  circleBoundsDisplay.setWidth(circleBounds.getWidth());

                  // show hit bounds
                  Bounds hitBounds = hitShape.getBoundsInParent();
                  hitShapeBoundsDisplay.setLayoutX(hitBounds.getMinX());
                  hitShapeBoundsDisplay.setLayoutY(hitBounds.getMinY());
                  hitShapeBoundsDisplay.setHeight(hitBounds.getHeight());
                  hitShapeBoundsDisplay.setWidth(hitBounds.getWidth());

                  Shape intersect = Shape.intersect(hitShape, circle);
                  boolean intersects = intersect.getBoundsInLocal().getWidth() >= 0
                          || intersect.getBoundsInLocal().getHeight() >= 0;
                  System.out.println("Intersects: " + intersects);
                  circle.setFill(intersects ? Color.YELLOW : Color.BLUE);
              }
          });

          Scene scene = new Scene(root, 800, 600);
          stage.setScene(scene);
          stage.show();
      }

            Unassigned Unassigned
            dzwolenskjfx Daniel Zwolenski (Inactive)
            Votes:
            1 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Imported: