# Help with method to check if two rectangles overlap

• March 19th, 2013, 08:29 PM
jikatz09
Help with method to check if two rectangles overlap
Hey everyone. I am working on a hw assignment where I have to create a class called Rectangle that is utilized in another program. The end result is a graphic of colored rectangles, which overlap in certain areas and change color where they overlap. I have to create a method called overlapWith that checks to see if two rectangles overlap.

if (redRect.overlapsWith(greenRect)) {
Rectangle overlap = redRect.getOverlap(greenRect);
overlap.draw(page);
This piece of code is from the program that utilizes the overlapsWith method I am trying to create. redRect and greenRect were rectangles that were previously defined. This is from my hw assignment: the overlapsWith method should return whether or not the rectangle you call it on (this) overlaps with another rectangle (the argument). Here's a strategy for checking if this rectangle overlaps with another:
If the left x of this rectangle is larger than the right x of the other (or vice versa), they don't overlap.
If the top y of this rectangle is larger than the bottom y of the other (or vice versa), they don't overlap.
Otherwise, they do overlap.
I am stuck and do not know how to start this method. How do I compare x,y data from two seperate variables? Any help would be appreciated.
• March 19th, 2013, 08:36 PM
Norm
Re: Help with method to check if two rectangles overlap
Where are you having problems coding the algorithm that was given?

Take a piece of paper and draw a rectangle on it and label the corners with the values you are given to define the location of a rectangle: x,y and width and height. Looking at that diagram you have drawn read the algorithm's steps for determining an overlap. You could draw several other rectangles around the first one with some overlapping and others not overlapping.
• March 19th, 2013, 09:26 PM
jikatz09
Re: Help with method to check if two rectangles overlap
Ok so I've got that last part finished. Now I need to create a getOverlap method that draws a new rectangle over the overlap, and assigns a new color that is the rgb value of both rectangles combined, but not to reach over 255 on any given value. I really don't know how to get this one started.

--- Update ---

// Jon Katz
// Initiates Rectangle class and draw and getOverlap methods.

import java.awt.Color;
import java.awt.Graphics;

public class Rectangle {

// Instance data for Rectangle class
private int x1;
private int y1;
private int x2;
private int y2;
private int r, g, b;

// Initialize instance data, Constructor
public Rectangle (int x1, int y1, int x2, int y2, int r, int g, int b) {

// Define variables
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
this.r = r;
this.g = g;
this.b = b;

}

// Create a draw method
public void draw(Graphics page) {

int width = x2 - x1;
int height = y2 - y1;
Color color = new Color (r, g, b);
page.setColor(color);
page.fillRect(x1, y1, width, height);
}

// Create overlapsWith method
public boolean overlapsWith(Rectangle a) {
return this.x2 >= a.x1 &&
this.x1 <= a.x2 &&
this.y2 >= a.y1 &&
this.y1 <= a.y2;
}
public void getOverlap(Rectangle a) {
r = this.r + a.r;
b = this.b + a.b;
g = this.g + a.g;
Color overlapColor = new Color (r, g, b);
page.setColor(overlapColor);
}

}

--- Update ---

// Lisa Torrey
// A custom panel that draws a pattern of overlapping rectangles.

import java.awt.*;
import javax.swing.*;

public class RectanglePanel extends JPanel {

public static final int WIDTH = 200; // Panel width
public static final int HEIGHT = 200; // Panel height
public static final int RGB_MAX = 255; // For colors

// Set the panel's size and color
public RectanglePanel() {
super.setPreferredSize(new Dimension(WIDTH, HEIGHT));
super.setBackground(Color.white);
}

// Draw some overlapping rectangles
public void paintComponent(Graphics page) {
super.paintComponent(page);

// A red one in the top left
Rectangle redRect = new Rectangle(0, 0, WIDTH/2, HEIGHT/2, RGB_MAX, 0, 0);
redRect.draw(page);

// A green one in the bottom right
Rectangle greenRect = new Rectangle(WIDTH/2, HEIGHT/2, WIDTH, HEIGHT, 0, RGB_MAX, 0);
greenRect.draw(page);

// A blue one in the middle
Rectangle blueRect = new Rectangle(WIDTH/4, HEIGHT/4, WIDTH*3/4, HEIGHT*3/4, 0, 0, RGB_MAX);
blueRect.draw(page);

// Possibly an overlap of red and green
if (redRect.overlapsWith(greenRect)) {
Rectangle overlap = redRect.getOverlap(greenRect);
overlap.draw(page);
}

// Possibly an overlap of red and blue
if (redRect.overlapsWith(blueRect)) {
Rectangle overlap = redRect.getOverlap(blueRect);
overlap.draw(page);
}

// Possibly an overlap of green and blue
if (greenRect.overlapsWith(blueRect)) {
Rectangle overlap = greenRect.getOverlap(blueRect);
overlap.draw(page);
}
}

// Create and display one of these panels
public static void main(String[] args) {

JFrame frame = new JFrame("Rectangle Panel");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);

RectanglePanel panel = new RectanglePanel();

frame.pack();
frame.setVisible(true);
}
}

--- Update ---

The first is my code so far and the second is the test program. I am confused on how to set up the getOverlap method. Any ideas?
• March 20th, 2013, 01:09 AM
Darryl.Burke
Re: Help with method to check if two rectangles overlap
This thread has been cross posted here: