If this is to be implemented in JavaFX, then it's better to do (not tested):

1. Extract the intersection computation from Shape.intersect

private static Area intersectionArea(Shape shape1, Shape shape2) {
    Area result = shape1.getTransformedArea();
    return result.intersect(shape2.getTransformedArea());
}

2.  Shape.intersect becomes

public static Shape Shape.intersects(Shape shape1, Shape shape2) {
    var intersectionArea = intersectionArea(Shape shape1, Shape shape2);
    return createFromGeomShape(intersectionArea);
}

3. Add the new method Shape.intersects

public static boolean Shape.intersects( Shape shape1, Shape shape2) {
    var intersectionArea = intersectionArea(Shape shape1, Shape shape2);
    return !intersectionArea.isEmpty();
}

Regardless, I wonder why the geometry methods were implemented as static
methods. Why not shape1.intersect(shape2)? I assume the new method should
follow these, but on a clean slate I think I would have used the non-static
approach.

Another thing I would think about is whether it makes sense to just one
method or is it a part of a more comprehensive shape geometry bundle. Is
"intersects?" the only question we would like to ask?

- Nir

On Mon, Jan 18, 2021 at 12:12 PM Dirk Lemmermann <dlemmerm...@gmail.com>
wrote:

> I just noticed that there is no „intuitive“ API to check whether two
> shapes intersect with each other. The only way (I think) to do it is as
> follows:
>
> Shape.intersect((Shape) child, circle).getBoundsInLocal().getWidth() != -1
>
> If this is indeed the case I would like to propose that a method shall be
> added called „boolean Shape.intersects(Shape,Shape").
>
> See also:
> https://stackoverflow.com/questions/15013913/checking-collision-of-shapes-with-javafx
> <
> https://stackoverflow.com/questions/15013913/checking-collision-of-shapes-with-javafx
> >
>
> Dirk
>
>
>

Reply via email to