Java Utililty Methods Rectangle Intersect

List of utility methods to do Rectangle Intersect

Description

The list of methods to do Rectangle Intersect are organized into topic(s).

Method

booleanintersects(final Rectangle elementRect, final Rectangle viewportRect)
intersects
if ((elementRect.y < viewportRect.y) || (elementRect.y > viewportRect.y + viewportRect.height)) {
    return false;
if ((elementRect.y + elementRect.height < viewportRect.y)
        || (elementRect.y + elementRect.height > viewportRect.y + viewportRect.height)) {
    return false;
return true;
...
booleanintersects(final Rectangle2D a, final Rectangle2D b)
intersects
return Math.abs(a.getCenterX() - b.getCenterX()) < a.getWidth() * 0.5 + b.getWidth() * 0.5
        && Math.abs(a.getCenterY() - b.getCenterY()) < a.getHeight() * 0.5 + b.getHeight() * 0.5;
booleanintersects(float cx, float cy, float radius, float left, float top, float right, float bottom)
intersects
float closestX = (cx < left ? left : (cx > right ? right : cx));
float closestY = (cy < top ? top : (cy > bottom ? bottom : cy));
float dx = closestX - cx;
float dy = closestY - cy;
return (dx * dx + dy * dy) <= radius * radius;
booleanintersects(Point p1, Point p2, Rectangle rect)
intersects
if (intersects(p1, p2, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y)))
    return true;
if (intersects(p1, p2, new Point(rect.x, rect.y + rect.height),
        new Point(rect.x + rect.width, rect.y + rect.height)))
    return true;
if (intersects(p1, p2, new Point(rect.x, rect.y), new Point(rect.x, rect.y + rect.height)))
    return true;
if (intersects(p1, p2, new Point(rect.x + rect.width, rect.y),
...
booleanintersects(Rectangle aRectangle, Line2D aLine)
intersects
if (aLine == null || aRectangle == null) {
    return false;
return (!segmentOutsideRectangle(aRectangle, aLine)) && (!segmentInsideRectangle(aRectangle, aLine));
booleanintersects(Rectangle rect1, Rectangle rect2, boolean vertical)
intersects
if (vertical) {
    return !((rect1.y > rect2.y + rect2.height) || (rect2.y > rect1.y + rect1.height));
} else {
    return !((rect1.x > rect2.x + rect2.width) || (rect2.x > rect1.x + rect1.width));
booleanintersects(Rectangle2D r, Object o)
Check for intersection between a rectangle and another (presumably) geometric object.
if (o instanceof Point2D) {
    return intersects(r, (Point2D) o);
if (o instanceof Shape) {
    return intersects(r, (Shape) o);
throw new IllegalArgumentException("Object passed must be either a shape or a point.");
booleanintersects(Rectangle2D rect1, Rectangle2D rect2)
Checks, whether the given rectangle1 fully contains rectangle 2 (even if rectangle 2 has a height or width of zero!).
double x0 = rect1.getX();
double y0 = rect1.getY();
double x = rect2.getX();
double width = rect2.getWidth();
double y = rect2.getY();
double height = rect2.getHeight();
return (x + width >= x0 && y + height >= y0 && x <= x0 + rect1.getWidth() && y <= y0 + rect1.getHeight());
booleanintersectsOutline(Rectangle2D r, Shape s)
Return true if the outline of the given shape intersects with the given rectangle.
PathIterator i = s.getPathIterator(null, .01);
double points[] = new double[6];
double lastX = 0, lastY = 0;
double firstX = 0, firstY = 0;
while (!i.isDone()) {
    int type = i.currentSegment(points);
    if (type == PathIterator.SEG_MOVETO) {
        firstX = points[0];
...
booleanintersectsRectangle(final double checkMinX, final double checkMinY, final double checkMaxX, final double checkMaxY, final double againstMinX, final double againstMinY, final double againstMaxX, final double againstMaxY)
Checks whether a rectangle intersects another rectangle.
boolean output = false;
if (againstMaxX > checkMinX && againstMinX < checkMaxX && againstMaxY > checkMinY
        && againstMinY < checkMaxY) {
    output = true;
return output;