Skip to content

Commit dfe2e95

Browse files
HeikoKlareptziegler
authored andcommitted
Restore PrecisionRectangle substitutability for Rectangle
Recent changes to PrecisionRectangle adapted how the integer-based size values are calculated, which introduced inconsistencies with other operations and data (such as the relation between bottom-right and top-left corner of a rectangle) and which may also break existing consumers due to the changed behavior of API methods. This change reverts the changes to the necessary extent such that the existing behavior is restored. It also adds tests for some of the scenarios that broke with the changes.
1 parent a878942 commit dfe2e95

File tree

3 files changed

+26
-11
lines changed

3 files changed

+26
-11
lines changed

org.eclipse.draw2d.tests/src/org/eclipse/draw2d/test/PrecisionRectangleTest.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public void testScale() {
119119
assertEquals(91, r.preciseWidth(), 0);
120120
assertEquals(91, r.preciseHeight(), 0);
121121
// Different rounding behavior between Rectangle and PrecisionRectangle.
122-
assertEquals(17, 17, 92, 92, r);
122+
assertEquals(17, 17, 91, 91, r);
123123
}
124124

125125
@SuppressWarnings("static-method")
@@ -147,6 +147,16 @@ public void testGetSize() {
147147
assertEquals(r.height, r.getSize().height);
148148
}
149149

150+
@SuppressWarnings("static-method")
151+
@Test
152+
public void testGetSize_Zero() {
153+
PrecisionRectangle r = new PrecisionRectangle(0.5, 0.5, 0, 0);
154+
assertEquals(r.width, r.getSize().width);
155+
assertEquals(r.height, r.getSize().height);
156+
assertEquals(0, r.getSize().width);
157+
assertEquals(0, r.getSize().height);
158+
}
159+
150160
@SuppressWarnings("static-method")
151161
@Test
152162
public void testGetLocation() {
@@ -155,4 +165,13 @@ public void testGetLocation() {
155165
assertEquals(r.x, r.getLocation().x);
156166
assertEquals(r.y, r.getLocation().y);
157167
}
168+
169+
@SuppressWarnings("static-method")
170+
@Test
171+
public void testGetBottomRight() {
172+
Rectangle rect = new PrecisionRectangle(100.5, 100.5, 250, 250);
173+
assertEquals(rect.getBottomRight().x - rect.getTopLeft().x, rect.width);
174+
assertEquals(rect.getBottomRight().y - rect.getTopLeft().y, rect.height);
175+
}
176+
158177
}

org.eclipse.draw2d.tests/src/org/eclipse/draw2d/test/PrecisionTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public void testPreciseTranslateToAbsolute() {
6969
Rectangle r2 = new PrecisionRectangle(13, 37, 163, 377);
7070
fig.translateToAbsolute(r1);
7171
fig.translateToAbsolute(r2);
72-
assertEquals(493, 1404, 6187, 14309, r1);
72+
assertEquals(493, 1404, 6186, 14307, r1);
7373
assertEquals(r1.x, r2.x);
7474
assertEquals(r1.y, r2.y);
7575
assertEquals(r1.width, r2.width);
@@ -82,7 +82,7 @@ public void testPreciseTranslateToRelative() {
8282
Rectangle r2 = new PrecisionRectangle(753, 891, 353, 564);
8383
fig.translateToRelative(r1);
8484
fig.translateToRelative(r2);
85-
assertEquals(19, 23, 11, 16, r1);
85+
assertEquals(19, 23, 9, 14, r1);
8686
assertEquals(r1.x, r2.x);
8787
assertEquals(r1.y, r2.y);
8888
assertEquals(r1.width, r2.width);

org.eclipse.draw2d/src/org/eclipse/draw2d/geometry/PrecisionRectangle.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -952,12 +952,10 @@ private final void updateHeightInt() {
952952
}
953953

954954
/**
955-
* Calculates the int-height of this rectangle using the same algorithm as used
956-
* by AWT in {@link java.awt.geom.RectangularShape#getBounds()
957-
* RectangularShape#getBounds()}
955+
* Calculates the int-height of this rectangle.
958956
*/
959957
private int getHeightInt() {
960-
return PrecisionGeometry.doubleToInteger((Math.ceil(preciseY + preciseHeight) - Math.floor(preciseY)));
958+
return PrecisionGeometry.doubleToInteger(preciseHeight);
961959
}
962960

963961
/**
@@ -1025,12 +1023,10 @@ private final void updateWidthInt() {
10251023
}
10261024

10271025
/**
1028-
* Calculates the int-height of this rectangle using the same algorithm as used
1029-
* by AWT in {@link java.awt.geom.RectangularShape#getBounds()
1030-
* RectangularShape#getBounds()}
1026+
* Calculates the int-height of this rectangle.
10311027
*/
10321028
private int getWidthInt() {
1033-
return PrecisionGeometry.doubleToInteger(Math.ceil(preciseX + preciseWidth) - Math.floor(preciseX));
1029+
return PrecisionGeometry.doubleToInteger(preciseWidth);
10341030
}
10351031

10361032
/**

0 commit comments

Comments
 (0)