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.Dimension;
023: import java.awt.Point;
024: import java.awt.Rectangle;
025: import java.awt.geom.Arc2D;
026: import java.awt.geom.ShapeTestCase;
027:
028: public class Arc2DTest extends ShapeTestCase {
029:
030: final double DELTA = 0.001;
031: final double s2 = 2.0 / Math.sqrt(2.0);
032: final double s3 = 3.0 / Math.sqrt(2.0);
033:
034: // Test getStartPoint()/getEndPoint()
035: double angles[][] = new double[][] {
036: // Angle, point
037: // 90s angels
038: { 0, 5, 5 }, { 90, 3, 2 }, { 180, 1, 5 }, { 270, 3, 8 },
039: { 360, 5, 5 }, { 450, 3, 2 }, { -90, 3, 8 },
040: { -180, 1, 5 }, { -270, 3, 2 }, { -360, 5, 5 },
041: { -450, 3, 8 },
042: // 45s angle
043: { 45, 3 + s2, 5 - s3 }, { 135, 3 - s2, 5 - s3 },
044: { 225, 3 - s2, 5 + s3 }, { 315, 3 + s2, 5 + s3 } };
045:
046: // Test containsAngle(double)
047: double[][] arcs = new double[][] {
048: // Start, extent, angle, contains
049:
050: { 0, 10, 5, 1 }, { 0, 10, 15, 0 }, { 0, 10, 115, 0 },
051: { 0, 10, 365, 1 }, { 0, 10, 725, 1 }, { 0, 10, -355, 1 },
052: { 0, 10, -715, 1 },
053:
054: { 10, 370, 0, 1 }, { 10, 370, 90, 1 }, { 10, 370, 999, 1 },
055: { 10, 370, -999, 1 },
056:
057: { 135, -50, 84, 0 }, { 135, -50, 85, 1 },
058: { 135, -50, 135, 1 }, { 135, -50, 136, 0 },
059:
060: { -135, -50, -186, 0 }, { -135, -50, -185, 1 },
061: { -135, -50, -135, 1 }, { -135, -50, -134, 0 },
062:
063: { 435, 30, 74, 0 }, { 435, 30, 75, 1 },
064: { 435, 30, 105, 1 }, { 435, 30, 106, 0 },
065: { 435, 30, 800, 1 },
066:
067: { -435, 30, -76, 0 }, { -435, 30, -75, 1 },
068: { -435, 30, -45, 1 }, { -435, 30, -44, 0 },
069: { -435, 30, -770, 1 },
070:
071: { 200, 350, 20, 1 }
072:
073: };
074:
075: // Test getBounds()
076: double bounds[][] = new double[][] {
077: // Type, start, extent, bounds
078: { Arc2D.PIE, 0, 90, 3, 2, 5, 5 },
079: { Arc2D.CHORD, 0, 90, 3, 2, 5, 5 },
080: { Arc2D.OPEN, 0, 90, 3, 2, 5, 5 },
081:
082: { Arc2D.PIE, 0, 45, 3, 5 - s3, 5, 5 },
083: { Arc2D.CHORD, 0, 45, 3 + s2, 5 - s3, 5, 5 },
084: { Arc2D.OPEN, 0, 45, 3 + s2, 5 - s3, 5, 5 },
085:
086: { Arc2D.PIE, 45, 90, 3 - s2, 2, 3 + s2, 5 },
087: { Arc2D.CHORD, 45, 90, 3 - s2, 2, 3 + s2, 5 - s3 },
088: { Arc2D.OPEN, 45, 90, 3 - s2, 2, 3 + s2, 5 - s3 },
089:
090: { Arc2D.PIE, 135, 180, 1, 5 - s3, 3 + s2, 8 },
091: { Arc2D.CHORD, 135, 180, 1, 5 - s3, 3 + s2, 8 },
092: { Arc2D.OPEN, 135, 180, 1, 5 - s3, 3 + s2, 8 },
093:
094: { Arc2D.PIE, 225, 90, 3 - s2, 5, 3 + s2, 8 },
095: { Arc2D.CHORD, 225, 90, 3 - s2, 5 + s3, 3 + s2, 8 },
096: { Arc2D.OPEN, 225, 90, 3 - s2, 5 + s3, 3 + s2, 8 } };
097:
098: // Test SetAngleStart(Point2D)
099: double points1[][] = new double[][] {
100: // X, Y, angle
101: { 5, 5, 0 }, { 5, 3, 45 }, { 3, 3, 90 }, { 1, 3, 135 },
102: { 1, 5, 180 }, { 1, 7, 225 }, { 3, 7, 270 }, { 5, 7, 315 } };
103:
104: // Test setAngles(double, double, double, double)
105: double points2[][] = new double[][] {
106: // X1, Y1, X2, Y2, start, extent
107: // Full arc
108: { 5, 5, 5, 5, 0, 360 }, { 0, 2, 0, 2, 135, 360 },
109: { 1, 7, 1, 7, 225, 360 }, { 5, 7, 5, 7, 315, 360 },
110:
111: { 5, 3, 1, 3, 45, 90 }, { 5, 3, 5, 7, 45, 270 },
112:
113: { 1, 3, 5, 7, 135, 180 } };
114:
115: // Test setArcByTangent(double, double, double, double)
116: double tangent[][] = new double[][] {
117: // X1, Y1, X2, Y2, X3, Y3, radius, centerX, centerY, start, extent
118: // Quarter sectors
119: { 0, 5, 0, 0, 5, 0, 3, 3, 3, 180, 270 },
120: { 5, 0, 0, 0, 0, 5, 3, 3, 3, 90, 90 },
121:
122: { -5, 0, 0, 0, 0, 5, 3, -3, 3, 90, 270 },
123: { 0, 5, 0, 0, -5, 0, 3, -3, 3, 0, 90 },
124:
125: { 6, 7, 1, 2, -4, 7, 2 * s2, 1, 6, 45, 90 },
126: { -4, 7, 1, 2, 6, 7, 2 * s2, 1, 6, 135, 270 } };
127:
128: Arc2D.Double a;
129:
130: public Arc2DTest(String name) {
131: super (name);
132: // filterImage = createFilter("^(arc).*([.]ico)$", "(.*)((affine)|(flat)|(bounds))(.*)");
133: filterShape = createFilter("^(arc).*([.]shape)$", null);
134: }
135:
136: @Override
137: protected void setUp() throws Exception {
138: super .setUp();
139: a = new Arc2D.Double(1, 2, 4, 6, 45, 240 - 45, Arc2D.PIE);
140: }
141:
142: @Override
143: protected void tearDown() throws Exception {
144: a = null;
145: super .tearDown();
146: }
147:
148: public void testGetArcType() {
149: assertEquals("Arc type", Arc2D.PIE, a.getArcType());
150: }
151:
152: public void testSetArcType() {
153: a.setArcType(Arc2D.CHORD);
154: assertEquals("Arc type", Arc2D.CHORD, a.getArcType());
155:
156: // Regression for HARMONY-1403
157: try {
158: // Invalid type
159: a.setArcType(17);
160: fail("Expected IAE");
161: } catch (IllegalArgumentException e) {
162: // expected
163: }
164: }
165:
166: public void testGetStartPoint() {
167: for (double[] element : angles) {
168: double angle = element[0];
169: assertEquals("[angle=" + doubleToStr(angle)
170: + "] Start point", new Point2D.Double(element[1],
171: element[2]), new Arc2D.Double(1, 2, 4, 6, angle,
172: 10, Arc2D.PIE).getStartPoint(), DELTA);
173: }
174: }
175:
176: public void testGetEndPoint() {
177: for (double[] element : angles) {
178: double angle = element[0];
179: assertEquals(
180: "[angle=" + doubleToStr(angle) + "] End point",
181: new Point2D.Double(element[1], element[2]),
182: new Arc2D.Double(1, 2, 4, 6, 10, angle - 10,
183: Arc2D.PIE).getEndPoint(), DELTA);
184: }
185: }
186:
187: public void testGetBounds2D() {
188: for (double[] element : bounds) {
189: int type = (int) element[0];
190: double start = element[1];
191: double extent = element[2];
192: assertEquals("[start=" + doubleToStr(start) + ",extent="
193: + doubleToStr(extent) + ",type=" + type
194: + "] Bounds", new Rectangle2D.Double(element[3],
195: element[4], element[5] - element[3], element[6]
196: - element[4]), new Arc2D.Double(1, 2, 4, 6,
197: start, extent, type).getBounds2D(), DELTA);
198: }
199: }
200:
201: public void testContainsAngle() {
202: for (double[] element : arcs) {
203: double start = element[0];
204: double extent = element[1];
205: double angle = element[2];
206: assertEquals("[start=" + doubleToStr(start) + ",extent="
207: + extent + "] containsAngle(" + angle + ")",
208: element[3] == 1, new Arc2D.Double(1, 2, 4, 6,
209: start, extent, Arc2D.OPEN)
210: .containsAngle(angle));
211: }
212: }
213:
214: public void testSetFrame() {
215: int type = a.getArcType();
216: double start = a.getAngleStart();
217: double extent = a.getAngleExtent();
218: a.setFrame(7, 8, 9, 10);
219: assertEquals(
220: new Arc2D.Double(7, 8, 9, 10, start, extent, type), a);
221: }
222:
223: public void testSetArc1() {
224: a.setArc(new Point(7, 8), new Dimension(9, 10), 11, 12,
225: Arc2D.CHORD);
226: assertEquals(
227: new Arc2D.Double(7, 8, 9, 10, 11, 12, Arc2D.CHORD), a);
228: }
229:
230: public void testSetArc2() {
231: a.setArc(new Rectangle(7, 8, 9, 10), 11, 12, Arc2D.CHORD);
232: assertEquals(
233: new Arc2D.Double(7, 8, 9, 10, 11, 12, Arc2D.CHORD), a);
234: }
235:
236: public void testSetArc3() {
237: a.setArc(new Arc2D.Double(7, 8, 9, 10, 11, 12, Arc2D.CHORD));
238: assertEquals(
239: new Arc2D.Double(7, 8, 9, 10, 11, 12, Arc2D.CHORD), a);
240: }
241:
242: public void testSetArcByCenter() {
243: a.setArcByCenter(7, 8, 3, 11, 12, Arc2D.CHORD);
244: assertEquals(new Arc2D.Double(4, 5, 6, 6, 11, 12, Arc2D.CHORD),
245: a);
246: }
247:
248: public void testSetArcByTangent() {
249: for (double[] element : tangent) {
250: double x1 = element[0];
251: double y1 = element[1];
252: double x2 = element[2];
253: double y2 = element[3];
254: double x3 = element[4];
255: double y3 = element[5];
256: double radius = element[6];
257: a.setArcByTangent(new Point2D.Double(x1, y1),
258: new Point2D.Double(x2, y2), new Point2D.Double(x3,
259: y3), radius);
260: Arc2D a2 = new Arc2D.Double();
261: a2.setArcByCenter(element[7], element[8], radius,
262: element[9], element[10], a.getArcType());
263: assertEquals("Arc2d.setArcByTangent((" + doubleToStr(x1)
264: + "," + doubleToStr(y1) + "),(" + doubleToStr(x2)
265: + "," + doubleToStr(y2) + "),(" + doubleToStr(x3)
266: + "," + doubleToStr(y3) + "),"
267: + doubleToStr(radius) + ") failed", a2, a, DELTA);
268: }
269: }
270:
271: public void testSetAngleStart() {
272: for (double[] element : points1) {
273: double x = element[0];
274: double y = element[1];
275: double angle = element[2];
276: a.setAngleStart(new Point2D.Double(x, y));
277: assertEquals("Arc2d.setAngleStart(" + x + "," + y
278: + ") failed", angle, a.getAngleStart(), DELTA);
279: }
280: }
281:
282: public void testSetAngels1() {
283: for (double[] element : points2) {
284: double x1 = element[0];
285: double y1 = element[1];
286: double x2 = element[2];
287: double y2 = element[3];
288: double start = element[4];
289: double extent = element[5];
290: a.setAngles(x1, y1, x2, y2);
291: assertEquals("Arc2d.setAngles(" + x1 + "," + y1 + "," + x2
292: + "," + y2 + ") failed. Start angle", start, a
293: .getAngleStart(), DELTA);
294: assertEquals("Arc2d.setAngles(" + x1 + "," + y1 + "," + x2
295: + "," + y2 + ") failed. Extent angle", extent, a
296: .getAngleExtent(), DELTA);
297: }
298: }
299:
300: public void testSetAngels2() {
301: for (double[] element : points2) {
302: double x1 = element[0];
303: double y1 = element[1];
304: double x2 = element[2];
305: double y2 = element[3];
306: double start = element[4];
307: double extent = element[5];
308: a.setAngles(new Point2D.Double(x1, y1), new Point2D.Double(
309: x2, y2));
310: assertEquals("Arc2d.setAngles(" + x1 + "," + y1 + "," + x2
311: + "," + y2 + ") failed. Start angle", start, a
312: .getAngleStart(), DELTA);
313: assertEquals("Arc2d.setAngles(" + x1 + "," + y1 + "," + x2
314: + "," + y2 + ") failed. Extent angle", extent, a
315: .getAngleExtent(), DELTA);
316: }
317: }
318:
319: public void testGetPathIteratorEmpty() {
320: // Regression test HARMONY-1585
321: Arc2D a = new Arc2D.Double();
322: PathIterator p = a.getPathIterator(null);
323: checkPathMove(p, true, 0, 0, 0.0);
324: }
325:
326: public static void main(String[] args) {
327: junit.textui.TestRunner.run(Arc2DTest.class);
328: }
329:
330: }
|