summaryrefslogtreecommitdiffstats
path: root/awt/java/awt/geom/Ellipse2D.java
blob: 89fd0d06617da221f1932749f24da9a162b08451 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
/*
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 *  (the "License"); you may not use this file except in compliance with
 *  the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
/**
 * @author Denis M. Kishenko
 * @version $Revision$
 */

package java.awt.geom;

import java.util.NoSuchElementException;

import org.apache.harmony.awt.internal.nls.Messages;

/**
 * The Class Ellipse2D describes an ellipse defined by a rectangular area in
 * which it is inscribed.
 * 
 * @since Android 1.0
 */
public abstract class Ellipse2D extends RectangularShape {

    /**
     * The Class Float is the subclass of Ellipse2D that has all of its data
     * values stored with float-level precision.
     * 
     * @since Android 1.0
     */
    public static class Float extends Ellipse2D {

        /**
         * The x coordinate of the upper left corner of the ellipse's bounding
         * rectangle.
         */
        public float x;

        /**
         * The y coordinate of the upper left corner of the ellipse's bounding
         * rectangle.
         */
        public float y;

        /**
         * The width of the ellipse's bounding rectangle.
         */
        public float width;

        /**
         * The height of the ellipse's bounding rectangle.
         */
        public float height;

        /**
         * Instantiates a new float-valued Ellipse2D.
         */
        public Float() {
        }

        /**
         * Instantiates a new float-valued Ellipse2D with the specified data.
         * 
         * @param x
         *            the x coordinate of the upper left corner of the ellipse's
         *            bounding rectangle.
         * @param y
         *            the y coordinate of the upper left corner of the ellipse's
         *            bounding rectangle.
         * @param width
         *            the width of the ellipse's bounding rectangle.
         * @param height
         *            the height of the ellipse's bounding rectangle.
         */
        public Float(float x, float y, float width, float height) {
            setFrame(x, y, width, height);
        }

        @Override
        public double getX() {
            return x;
        }

        @Override
        public double getY() {
            return y;
        }

        @Override
        public double getWidth() {
            return width;
        }

        @Override
        public double getHeight() {
            return height;
        }

        @Override
        public boolean isEmpty() {
            return width <= 0.0 || height <= 0.0;
        }

        /**
         * Sets the data of the ellipse's bounding rectangle.
         * 
         * @param x
         *            the x coordinate of the upper left corner of the ellipse's
         *            bounding rectangle.
         * @param y
         *            the y coordinate of the upper left corner of the ellipse's
         *            bounding rectangle.
         * @param width
         *            the width of the ellipse's bounding rectangle.
         * @param height
         *            the height of the ellipse's bounding rectangle.
         */
        public void setFrame(float x, float y, float width, float height) {
            this.x = x;
            this.y = y;
            this.width = width;
            this.height = height;
        }

        @Override
        public void setFrame(double x, double y, double width, double height) {
            this.x = (float)x;
            this.y = (float)y;
            this.width = (float)width;
            this.height = (float)height;
        }

        public Rectangle2D getBounds2D() {
            return new Rectangle2D.Float(x, y, width, height);
        }
    }

    /**
     * The Class Double is the subclass of Ellipse2D that has all of its data
     * values stored with double-level precision.
     * 
     * @since Android 1.0
     */
    public static class Double extends Ellipse2D {

        /**
         * The x coordinate of the upper left corner of the ellipse's bounding
         * rectangle.
         */
        public double x;

        /**
         * The y coordinate of the upper left corner of the ellipse's bounding
         * rectangle.
         */
        public double y;

        /**
         * The width of the ellipse's bounding rectangle.
         */
        public double width;

        /**
         * The height of the ellipse's bounding rectangle.
         */
        public double height;

        /**
         * Instantiates a new double-valued Ellipse2D.
         */
        public Double() {
        }

        /**
         * Instantiates a new double-valued Ellipse2D with the specified data.
         * 
         * @param x
         *            the x coordinate of the upper left corner of the ellipse's
         *            bounding rectangle.
         * @param y
         *            the y coordinate of the upper left corner of the ellipse's
         *            bounding rectangle.
         * @param width
         *            the width of the ellipse's bounding rectangle.
         * @param height
         *            the height of the ellipse's bounding rectangle.
         */
        public Double(double x, double y, double width, double height) {
            setFrame(x, y, width, height);
        }

        @Override
        public double getX() {
            return x;
        }

        @Override
        public double getY() {
            return y;
        }

        @Override
        public double getWidth() {
            return width;
        }

        @Override
        public double getHeight() {
            return height;
        }

        @Override
        public boolean isEmpty() {
            return width <= 0.0 || height <= 0.0;
        }

        @Override
        public void setFrame(double x, double y, double width, double height) {
            this.x = x;
            this.y = y;
            this.width = width;
            this.height = height;
        }

        public Rectangle2D getBounds2D() {
            return new Rectangle2D.Double(x, y, width, height);
        }
    }

    /*
     * Ellipse2D path iterator
     */
    /**
     * The subclass of PathIterator to traverse an Ellipse2D.
     */
    class Iterator implements PathIterator {

        /*
         * Ellipse is subdivided into four quarters by x and y axis. Each part
         * approximated by cubic Bezier curve. Arc in first quarter is started
         * in (a, 0) and finished in (0, b) points. Control points for cubic
         * curve wiil be (a, 0), (a, m), (n, b) and (0, b) where n and m are
         * calculated based on requirement Bezier curve in point 0.5 should lay
         * on the arc.
         */

        /**
         * The coefficient to calculate control points of Bezier curves.
         */
        final double u = 2.0 / 3.0 * (Math.sqrt(2.0) - 1.0);

        /**
         * The points coordinates calculation table.
         */
        final double points[][] = {
                {
                        1.0, 0.5 + u, 0.5 + u, 1.0, 0.5, 1.0
                }, {
                        0.5 - u, 1.0, 0.0, 0.5 + u, 0.0, 0.5
                }, {
                        0.0, 0.5 - u, 0.5 - u, 0.0, 0.5, 0.0
                }, {
                        0.5 + u, 0.0, 1.0, 0.5 - u, 1.0, 0.5
                }
        };

        /**
         * The x coordinate of left-upper corner of the ellipse bounds.
         */
        double x;

        /**
         * The y coordinate of left-upper corner of the ellipse bounds.
         */
        double y;

        /**
         * The width of the ellipse bounds.
         */
        double width;

        /**
         * The height of the ellipse bounds.
         */
        double height;

        /**
         * The path iterator transformation.
         */
        AffineTransform t;

        /**
         * The current segment index.
         */
        int index;

        /**
         * Constructs a new Ellipse2D.Iterator for given ellipse and
         * transformation
         * 
         * @param e
         *            the source Ellipse2D object.
         * @param t
         *            the affine transformation object.
         */
        Iterator(Ellipse2D e, AffineTransform t) {
            this.x = e.getX();
            this.y = e.getY();
            this.width = e.getWidth();
            this.height = e.getHeight();
            this.t = t;
            if (width < 0.0 || height < 0.0) {
                index = 6;
            }
        }

        public int getWindingRule() {
            return WIND_NON_ZERO;
        }

        public boolean isDone() {
            return index > 5;
        }

        public void next() {
            index++;
        }

        public int currentSegment(double[] coords) {
            if (isDone()) {
                // awt.4B=Iterator out of bounds
                throw new NoSuchElementException(Messages.getString("awt.4B")); //$NON-NLS-1$
            }
            if (index == 5) {
                return SEG_CLOSE;
            }
            int type;
            int count;
            if (index == 0) {
                type = SEG_MOVETO;
                count = 1;
                double p[] = points[3];
                coords[0] = x + p[4] * width;
                coords[1] = y + p[5] * height;
            } else {
                type = SEG_CUBICTO;
                count = 3;
                double p[] = points[index - 1];
                int j = 0;
                for (int i = 0; i < 3; i++) {
                    coords[j] = x + p[j++] * width;
                    coords[j] = y + p[j++] * height;
                }
            }
            if (t != null) {
                t.transform(coords, 0, coords, 0, count);
            }
            return type;
        }

        public int currentSegment(float[] coords) {
            if (isDone()) {
                // awt.4B=Iterator out of bounds
                throw new NoSuchElementException(Messages.getString("awt.4B")); //$NON-NLS-1$
            }
            if (index == 5) {
                return SEG_CLOSE;
            }
            int type;
            int count;
            if (index == 0) {
                type = SEG_MOVETO;
                count = 1;
                double p[] = points[3];
                coords[0] = (float)(x + p[4] * width);
                coords[1] = (float)(y + p[5] * height);
            } else {
                type = SEG_CUBICTO;
                count = 3;
                int j = 0;
                double p[] = points[index - 1];
                for (int i = 0; i < 3; i++) {
                    coords[j] = (float)(x + p[j++] * width);
                    coords[j] = (float)(y + p[j++] * height);
                }
            }
            if (t != null) {
                t.transform(coords, 0, coords, 0, count);
            }
            return type;
        }

    }

    /**
     * Instantiates a new Ellipse2D.
     */
    protected Ellipse2D() {
    }

    public boolean contains(double px, double py) {
        if (isEmpty()) {
            return false;
        }

        double a = (px - getX()) / getWidth() - 0.5;
        double b = (py - getY()) / getHeight() - 0.5;

        return a * a + b * b < 0.25;
    }

    public boolean intersects(double rx, double ry, double rw, double rh) {
        if (isEmpty() || rw <= 0.0 || rh <= 0.0) {
            return false;
        }

        double cx = getX() + getWidth() / 2.0;
        double cy = getY() + getHeight() / 2.0;

        double rx1 = rx;
        double ry1 = ry;
        double rx2 = rx + rw;
        double ry2 = ry + rh;

        double nx = cx < rx1 ? rx1 : (cx > rx2 ? rx2 : cx);
        double ny = cy < ry1 ? ry1 : (cy > ry2 ? ry2 : cy);

        return contains(nx, ny);
    }

    public boolean contains(double rx, double ry, double rw, double rh) {
        if (isEmpty() || rw <= 0.0 || rh <= 0.0) {
            return false;
        }

        double rx1 = rx;
        double ry1 = ry;
        double rx2 = rx + rw;
        double ry2 = ry + rh;

        return contains(rx1, ry1) && contains(rx2, ry1) && contains(rx2, ry2) && contains(rx1, ry2);
    }

    public PathIterator getPathIterator(AffineTransform at) {
        return new Iterator(this, at);
    }
}