Java check if two rectangles overlap at any point

后端 未结 9 2251
醉梦人生
醉梦人生 2020-11-29 05:12

I have multiple rectangles and one special rectangle: the selection rect. I want to check for each rectangle if the rectangle contains at least one point which is inside the

9条回答
  •  隐瞒了意图╮
    2020-11-29 05:48

    I have a generic implemententation for polygons in the gps coordinate system, which may be a bit overkill for rectangles (which are simple polygons); but it will work. It should be fairly straightforward to adapt the approach to your usecase if for whatever reason you don't want to use AWT.

    https://github.com/jillesvangurp/geogeometry/blob/master/src/main/java/com/jillesvangurp/geo/GeoGeometry.java#L753 (overlap method)

    What I do there is simply check if the polygons have any points that are contained by the other polygon.

    For polygon containment of points, I have a simple algorithm that walks the edges of the polygon to check if the point is inside or outside O(n). For rectangles it should be cheap to run.

    The nice thing about this approach it will work for any rectangles and also rotated rectangles or more complex shapes.

提交回复
热议问题