Java Rectangle Intersect intersection(Rectangle r1, Rectangle r2, Rectangle out)

Here you can find the source of intersection(Rectangle r1, Rectangle r2, Rectangle out)

Description

intersection

License

Apache License

Declaration

public static boolean intersection(Rectangle r1, Rectangle r2, Rectangle out) 

Method Source Code


//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;
    }
}

Related

  1. getMaxIntersection(List targetRects, Rectangle rect)
  2. intersect(Rectangle r1, Rectangle r2, Rectangle result)
  3. intersect(Rectangle rect1, Rectangle rect2)
  4. intersection(Line2D.Double line, Rectangle2D.Double bounds)
  5. intersection(Rectangle rectangle, Rectangle rectangle1, Rectangle rectangle2)
  6. intersectRect(double x1, double y1, double w1, double h1, double x2, double y2, double w2, double h2)
  7. intersects(double oldx, double oldy, double oldwidth, double oldheight, double oldx2, double oldy2, double oldwidth2, double oldheight2)
  8. intersects(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4)