Java intersection between rectangles

k_bhushan27

Joined Jan 26, 2012
11
hey guys i have another question im stumped on here it is
You are given one rectangle, called box1.
Make a second rectangle, called box2, so that
the top left corner of box2 is in the center of box1,
and the width of box2 is half the width of box1.
and the height of box2 is half the height of box1.

public class IntersectionPrinter
{
public static void main(String[] args)
{
Random random = new Random() ;
Rectangle box1 = new Rectangle(100, 200, 300, 400) ;
//----------------------Start here. Do not remove this comment.
// todo
// approximate lines of code required: 1

//----------------------End here. Do not remove this comment.

Rectangle box3 = box1.intersection(box2) ;
System.out.println(box2.equals(box3)) ;

my prof already made the program i just have to edit it. although im not sure how to come with line of code required if anyone can help me it will be much appreciated!

kubeek

Joined Sep 20, 2005
5,724
You should draw the two rectangles on a piece of paper. Then you should clearly see how are the coordinates and size calculated.
Putting it into the code is then simple.

Again, if you don´t post the Rectangle class noone can guess how to use it. And please use the code tag (the # symbol in the menu) around your code, it makes it easier to read.

Joined Nov 28, 2011
57
Oh, this is really easy. If there's something in the problem that you don't understand, then ask about that. But don't just ask other people to do your really easy class work so that you don't have to think. You won't learn anything.

Joined Nov 28, 2011
57
Make a second rectangle, called box2, so that
the top left corner of box2 is in the center of box1,
and the width of box2 is half the width of box1.
and the height of box2 is half the height of box1.

...
Rich (BB code):
Rectangle box1 = new Rectangle(100, 200, 300, 400) ;
Start by telling us what the numbers are, specifically. Which number is "left", for instance?

Then tell us how you would make a second rectangle with a "left" value half-way between the left and the right (aka, "center") of box1, the "top" value of box2 two half-way between the top and bottom of box2, etc. When you are done, you will have the answer to your problem.

kubeek

Joined Sep 20, 2005
5,724
k_bhushan27 wasn't here since the 28th when he posted this and I doubt he will ever come back..