-1

I have been searching for solutions to my problem but haven't found any that I could understand fully, feel free to link to solutions.

I want to calculate the x,y co-ordinates of a rectangle which is the intersection of two other 2D, normal rectangles:

  (x0,y0)      
    +------------+
    |            |
    |    (x4,y4) |
    |       +----------+
    |       |    |     |
    +-------|----+     |
            |  (x2,y2) |
            |          |
            +----------+
                     (x5,y5)

Basically I just need co-ordinates for the intersection rectangle. I'll be implementing this in C but the answer can be in pseudo code.

Thanks

Edit: what i am looking for is an algorithm to find the rectangle of intersection between any two 2d, normal rectangles, not a solution solely for the example above

conor
  • 883
  • 5
  • 14
  • 25

4 Answers4

4

The co-ordinates of the top-left corner are given by: (max(x4, x0), max(y4, y0)).

The co-ordinates of the bottom-right corner are given by: (min(x2, x5), min(y2, y5)).

If max(x4, x0) > min(x2, x5) or max(y4,y0) > min(y2, y5) then there is no intersection.

Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
  • thanks. should have mentioned this in the question but the solution has to work for any possible intersection between two 2d, normal rectangles. i suppose the first thing to do would be to figure out which points in rect2 are in rect1? – conor Jul 18 '12 at 20:47
  • 1
    @conor: What's wrong with the solution I posted? Have you even tried it? Can you give an example of where it fails? – Mark Byers Jul 18 '12 at 20:48
0

Felt this was to long to post as a comment: Have you tried using google? there are plenty of links available (many from stackoverflow):

How to get overlapping rectangle coordinates

What is an Efficient algorithm to find Area of Overlapping Rectangles

Calculate overlap between two rectangles on x/y grid?

http://www.leetcode.com/2011/05/determine-if-two-rectangles-overlap.html

In case you have poor google-fu:

http://www.google.com/search?q=find+coordinates+for+overlapping+rectangle

Community
  • 1
  • 1
Colin D
  • 5,641
  • 1
  • 23
  • 35
  • please read the first line of my post. I have seen all of the links that you have provided.. i do know that the rectangles actually intersect, that part i have figured out. what i need is an algorithm for figuring out the rectangle of intersection between the two - and this should work for any two rectangles intersecting not just the example i have shown – conor Jul 18 '12 at 20:52
  • @conor, look at the answer by stakx on the first link. – Colin D Jul 18 '12 at 20:54
0

Two coordinates are already known since they belong to each one of the intersecting triangles.

The other two can be found by using intersection algorithm for two sides. First find the line equations of intersecting sides and then use them to find the point of intersection of two lines.

dreamerkumar
  • 1,540
  • 1
  • 18
  • 28
0

This is a Java Program for same. Here a rectangle is constructed by any two corner points(p1 and p2).

You can validate the Rectangle You can check if they have common area rectangle

You can get the Intersecting rectangle and its area(Java).

package com.prb.problemSolvingSkill;

import java.util.Arrays;

public class Rectangle {

public class Point {
    /*
     * This is a 2D point with coordinate (x,y)
     */
    double x;
    double y;

    Point() {
        this.x = 0;
        this.y = 0;
    }

    Point(double x, double y) {
        this.x = x;
        this.y = y;
    }

    public String show() {
        return "( " + x + " , " + y + " )";
    }

    public boolean isEqual(Point p) {
        return this.x == p.x && this.y == p.y;
    }

}

/**
 * Rectangle is constructed by any two corner points p1 and p2
 */
Point p1, p2;

public Rectangle() {
    this.p1 = new Point();
    this.p2 = new Point();
}

public Rectangle(double x1, double y1, double x2, double y2) {
    this.p1 = new Point(x1, y1);
    this.p2 = new Point(x2, y2);

}

public Rectangle(Point p1, Point p2) {
    this.p1 = p1;
    this.p2 = p2;
}

public void show() {

    System.out.println("---------- " + this + " ------------");
    System.out.println("Point  p1 is : " + p1.show());
    System.out.println("Point  p2 is : " + p2.show());

}

public boolean validate() {

    if (this.p1.x != this.p2.x && this.p1.y != this.p2.y)
        return true;
    else
        return false;
}

public double getArea() {

    double height = Math.abs(p1.y - p2.y);
    double width = Math.abs(p1.x - p2.x);

    return height * width;
}

/**
 * This is like a utility method
 * 
 * @param rect1
 * @param rect2
 * @return
 */
public static Rectangle getIntersectedRectangle(Rectangle rect1,
        Rectangle rect2) {

    if (!hasCommonArea(rect1, rect2))
        return null;

    /*
     * If Common area exists then find Rectangle
     * 
     * Two x-coordinate of intersected rectangle will be middle two
     * x-coordinate of four x-coordinates
     */
    double[] dXArr = new double[] { rect1.p1.x, rect1.p2.x, rect2.p1.x,
            rect2.p2.x };
    double[] dYArr = new double[] { rect1.p1.y, rect1.p2.y, rect2.p1.y,
            rect2.p2.y };

    Arrays.sort(dXArr);
    Arrays.sort(dYArr);

    Rectangle inRect = new Rectangle(dXArr[1], dYArr[1], dXArr[2], dYArr[2]);

    inRect.show();
    return inRect;
}

/**
 * This is like a utility method
 * 
 * @param rect1
 * @param rect2
 * @return
 */
public static boolean hasCommonArea(Rectangle rect1, Rectangle rect2) {

    boolean flag1 = true, flag2 = true;
    if ((Math.min(rect1.p1.x, rect1.p2.x) >= Math.max(rect2.p1.x,
            rect2.p2.x))
            || (Math.max(rect1.p2.x, rect1.p2.x) <= Math.min(rect2.p1.x,
                    rect2.p2.x))) {

        flag1 = false;
    }

    if ((Math.min(rect1.p1.y, rect1.p2.y) >= Math.max(rect2.p1.y,
            rect2.p2.y))
            || (Math.max(rect1.p2.y, rect1.p2.y) <= Math.min(rect2.p1.y,
                    rect2.p2.y))) {

        flag2 = false;
    }

    if (!(flag1 && flag2))
        System.out.println("Common Area doesnot exist");

    // System.out.println("flag1 :x " + flag1 + "  flag2 :y " + flag2);

    return flag1 && flag2;
}

public static void main(String[] args) {
    // TODO Auto-generated method stub

    Rectangle rect1 = new Rectangle(1, 1, 6, 6);
    Rectangle rect2 = new Rectangle(1, 16, 6, 20);

    if (null != getIntersectedRectangle(rect1, rect2))
        System.out.println("Area is : "
                + getIntersectedRectangle(rect1, rect2).getArea()
                + " sq unit");

}

}