1

I was developing an task when I decided to use java.awt.Rectangle to calculate the intersection between two rectangles.

I realised that the output is different from what I expected. I'm not sure if I understood how this method works or not.

For the values in the example here java.awt.Rectangle[x=0,y=10,width=5,height=8] java.awt.Rectangle[x=3,y=15,width=17,height=14]

I expect the intersection to be java.awt.Rectangle[x=3,y=10,width=2,height=8] but the program prints java.awt.Rectangle[x=3,y=15,width=2,height=3] instead!

here is my code:

public void printIntersection(){ 
    Rectangle r1 = new Rectangle(0, 10, 5, 8);
    Rectangle r2 = new Rectangle(3, 15, 17, 14);
    Rectangle r3 = r1.intersection(r2);

    System.out.println(r1);
    System.out.println(r2);
    System.out.println(r3);

}

Can anyone help me by pointing out what am I missing here?

UPDATE: The source of my confusion is that the code treats the (x,y) values in the constructor as the bottom-left corner, while the class doc suggests that they are the upper-left corner!

Matto
  • 2,538
  • 2
  • 18
  • 23

3 Answers3

3

The answer you are getting is correct. The method works like this.

1st Rectangle:

  • X co-ordinates: 0
  • Y co-ordinates: 10
  • Width: 5
  • Height: 8

2nd Rectangle:

  • X co-ordinates: 3
  • Y co-ordinates: 15
  • Width: 17
  • Height: 14

For the intersection the X and Y co-ordinates are same as 2nd rectangle. Width is 5-3=2 and Height is 18-15=3

Ananth
  • 41
  • 1
  • 1
  • 10
  • 1
    Would you please tell me if you considering (x, y) to be the upper-left corner or botton-left corner, I'm asking because the documentation says it's upper-left while your answer, @pablo Lazano's answer suggesting that it's bottom-left! I hope u know understand the source of confusion – Matto May 27 '13 at 12:50
  • 1
    My answer is by considering (x,y) co-ordinates as upper left corner only. (0,10) is the upper left corner for 1st rectangle. Likewise (3,15) is the upper left corner of 2nd rectangle.The confines of the intersection part is (3,15),(5,15),(3,18) and (5,18). So the output you'll get is {3,15,2,3} – Ananth May 28 '13 at 11:45
2

I also had trouble with this. The way I think about it is that the grid used is inverted on the y axis. Because point 0.0 is at the top left of the screen with point 0,1 being below rather than above that point you can get the answer you are expecting by inverting the the y axis in your original code.

For example.

public void printIntersection(){ 
Rectangle r1 = new Rectangle(0, 10 * -1 , 5, 8);
Rectangle r2 = new Rectangle(3, 15 * -1, 17, 14);
Rectangle r3 = r1.intersection(r2);

System.out.println(r1);
System.out.println(r2);
System.out.println(r3);

}

This should give you the answer you are expecting

Abelgo
  • 772
  • 7
  • 7
0

The opposite corners of your rectangles are (0,10),(5,18) and (3,15),(20,29), so the intersection is (3,15),(5,18), so I think the result is the expected one. Notice the opposite corners of the resultant one are the bottom-right of the first one and the top-left of the second one.

Edit: The way it works is: the starting point is (x,y), and the sides are calculated adding the widthand height to the starting point, so the opposite corner will be (x+width,y+height)

Final note: (0,0) is the upper-left corner of the canvas: Here is an example: (0,0,4,4) and (2,2,4,4) intersection is (2,2,2,2): (2,2) is the upper-left one and (2+2,2+2) is opposite corner

enter image description here

Pablo Lozano
  • 10,122
  • 2
  • 38
  • 59
  • The documentation says that (x, y) makes the upper-left corner . for r1 (0, 10) is the upper-left corner so the opposite corner should be (5, 2). Likewise, (3, 15) -> (20, 1) for r2? – Matto May 27 '13 at 11:15
  • No, the opposite corner is (0+5,10+8) – Pablo Lozano May 27 '13 at 12:49
  • 1
    from the class documentation page, http://docs.oracle.com/javase/6/docs/api/java/awt/Rectangle.html#Rectangle(int, int, int, int): Constructs a new Rectangle whose upper-left corner is specified as (x,y) – Matto May 27 '13 at 12:53
  • Yes, I've also read the documentation but you are not understanding what it means: see my edited answer – Pablo Lozano May 27 '13 at 12:55
  • 1
    for (x, y) to be the upper-left corner shouldn't the opposing corners be (x+width, y-hight)! don't get me wrong ur answer seem to be right but I cannot work out the logic! – Matto May 27 '13 at 13:03
  • 1
    Why y-height? if you put a negative height then the x,y would be the lower-left corner and not the upper one. – Pablo Lozano May 27 '13 at 13:22