# Thread: Intersection of two Rectangles

1. Member
Join Date
Mar 2012
Posts
29
Rep Power
0

## 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...
Java Code:
```    Rectangle r3 = randRectangle.intersection(box);
System.out.println(r3.toString());```

Thanks

2. ## Re: Intersection of two Rectangles

Read the API documentation for the Rectangle class; you're almost there.

kind regards,

Jos

3. Member
Join Date
Mar 2012
Posts
29
Rep Power
0

## Re: Intersection of two Rectangles

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?

4. ## Re: Intersection of two Rectangles

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

5. Member
Join Date
Mar 2012
Posts
29
Rep Power
0

## Re: Intersection of two Rectangles

Thank you Jos , seems to be working now :)

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•