Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 140056

Summary: org.eclipse.draw2d.geometry.Rectangle.Rectangle(Point,Point)
Product: [Tools] GEF Reporter: Holger Oehm <public>
Component: GEF-Legacy Draw2dAssignee: Steven R. Shaw <steveshaw>
Status: RESOLVED FIXED QA Contact:
Severity: normal    
Priority: P3 CC: hudsonr, public
Version: 3.2   
Target Milestone: 3.2.0 (Callisto) RC3   
Hardware: All   
OS: All   
Whiteboard:
Attachments:
Description Flags
Short main() routine showing strange behavior of Rectangles
none
proposed patch
none
new proposed patch none

Description Holger Oehm CLA 2006-05-03 15:24:38 EDT
The constructor of org.eclipse.draw2d.geometry.Rectangle that takes two Points as arguments behaves strange. The attached snippet should shed some light on that.
The expected behavior of the snippet is that both Rectangles have the same
coordinates after construction.
The actual behavior is that both Rectangles differ.
Comment 1 Holger Oehm CLA 2006-05-03 15:26:40 EDT
Created attachment 40283 [details]
Short main() routine showing strange behavior of Rectangles
Comment 2 Steven R. Shaw CLA 2006-05-03 15:54:05 EDT
probably points to an issue with Rectangle#union
Comment 3 Randy Hudson CLA 2006-05-03 16:10:00 EDT
union is working ok. The problem is that:
new Rectangle().setLocation(point)
does not create a rectangle that contains that point. It creates an empty rectangle containing nothing, but at that location. So the first point is not guaranteed to be contained in the result.

This is also affecting Point.max(), which is creating a new rectangle for some strange reason.

We should just write the code instead of trying to be cute and call other methods.
Comment 4 Holger Oehm CLA 2006-05-05 14:52:15 EDT
Just in case, here is what I use as a workaround (feel free to use it, if you like):
        final int x = Math.min(start.x, end.x);
        final int y = Math.min(start.y, end.y);
        final int width = Math.abs(end.x - start.x);
        final int height = Math.abs(end.y - start.y);
        final Rectangle rect = new Rectangle(x, y, width + 1, height + 1);
Comment 5 Steven R. Shaw CLA 2006-05-10 13:53:11 EDT
Created attachment 40982 [details]
proposed patch

- Proposed patch to Rectangle class in constructor.
- New JUnit in org.eclipse.draw2d.test
Comment 6 Holger Oehm CLA 2006-05-10 14:31:50 EDT
(In reply to comment #5)
Hi Steven,

I think one needs to add one to the width and height of the Rectangle to make it contain both Points.
I am not completely sure about that, but AFAIU the javadoc of the Rectangle, it says that Points on the right/lower bounds of the Rectangle are not contained in the Rectangle.

Best, Holger.
PS: BTW, I like the patch and the test case, too. ;-)
Comment 7 Randy Hudson CLA 2006-05-10 14:48:33 EDT
Yes, need to add 1. I think the test case should hard-code the expected result, rather than just test two results to each other. Correct result is 0,0,11,11. Another case which previously failed: new Rect(br, tl).
Comment 8 Steven R. Shaw CLA 2006-05-11 11:27:37 EDT
It looks like most of the code in Rectangle is not consistent with this Java doc comment though - 

i.e. 
public Point getBottomRight() {
	return new Point(x + width, y + height);
}
Comment 9 Steven R. Shaw CLA 2006-05-11 11:36:18 EDT
Created attachment 41136 [details]
new proposed patch

Ok - but I verified the new implementation returns the exact same result as the previous constructor where the width and height don't include the bottom right. 

See new attachment for patch...
Comment 10 Randy Hudson CLA 2006-05-11 13:26:51 EDT
(In reply to comment #8)
> It looks like most of the code in Rectangle is not consistent with this Java
> doc comment though - 
> i.e. 
> public Point getBottomRight() {
>         return new Point(x + width, y + height);
> }

That constructor is the exception to the rule, which is why it is documented. The top and left edges of a rectangle are inclusive, the bottom and right are exclusive. This is how both SWT and AWT work as well.
Comment 11 Randy Hudson CLA 2006-05-11 13:31:37 EDT
The patch seems fine, but I'm not sure there is a hurry for 3.2. A workaround to the bug is to call:
new Rectangle(p1, new Dimension(1,1)).union(p2);

I could go either way:
+0.5
Comment 12 Steven R. Shaw CLA 2006-05-15 11:37:36 EDT
One could argue that this is data corruption since the contructor parameters are not being translated properly.  I think this is a safe fix. +1
Comment 13 Steven R. Shaw CLA 2006-05-15 13:42:09 EDT
Committed change