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: /**
018: * @author Denis M. Kishenko
019: * @version $Revision$
020: */package java.awt.geom;
021:
022: import java.awt.Point;
023: import java.awt.Rectangle;
024:
025: public class Line2DTest extends PathIteratorTestCase {
026:
027: final static double DELTA = 0.001;
028:
029: double points[][] = new double[][] {
030: // X, Y, CCW, seg distance^2, line distance^2
031: // On the right
032: { 0, 0, 1, 5, 0.5 }, { 1, 1, 1, 1, 0.5 },
033: { 2, 2, 1, 0.5, 0.5 }, { 3, 3, 1, 0.5, 0.5 },
034: { 4, 4, 1, 1, 0.5 },
035: { 5, 5, 1, 5, 0.5 },
036: // On the left
037: { -1, 1, -1, 5, 0.5 }, { 0, 2, -1, 1, 0.5 },
038: { 1, 3, -1, 0.5, 0.5 }, { 2, 4, -1, 0.5, 0.5 },
039: { 3, 5, -1, 1, 0.5 }, { 4, 6, -1, 5, 0.5 },
040: // Online behind
041: { 0, 1, -1, 2, 0 }, { -1, 0, -1, 8, 0 },
042: // Online forward
043: { 4, 5, 1, 2, 0 }, { 5, 6, 1, 8, 0 },
044: // Inside
045: { 1, 2, 0, 0, 0 }, { 2, 3, 0, 0, 0 }, { 3, 4, 0, 0, 0 } };
046:
047: int[][] lines = new int[][] {
048: // X1, Y1, X2, Y2, intersects
049: // Parallel on the right
050: { 0, 0, 3, 3, 0 },
051: { 1, 1, 2, 2, 0 },
052: { 3, 3, 5, 5, 0 },
053: // Parallel on the left
054: { -2, 0, 0, 2, 0 },
055: { 0, 2, 2, 4, 0 },
056: { 3, 5, 5, 7, 0 },
057: // Ortogonal behind
058: { 1, 0, 0, 1, 0 },
059: { 1, 0, -1, 2, 0 },
060: { 0, 1, -1, 2, 0 },
061: // Ortogonal forward
062: { 3, 6, 5, 4, 0 },
063: { 3, 6, 4, 5, 0 },
064: { 4, 5, 5, 4, 0 },
065: // Ortogonal on the right
066: { 2, 2, 3, 1, 0 },
067: { 3, 3, 4, 2, 0 },
068: // Ortogonal on the left
069: { 1, 3, 0, 4, 0 },
070: { 2, 4, 1, 5, 0 },
071: // Doesn't intersect
072: { 0, 0, 10, 3, 0 },
073: { 3, 3, 6, 4, 0 },
074: // Intersects
075: { 2, 0, 2, 5, 1 },
076: { 3, 2, 0, 4, 1 },
077: // Online
078: { -1, 0, 0, 1, 0 },
079: { 4, 5, 5, 6, 0 },
080: { 0, 1, 1, 2, 1 }, // touch start
081: { 0, 1, 2, 3, 1 },
082: { 0, 1, 3, 4, 1 },
083: { 0, 1, 5, 6, 1 }, // cover
084: { 1, 2, 3, 4, 1 }, // the same
085: { 3, 4, 4, 5, 1 }, // touch end
086: { 2, 3, 4, 5, 1 },
087: // Touch
088: { 1, 0, 1, 2, 1 }, { 0, 2, 1, 2, 1 }, { 2, 0, 2, 3, 1 },
089: { 0, 3, 3, 3, 1 }, { 3, 0, 3, 4, 1 }, { 0, 4, 3, 4, 1 },
090: { 0, 0, 0, 1, 0 }, { -1, 1, 0, 1, 0 }, { 4, 0, 4, 5, 0 },
091: { 0, 5, 4, 5, 0 } };
092:
093: public static int[][][] bounds = new int[][][] {
094: { { 1, 2, 3, 4 }, { 1, 2, 2, 2 } },
095: { { 1, 2, 0, 0 }, { 0, 0, 1, 2 } },
096: { { 1, 2, 0, 3 }, { 0, 2, 1, 1 } },
097: { { 1, 2, 3, 0 }, { 1, 0, 2, 2 } } };
098:
099: Line2D l;
100:
101: public Line2DTest(String name) {
102: super (name);
103: }
104:
105: @Override
106: protected void setUp() throws Exception {
107: super .setUp();
108: l = new Line2D.Double(1, 2, 3, 4);
109: }
110:
111: @Override
112: protected void tearDown() throws Exception {
113: l = null;
114: super .tearDown();
115: }
116:
117: public void testSetLine1() {
118: l.setLine(new Point(5, 6), new Point(7, 8));
119: assertEquals(new Line2D.Double(5, 6, 7, 8), l);
120: }
121:
122: public void testSetLine2() {
123: l.setLine(new Line2D.Double(5, 6, 7, 8));
124: assertEquals(new Line2D.Double(5, 6, 7, 8), l);
125: }
126:
127: public void testGetBounds() {
128: for (int[][] element : bounds) {
129: assertEquals(new Rectangle(element[1][0], element[1][1],
130: element[1][2], element[1][3]), new Line2D.Double(
131: element[0][0], element[0][1], element[0][2],
132: element[0][3]).getBounds());
133: }
134: }
135:
136: public void testClone() {
137: assertEquals(l, (Line2D) l.clone());
138: }
139:
140: public void testRelativeCCW1() {
141: for (double[] element : points) {
142: int x = (int) element[0];
143: int y = (int) element[1];
144: assertEquals("Line2D.relativeCCW(" + x + "," + y
145: + ") failed", (int) element[2], Line2D.relativeCCW(
146: l.getX1(), l.getY1(), l.getX2(), l.getY2(), x, y));
147: }
148: }
149:
150: public void testRelativeCCW2() {
151: for (double[] element : points) {
152: int x = (int) element[0];
153: int y = (int) element[1];
154: assertEquals("Line2D.relativeCCW(" + x + "," + y
155: + ") failed", (int) element[2], l.relativeCCW(x, y));
156: }
157: }
158:
159: public void testRelativeCCW3() {
160: for (double[] element : points) {
161: int x = (int) element[0];
162: int y = (int) element[1];
163: assertEquals("Line2D.relativeCCW(" + x + "," + y
164: + ") failed", (int) element[2], l
165: .relativeCCW(new Point(x, y)));
166: }
167: }
168:
169: public void testLinesIntersect() {
170: for (int[] element : lines) {
171: int x1 = element[0];
172: int y1 = element[1];
173: int x2 = element[2];
174: int y2 = element[3];
175: assertEquals("Line2D.intersectsLine(" + x1 + "," + y1 + ","
176: + x2 + "," + y2 + ") failed", element[4] == 1,
177: Line2D.linesIntersect(l.getX1(), l.getY1(), l
178: .getX2(), l.getY2(), x1, y1, x2, y2));
179: }
180: }
181:
182: public void testIntersectsLine1() {
183: for (int[] element : lines) {
184: int x1 = element[0];
185: int y1 = element[1];
186: int x2 = element[2];
187: int y2 = element[3];
188: assertEquals("Line2D.intersectsLine(" + x1 + "," + y1 + ","
189: + x2 + "," + y2 + ") failed", element[4] == 1, l
190: .intersectsLine(x1, y1, x2, y2));
191: }
192: }
193:
194: public void testIntersectsLine2() {
195: for (int[] element : lines) {
196: int x1 = element[0];
197: int y1 = element[1];
198: int x2 = element[2];
199: int y2 = element[3];
200: assertEquals("Line2D.intersectsLine(" + x1 + "," + y1 + ","
201: + x2 + "," + y2 + ") failed", element[4] == 1, l
202: .intersectsLine(new Line2D.Double(x1, y1, x2, y2)));
203: }
204: }
205:
206: public void testPtSegDistSq1() {
207: for (double[] element : points) {
208: int x = (int) element[0];
209: int y = (int) element[1];
210: assertEquals("Line2D.ptSegDistSq(" + x + "," + y
211: + ") failed", element[3], Line2D.ptSegDistSq(l
212: .getX1(), l.getY1(), l.getX2(), l.getY2(), x, y),
213: DELTA);
214: }
215: }
216:
217: public void testPtSegDistSq2() {
218: for (double[] element : points) {
219: int x = (int) element[0];
220: int y = (int) element[1];
221: assertEquals("Line2D.ptSegDistSq(" + x + "," + y
222: + ") failed", element[3], l.ptSegDistSq(x, y),
223: DELTA);
224: }
225: }
226:
227: public void testPtSegDistSq3() {
228: for (double[] element : points) {
229: int x = (int) element[0];
230: int y = (int) element[1];
231: assertEquals("Line2D.ptSegDistSq(" + x + "," + y
232: + ") failed", element[3], l.ptSegDistSq(new Point(
233: x, y)), DELTA);
234: }
235: }
236:
237: public void testPtSegDist1() {
238: for (double[] element : points) {
239: int x = (int) element[0];
240: int y = (int) element[1];
241: assertEquals(
242: "Line2D.ptSegDist(" + x + "," + y + ") failed",
243: Math.sqrt(element[3]), Line2D.ptSegDist(l.getX1(),
244: l.getY1(), l.getX2(), l.getY2(), x, y),
245: DELTA);
246: }
247: }
248:
249: public void testPtSegDist2() {
250: for (double[] element : points) {
251: int x = (int) element[0];
252: int y = (int) element[1];
253: assertEquals(
254: "Line2D.ptSegDist(" + x + "," + y + ") failed",
255: Math.sqrt(element[3]), l.ptSegDist(x, y), DELTA);
256: }
257: }
258:
259: public void testPtSegDist3() {
260: for (double[] element : points) {
261: int x = (int) element[0];
262: int y = (int) element[1];
263: assertEquals(
264: "Line2D.ptSegDist(" + x + "," + y + ") failed",
265: Math.sqrt(element[3]),
266: l.ptSegDist(new Point(x, y)), DELTA);
267: }
268: }
269:
270: public void testPtLineDistSq1() {
271: for (double[] element : points) {
272: int x = (int) element[0];
273: int y = (int) element[1];
274: assertEquals("Line2D.ptLineDistSq(" + x + "," + y
275: + ") failed", element[4], Line2D.ptLineDistSq(l
276: .getX1(), l.getY1(), l.getX2(), l.getY2(), x, y),
277: DELTA);
278: }
279: }
280:
281: public void testPtLineDistSq2() {
282: for (double[] element : points) {
283: int x = (int) element[0];
284: int y = (int) element[1];
285: assertEquals("Line2D.ptLineDistSq(" + x + "," + y
286: + ") failed", element[4], l.ptLineDistSq(x, y),
287: DELTA);
288: }
289: }
290:
291: public void testPtLineDistSq3() {
292: for (double[] element : points) {
293: int x = (int) element[0];
294: int y = (int) element[1];
295: assertEquals("Line2D.ptLineDistSq(" + x + "," + y
296: + ") failed", element[4], l.ptLineDistSq(new Point(
297: x, y)), DELTA);
298: }
299: }
300:
301: public void testPtLineDist1() {
302: for (double[] element : points) {
303: int x = (int) element[0];
304: int y = (int) element[1];
305: assertEquals("Line2D.ptLineDist(" + x + "," + y
306: + ") failed", Math.sqrt(element[4]), Line2D
307: .ptLineDist(l.getX1(), l.getY1(), l.getX2(), l
308: .getY2(), x, y), DELTA);
309: }
310: }
311:
312: public void testPtLineDist2() {
313: for (double[] element : points) {
314: int x = (int) element[0];
315: int y = (int) element[1];
316: assertEquals("Line2D.ptLineDist(" + x + "," + y
317: + ") failed", Math.sqrt(element[4]), l.ptLineDist(
318: x, y), DELTA);
319: }
320: }
321:
322: public void testPtLineDist3() {
323: for (double[] element : points) {
324: int x = (int) element[0];
325: int y = (int) element[1];
326: assertEquals("Line2D.ptLineDist(" + x + "," + y
327: + ") failed", Math.sqrt(element[4]), l
328: .ptLineDist(new Point(x, y)), DELTA);
329: }
330: }
331:
332: public void testContainsPoint() {
333: // Always returns false
334: assertFalse(l.contains(0, 0));
335: assertFalse(l.contains(1, 2));
336: assertFalse(l.contains(3, 4));
337: }
338:
339: public void testContainsPoint2() {
340: // Always returns false
341: assertFalse(l.contains(new Point(0, 0)));
342: assertFalse(l.contains(new Point(1, 2)));
343: assertFalse(l.contains(new Point(3, 4)));
344: }
345:
346: public void testContainsRect() {
347: // Always returns false
348: assertFalse(l.contains(1, 2, 3, 4));
349: }
350:
351: public void testContainsRect2() {
352: // Always returns false
353: assertFalse(l.contains(new Rectangle(1, 2, 3, 4)));
354: }
355:
356: public void testIntersects1() {
357: for (int i = 0; i < Rectangle2DTest.lines.length; i++) {
358: int x1 = Rectangle2DTest.lines[i][0];
359: int y1 = Rectangle2DTest.lines[i][1];
360: int x2 = Rectangle2DTest.lines[i][2];
361: int y2 = Rectangle2DTest.lines[i][3];
362: assertEquals("Line(" + x1 + "," + y1 + "," + x2 + "," + y2
363: + ") intersects Rectangle(1,2,3,4) #" + i,
364: Rectangle2DTest.lines[i][4] == 1,
365: new Line2D.Double(x1, y1, x2, y2).intersects(1, 2,
366: 3, 4));
367: }
368: }
369:
370: public void testIntersects2() {
371: for (int i = 0; i < Rectangle2DTest.lines.length; i++) {
372: int x1 = Rectangle2DTest.lines[i][0];
373: int y1 = Rectangle2DTest.lines[i][1];
374: int x2 = Rectangle2DTest.lines[i][2];
375: int y2 = Rectangle2DTest.lines[i][3];
376: assertEquals("Line(" + x1 + "," + y1 + "," + x2 + "," + y2
377: + ") intersects Rectangle(1,2,3,4) #" + i,
378: Rectangle2DTest.lines[i][4] == 1,
379: new Line2D.Double(x1, y1, x2, y2)
380: .intersects(new Rectangle(1, 2, 3, 4)));
381: }
382: }
383:
384: void checkPathIteratorDouble(PathIterator p, double[] values) {
385: checkPathRule(p, PathIterator.WIND_NON_ZERO);
386: checkPathMove(p, false, values[0], values[1], 0.0);
387: checkPathLine(p, true, values[2], values[3], 0.0);
388: }
389:
390: void checkPathIteratorFloat(PathIterator p, float[] values) {
391: checkPathRule(p, PathIterator.WIND_NON_ZERO);
392: checkPathMove(p, false, values[0], values[1], 0.0f);
393: checkPathLine(p, true, values[2], values[3], 0.0f);
394: }
395:
396: public void testGetPathIteratorDouble() {
397: checkPathIteratorDouble(l.getPathIterator(null), new double[] {
398: 1, 2, 3, 4 });
399: }
400:
401: public void testGetPathIteratorFloat() {
402: checkPathIteratorFloat(l.getPathIterator(null), new float[] {
403: 1, 2, 3, 4 });
404: }
405:
406: public void testGetPathIteratorDoubleFlat() {
407: checkPathIteratorDouble(l.getPathIterator(null, 2),
408: new double[] { 1, 2, 3, 4 });
409: }
410:
411: public void testGetPathIteratorFloatFlat() {
412: checkPathIteratorFloat(l.getPathIterator(null, 4), new float[] {
413: 1, 2, 3, 4 });
414: }
415:
416: public void testGetPathIteratorDoubleAffine() {
417: checkPathIteratorDouble(l.getPathIterator(AffineTransform
418: .getTranslateInstance(2, 1)),
419: new double[] { 3, 3, 5, 5 });
420: }
421:
422: public void testGetPathIteratorFloatAffine() {
423: checkPathIteratorFloat(l.getPathIterator(AffineTransform
424: .getTranslateInstance(2, 1)),
425: new float[] { 3, 3, 5, 5 });
426: }
427:
428: public static void main(String[] args) {
429: junit.textui.TestRunner.run(Line2DTest.class);
430: }
431:
432: }
|