# Intersection of two Rectangles

• 03-25-2012, 01:23 PM
Dex
Intersection of two Rectangles
Hey, Im struck trying to write some code.
Im trying to use the Intersection method , to print out the dimensions + position of the new rectangle(r3) that the intersection of two rectangles (randRectangle and box) makes. (hope that makes sense).
I already know how to create my two rectangles but I do not know how to do the intersection part and i must only use: import java.awt.Rectangle;

randRectangle and box are my two created rectangles

This is what i have so far...
Code:

```    Rectangle r3 = randRectangle.intersection(box);     System.out.println(r3.toString());```

Thanks
• 03-25-2012, 01:52 PM
JosAH
Re: Intersection of two Rectangles
Read the API documentation for the Rectangle class; you're almost there.

kind regards,

Jos
• 03-25-2012, 02:15 PM
Dex
Re: Intersection of two Rectangles
Quote:

Originally Posted by JosAH
Read the API documentation for the Rectangle class; you're almost there.

kind regards,

Jos

Ok thanks Ill have a look now!
Also I tried to do an If, else statement for if there there is intersection or not but I wasn't sure what to write as I've only ever used booleans for them. Any pointers?
• 03-25-2012, 02:21 PM
JosAH
Re: Intersection of two Rectangles
Quote:

Originally Posted by Dex
Ok thanks Ill have a look now!
Also I tried to do an If, else statement for if there there is intersection or not but I wasn't sure what to write as I've only ever used booleans for them. Any pointers?

It's all in the API documentation for the Rectangle class; read about it in the isEmpty() method (two Rectangles that don't intersect return an empty Rectangle).

kind regards,

Jos
• 03-25-2012, 02:28 PM
Dex
Re: Intersection of two Rectangles
Thank you Jos , seems to be working now :)