001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package java.awt.geom;
018:
019: import java.awt.Point;
020: import java.awt.Rectangle;
021:
022: public class Rectangle2DTest extends ShapeTestCase {
023:
024: // Test intersectLine
025: public static int lines[][] = new int[][] {
026: // Parallel left
027: { 0, 0, 0, 2, 0 },
028: { -1, 4, -1, 6, 0 },
029: { 0, 5, 0, 8, 0 },
030: { 0, 7, 0, 10, 0 },
031: // Parallel right
032: { 5, 0, 5, 2, 0 },
033: { 6, 4, 6, 6, 0 },
034: { 5, 5, 5, 8, 0 },
035: { 5, 7, 5, 10, 0 },
036: // Parallel top
037: { 0, 0, 2, 0, 0 },
038: { 0, 0, 1, 0, 0 },
039: { 2, 0, 5, 0, 0 },
040: { 4, 0, 6, 0, 0 },
041: // Parallel bottom
042: { 0, 7, 2, 7, 0 },
043: { 0, 7, 1, 7, 0 },
044: { 2, 7, 5, 7, 0 },
045: { 4, 7, 6, 7, 0 },
046: // Corner TL
047: { 0, 2, -1, 3, 0 },
048: { 0, 2, 1, 1, 0 },
049: { 1, 1, 2, 1, 0 },
050: { -9, 9, 9, -9, 0 },
051: // Corner TR
052: { 3, 0, 4, 1, 0 },
053: { 3, 0, 5, 2, 0 },
054: { 5, 2, 6, 4, 0 },
055: { -9, -9, 19, 9, 0 },
056: // Corner BR
057: { 4, 7, 6, 10, 0 },
058: { 5, 8, 6, 6, 0 },
059: { 6, 7, 8, 10, 0 },
060: { 0, 11, 11, 0, 0 },
061: // Corner BL
062: { 0, 6, -2, 1, 0 },
063: { 0, 6, 1, 8, 0 },
064: { -1, 8, 3, 10, 0 },
065: { -9, 0, 9, 19, 0 },
066: // Touch left
067: { -1, 1, 1, 2, 1 },
068: { 0, 3, 1, 4, 1 },
069: // Touch bottom
070: { 0, 5, 1, 6, 1 },
071: { 1, 8, 3, 6, 1 },
072: // Touch right
073: { 7, 6, 4, 6, 1 },
074: { 6, 4, 4, 3, 1 },
075: // Touch top
076: { 6, 0, 4, 2, 1 },
077: { 4, 0, 2, 2, 1 },
078: // On the left side
079: { 1, 0, 1, 1, 0 }, // up
080: { 1, 0, 1, 3, 1 },
081: { 1, 3, 1, 5, 1 }, // inside
082: { 1, 4, 1, 7, 1 },
083: { 1, 7, 1, 9, 0 }, // down
084: { 1, 0, 1, 9, 1 }, // cover
085: // On the right side
086: { 4, 0, 4, 1, 0 }, // up
087: { 4, 0, 4, 3, 1 },
088: { 4, 3, 4, 5, 1 }, // inside
089: { 4, 4, 4, 7, 1 },
090: { 4, 7, 4, 9, 0 }, // down
091: { 4, 0, 4, 9, 1 }, // cover
092: // On the top
093: { -1, 2, 0, 2, 0 }, { 0, 2, 2, 2, 1 }, { 2, 2, 3, 2, 1 },
094: { 3, 2, 6, 2, 1 }, { 5, 2, 8, 2, 0 }, { 0, 2, 7, 2, 1 },
095: // On the bottom
096: { -1, 6, 0, 6, 0 }, { 0, 6, 2, 6, 1 }, { 2, 6, 3, 6, 1 },
097: { 3, 6, 6, 6, 1 }, { 5, 6, 8, 6, 0 }, { 0, 6, 7, 6, 1 }
098: // Intersect vertical
099: };
100:
101: Rectangle2D.Double r;
102:
103: public Rectangle2DTest(String name) {
104: super (name);
105: // filterImage = createFilter("^(rect).*([.]ico)$", "(.*)((affine)|(flat)|(bounds))(.*)");
106: }
107:
108: @Override
109: protected void setUp() throws Exception {
110: super .setUp();
111: r = new Rectangle2D.Double(1, 2, 3, 4);
112: }
113:
114: @Override
115: protected void tearDown() throws Exception {
116: r = null;
117: super .tearDown();
118: }
119:
120: public void testSetRect() {
121: r.setRect(new Rectangle2D.Double(5, 6, 7, 8));
122: assertEquals(new Rectangle2D.Double(5, 6, 7, 8), r);
123: }
124:
125: public void testSetFrame() {
126: r.setFrame(5.0, 6.0, 7.0, 8.0);
127: assertEquals(new Rectangle2D.Double(5, 6, 7, 8), r);
128: }
129:
130: public void testGetBounds2D() {
131: assertEquals(new Rectangle2D.Double(1, 2, 3, 4), r
132: .getBounds2D());
133: }
134:
135: public void testOutcode() {
136: // Just check it works
137: assertEquals(Rectangle2D.OUT_LEFT | Rectangle2D.OUT_TOP, r
138: .outcode(0, 0));
139: assertEquals(0, r.outcode(new Point2D.Double(2, 3)));
140: }
141:
142: public void testEquals() {
143: assertTrue(r.equals(new Rectangle(1, 2, 3, 4)));
144: assertTrue(!r.equals(new Rectangle(0, 2, 3, 4)));
145: assertTrue(!r.equals(new Rectangle(1, 0, 3, 4)));
146: assertTrue(!r.equals(new Rectangle(1, 2, 0, 4)));
147: assertTrue(!r.equals(new Rectangle(1, 2, 3, 0)));
148: }
149:
150: public void testHashCode() {
151: assertTrue(r.hashCode() == new Rectangle(1, 2, 3, 4).hashCode());
152: assertTrue(r.hashCode() != new Rectangle(0, 2, 3, 4).hashCode());
153: assertTrue(r.hashCode() != new Rectangle(1, 0, 3, 4).hashCode());
154: assertTrue(r.hashCode() != new Rectangle(1, 2, 0, 4).hashCode());
155: assertTrue(r.hashCode() != new Rectangle(1, 2, 3, 0).hashCode());
156: }
157:
158: public void testIntersectsLine1() {
159: Rectangle2D rr = new Rectangle2D.Double(1, 2, 3, 4);
160: for (int[] element : lines) {
161: int x1 = element[0];
162: int y1 = element[1];
163: int x2 = element[2];
164: int y2 = element[3];
165: assertEquals("Rectangle2D.intersectsLine(" + x1 + "," + y1
166: + "," + x2 + "," + y2 + ") failed",
167: element[4] == 1, rr.intersectsLine(x1, y1, x2, y2));
168: }
169: }
170:
171: public void testIntersectsLine2() {
172: Rectangle2D rr = new Rectangle2D.Double(1, 2, 3, 4);
173: for (int[] element : lines) {
174: int x1 = element[0];
175: int y1 = element[1];
176: int x2 = element[2];
177: int y2 = element[3];
178: assertEquals("Rectangle2D.intersectsLine(" + x1 + "," + y1
179: + "," + x2 + "," + y2 + ") failed",
180: element[4] == 1, rr
181: .intersectsLine(new Line2D.Double(x1, y1,
182: x2, y2)));
183: }
184: }
185:
186: public void testIntersect() {
187: Rectangle dst = new Rectangle();
188: Rectangle2D.intersect(r, new Rectangle(1, 2, 3, 4), dst);
189: assertEquals(new Rectangle(1, 2, 3, 4), dst); // The same
190: Rectangle2D.intersect(r, new Rectangle(2, 3, 1, 2), dst);
191: assertEquals(new Rectangle(2, 3, 1, 2), dst); // Inside
192: Rectangle2D.intersect(r, new Rectangle(5, 7, 1, 2), dst);
193: assertEquals(new Rectangle(5, 7, -1, -1), dst); // Outside
194: Rectangle2D.intersect(r, new Rectangle(2, 3, 5, 6), dst);
195: assertEquals(new Rectangle(2, 3, 2, 3), dst); // Intersect
196: Rectangle2D.intersect(r, new Rectangle(0, 0, 5, 6), dst);
197: assertEquals(new Rectangle(1, 2, 3, 4), dst); // Cover
198: }
199:
200: public void testUnion() {
201: Rectangle dst = new Rectangle();
202: Rectangle2D.union(r, new Rectangle(1, 2, 3, 4), dst);
203: assertEquals(new Rectangle(1, 2, 3, 4), dst); // The same
204: Rectangle2D.union(r, new Rectangle(2, 3, 1, 2), dst);
205: assertEquals(new Rectangle(1, 2, 3, 4), dst); // Inside
206: Rectangle2D.union(r, new Rectangle(5, 7, 1, 2), dst);
207: assertEquals(new Rectangle(1, 2, 5, 7), dst); // Outside
208: Rectangle2D.union(r, new Rectangle(2, 3, 5, 6), dst);
209: assertEquals(new Rectangle(1, 2, 6, 7), dst); // Intersect
210: Rectangle2D.union(r, new Rectangle(0, 0, 5, 6), dst);
211: assertEquals(new Rectangle(0, 0, 5, 6), dst); // Cover
212: }
213:
214: public void testAdd1() {
215: r.add(1.0, 1.0);
216: assertEquals(new Rectangle2D.Double(1, 1, 3, 5), r);
217: r.add(7.0, 8.0);
218: assertEquals(new Rectangle2D.Double(1, 1, 6, 7), r);
219: r.add(5.0, 9.0);
220: assertEquals(new Rectangle2D.Double(1, 1, 6, 8), r);
221: r.add(9.0, 6.0);
222: assertEquals(new Rectangle2D.Double(1, 1, 8, 8), r);
223: r.add(0.0, 0.0);
224: assertEquals(new Rectangle2D.Double(0, 0, 9, 9), r);
225: }
226:
227: public void testAdd2() {
228: r.add(new Point(0, 0));
229: assertEquals(new Rectangle2D.Double(0, 0, 4, 6), r);
230: r.add(new Point(1, 1));
231: assertEquals(new Rectangle2D.Double(0, 0, 4, 6), r);
232: r.add(new Point(7, 8));
233: assertEquals(new Rectangle2D.Double(0, 0, 7, 8), r);
234: r.add(new Point(5, 9));
235: assertEquals(new Rectangle2D.Double(0, 0, 7, 9), r);
236: r.add(new Point(9, 6));
237: assertEquals(new Rectangle2D.Double(0, 0, 9, 9), r);
238: }
239:
240: public void testAdd3() {
241: r.add(new Rectangle(1, 2, 3, 4)); // The same
242: assertEquals(new Rectangle2D.Double(1, 2, 3, 4), r);
243: r.add(new Rectangle(2, 3, 2, 3)); // Inside
244: assertEquals(new Rectangle2D.Double(1, 2, 3, 4), r);
245: r.add(new Rectangle2D.Double(0, 0, 1, 2)); // Outside
246: assertEquals(new Rectangle2D.Double(0, 0, 4, 6), r);
247: r.add(new Rectangle(2, 3, 3, 4)); // Intersect
248: assertEquals(new Rectangle2D.Double(0, 0, 5, 7), r);
249: r.add(new Rectangle(-1, -1, 6, 8)); // Cover
250: assertEquals(new Rectangle2D.Double(-1, -1, 6, 8), r);
251: }
252:
253: void checkPathIteratorDouble(PathIterator p, double[] values) {
254: checkPathRule(p, PathIterator.WIND_NON_ZERO);
255: checkPathMove(p, false, values[0], values[1], 0.0);
256: checkPathLine(p, false, values[2], values[3], 0.0);
257: checkPathLine(p, false, values[4], values[5], 0.0);
258: checkPathLine(p, false, values[6], values[7], 0.0);
259: checkPathLine(p, false, values[0], values[1], 0.0);
260: checkPathClose(p, true);
261: }
262:
263: void checkPathIteratorFloat(PathIterator p, float[] values) {
264: checkPathRule(p, PathIterator.WIND_NON_ZERO);
265: checkPathMove(p, false, values[0], values[1], 0.0f);
266: checkPathLine(p, false, values[2], values[3], 0.0f);
267: checkPathLine(p, false, values[4], values[5], 0.0f);
268: checkPathLine(p, false, values[6], values[7], 0.0f);
269: checkPathLine(p, false, values[0], values[1], 0.0f);
270: checkPathClose(p, true);
271: }
272:
273: public void testGetPathIteratorDouble() {
274: checkPathIteratorDouble(new Rectangle(1, 2, 0, 0)
275: .getPathIterator(null), new double[] { 1, 2, 1, 2, 1,
276: 2, 1, 2 });
277: checkPathIteratorDouble(r.getPathIterator(null), new double[] {
278: 1, 2, 4, 2, 4, 6, 1, 6 });
279: }
280:
281: public void testGetPathIteratorFloat() {
282: checkPathIteratorFloat(r.getPathIterator(null), new float[] {
283: 1, 2, 4, 2, 4, 6, 1, 6 });
284: }
285:
286: public void testGetPathIteratorDoubleFlat() {
287: checkPathIteratorDouble(r.getPathIterator(null, 2),
288: new double[] { 1, 2, 4, 2, 4, 6, 1, 6 });
289: }
290:
291: public void testGetPathIteratorFloatFlat() {
292: checkPathIteratorFloat(r.getPathIterator(null, 5), new float[] {
293: 1, 2, 4, 2, 4, 6, 1, 6 });
294: }
295:
296: public void testGetPathIteratorDoubleAffine() {
297: checkPathIteratorDouble(r.getPathIterator(AffineTransform
298: .getTranslateInstance(3, 1)), new double[] { 4, 3, 7,
299: 3, 7, 7, 4, 7 });
300: }
301:
302: public void testGetPathIteratorFloatAffine() {
303: checkPathIteratorFloat(r.getPathIterator(AffineTransform
304: .getTranslateInstance(3, 1)), new float[] { 4, 3, 7, 3,
305: 7, 7, 4, 7 });
306: }
307:
308: public static void main(String[] args) {
309: junit.textui.TestRunner.run(Rectangle2DTest.class);
310: }
311:
312: }
|