Here you can find the source of intersection(Rectangle r1, Rectangle r2, Rectangle out)
public static boolean intersection(Rectangle r1, Rectangle r2, Rectangle out)
//package com.java2s; //License from project: Apache License import java.awt.Rectangle; public class Main { public static boolean intersection(Rectangle r1, Rectangle r2, Rectangle out) { int xmin = Math.max(r1.x, r2.x); int xmax1 = r1.x + r1.width; int xmax2 = r2.x + r2.width; int xmax = Math.min(xmax1, xmax2); if (xmax > xmin) { int ymin = Math.max(r1.y, r2.y); int ymax1 = r1.y + r1.height; int ymax2 = r2.y + r2.height; int ymax = Math.min(ymax1, ymax2); if (ymax > ymin) { if (out != null) { out.x = xmin;//from w ww. ja va 2 s. co m out.y = ymin; out.width = xmax - xmin; out.height = ymax - ymin; } return true; } } return false; } }