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!