Java Rectangle Intersect getMaxIntersection(List targetRects, Rectangle rect)

Here you can find the source of getMaxIntersection(List targetRects, Rectangle rect)

Description

get Max Intersection

License

Open Source License

Declaration

private static Rectangle getMaxIntersection(List<Rectangle> targetRects, Rectangle rect) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.awt.Rectangle;

import java.util.List;

public class Main {
    private static Rectangle getMaxIntersection(List<Rectangle> targetRects, Rectangle rect) {
        int maxIntersection = 0;
        Rectangle result = null;//w ww.  j av  a 2s.  c om
        for (Rectangle targetRect : targetRects) {
            int intersection = getIntersection(targetRect, rect);
            if (maxIntersection < intersection) {
                maxIntersection = intersection;
                result = targetRect;
            }
        }
        return result;
    }

    private static int getIntersection(Rectangle rect1, Rectangle rect2) {
        int ax1 = rect1.x;
        int ax2 = ax1 + rect1.width;
        int ay1 = rect1.y;
        int ay2 = ay1 + rect1.height;

        int bx1 = rect2.x;
        int bx2 = bx1 + rect2.width;
        int by1 = rect2.y;
        int by2 = by1 + rect2.height;

        return (((bx1 < ax2) && (bx2 > ax1) && (by1 < ay2) && (by2 > ay1))
                ? (Math.min(ax2, bx2) - Math.max(ax1, bx1)) * (Math.min(ay2, by2) - Math.max(ay1, by1))
                : 0);
    }
}

Related

  1. intersect(Rectangle r1, Rectangle r2, Rectangle result)
  2. intersect(Rectangle rect1, Rectangle rect2)
  3. intersection(Line2D.Double line, Rectangle2D.Double bounds)
  4. intersection(Rectangle r1, Rectangle r2, Rectangle out)