1 |
/* Ellipse2D.java -- represents an ellipse in 2-D space |
/* Ellipse2D.java -- represents an ellipse in 2-D space |
2 |
Copyright (C) 2000, 2002 Free Software Foundation |
Copyright (C) 2000, 2002, 2004 Free Software Foundation |
3 |
|
|
4 |
This file is part of GNU Classpath. |
This file is part of GNU Classpath. |
5 |
|
|
37 |
|
|
38 |
package java.awt.geom; |
package java.awt.geom; |
39 |
|
|
40 |
|
|
41 |
/** |
/** |
42 |
|
* Ellipse2D is the shape of an ellipse. |
43 |
|
* <BR> |
44 |
|
* <img src="doc-files/Ellipse-1.png" width="347" height="221" |
45 |
|
* alt="A drawing of an ellipse" /><BR> |
46 |
|
* The ellipse is defined by it's bounding box (shown in red), |
47 |
|
* and is defined by the implicit curve:<BR> |
48 |
|
* <blockquote>(<i>x</i>/<i>a</i>)<sup>2</sup> + |
49 |
|
* (<i>y</i>/<i>b</i>)<sup>2</sup> = 1<BR><BR> |
50 |
|
* |
51 |
* @author Tom Tromey <tromey@cygnus.com> |
* @author Tom Tromey <tromey@cygnus.com> |
52 |
* @author Eric Blake <ebb9@email.byu.edu> |
* @author Eric Blake <ebb9@email.byu.edu> |
53 |
|
* |
54 |
* @since 1.2 |
* @since 1.2 |
|
* @status still needs documentation |
|
55 |
*/ |
*/ |
56 |
public abstract class Ellipse2D extends RectangularShape |
public abstract class Ellipse2D extends RectangularShape |
57 |
{ |
{ |
58 |
|
/** |
59 |
|
* Ellipse2D is defined as abstract. |
60 |
|
* Implementing classes are Ellipse2D.Float and Ellipse2D.Double. |
61 |
|
*/ |
62 |
protected Ellipse2D() |
protected Ellipse2D() |
63 |
{ |
{ |
64 |
} |
} |
65 |
|
|
66 |
|
/** |
67 |
|
* Determines if a point is contained within the ellipse. <P> |
68 |
|
* @param x - x coordinate of the point. |
69 |
|
* @param y - y coordinate of the point. |
70 |
|
* @return true if the point is within the ellipse, false otherwise. |
71 |
|
*/ |
72 |
public boolean contains(double x, double y) |
public boolean contains(double x, double y) |
73 |
{ |
{ |
74 |
double rx = getWidth() / 2; |
double rx = getWidth() / 2; |
75 |
double ry = getHeight() / 2; |
double ry = getHeight() / 2; |
76 |
double tx = (x - getCenterX()) / rx; |
double tx = (x - (getX() + rx)) / rx; |
77 |
double ty = (y - getCenterY()) / ry; |
double ty = (y - (getY() + ry)) / ry; |
78 |
return tx * tx + ty * ty <= 1.0; |
return tx * tx + ty * ty < 1.0; |
79 |
} |
} |
80 |
|
|
81 |
|
/** |
82 |
|
* Determines if a rectangle is completely contained within the |
83 |
|
* ellipse. <P> |
84 |
|
* @param x - x coordinate of the upper-left corner of the rectangle |
85 |
|
* @param y - y coordinate of the upper-left corner of the rectangle |
86 |
|
* @param w - width of the rectangle |
87 |
|
* @param h - height of the rectangle |
88 |
|
* @return true if the rectangle is completely contained, false otherwise. |
89 |
|
*/ |
90 |
public boolean contains(double x, double y, double w, double h) |
public boolean contains(double x, double y, double w, double h) |
91 |
{ |
{ |
92 |
double x2 = x + w; |
double x2 = x + w; |
93 |
double y2 = y + h; |
double y2 = y + h; |
94 |
return (contains(x, y) && contains(x, y2) |
return (contains(x, y) && contains(x, y2) && contains(x2, y) |
95 |
&& contains(x2, y) && contains(x2, y2)); |
&& contains(x2, y2)); |
96 |
} |
} |
97 |
|
|
98 |
|
/** |
99 |
|
* Returns a PathIterator object corresponding to the ellipse.<P> |
100 |
|
* |
101 |
|
* Note: An ellipse cannot be represented exactly in PathIterator |
102 |
|
* segments, the outline is thefore approximated with cubic |
103 |
|
* Bezier segments. |
104 |
|
*/ |
105 |
public PathIterator getPathIterator(AffineTransform at) |
public PathIterator getPathIterator(AffineTransform at) |
106 |
{ |
{ |
107 |
// An ellipse is just a complete arc. |
// An ellipse is just a complete arc. |
108 |
return new Arc2D.ArcIterator(this, at); |
return new Arc2D.ArcIterator(this, at); |
109 |
} |
} |
110 |
|
|
111 |
|
/** |
112 |
|
* Determines if a rectangle intersects any part of the ellipse.<P> |
113 |
|
* @param x - x coordinate of the upper-left corner of the rectangle |
114 |
|
* @param y - y coordinate of the upper-left corner of the rectangle |
115 |
|
* @param w - width of the rectangle |
116 |
|
* @param h - height of the rectangle |
117 |
|
* @return true if the rectangle intersects the ellipse, false otherwise. |
118 |
|
*/ |
119 |
public boolean intersects(double x, double y, double w, double h) |
public boolean intersects(double x, double y, double w, double h) |
120 |
{ |
{ |
121 |
// fixme |
Rectangle2D r = new Rectangle2D.Double(x, y, w, h); |
122 |
|
if (! r.intersects(getX(), getY(), getWidth(), getHeight())) |
123 |
|
return false; |
124 |
|
|
125 |
|
if (contains(x, y) || contains(x, y + h) || contains(x + w, y) |
126 |
|
|| contains(x + w, y + h)) |
127 |
|
return true; |
128 |
|
|
129 |
|
Line2D l1 = new Line2D.Double(getX(), getY() + (getHeight() / 2), |
130 |
|
getX() + getWidth(), |
131 |
|
getY() + (getHeight() / 2)); |
132 |
|
Line2D l2 = new Line2D.Double(getX() + (getWidth() / 2), getY(), |
133 |
|
getX() + (getWidth() / 2), |
134 |
|
getY() + getHeight()); |
135 |
|
|
136 |
|
if (l1.intersects(r) || l2.intersects(r)) |
137 |
|
return true; |
138 |
|
|
139 |
return false; |
return false; |
140 |
} |
} |
141 |
|
|
142 |
public static class Double extends Ellipse2D |
public static class Double extends Ellipse2D |
143 |
{ |
{ |
144 |
|
/** |
145 |
|
* The height of the ellipse. |
146 |
|
*/ |
147 |
public double height; |
public double height; |
148 |
|
|
149 |
|
/** |
150 |
|
* The width of the ellipse. |
151 |
|
*/ |
152 |
public double width; |
public double width; |
153 |
|
|
154 |
|
/** |
155 |
|
* The upper-left x coordinate of the bounding-box |
156 |
|
*/ |
157 |
public double x; |
public double x; |
158 |
|
|
159 |
|
/** |
160 |
|
* The upper-left y coordinate of the bounding-box |
161 |
|
*/ |
162 |
public double y; |
public double y; |
163 |
|
|
164 |
|
/** |
165 |
|
* Creates a new Ellipse2D with an upper-right coordinate of (0,0) |
166 |
|
* and a zero size. |
167 |
|
*/ |
168 |
public Double() |
public Double() |
169 |
{ |
{ |
170 |
} |
} |
171 |
|
|
172 |
|
/** |
173 |
|
* Creates a new Ellipse2D within a given rectangle |
174 |
|
* using double-precision coordinates.<P> |
175 |
|
* @param x - x coordinate of the upper-right of the bounding rectangle |
176 |
|
* @param y - y coordinate of the upper-right of the bounding rectangle |
177 |
|
* @param w - width of the ellipse |
178 |
|
* @param h - height of the ellipse |
179 |
|
* |
180 |
|
*/ |
181 |
public Double(double x, double y, double w, double h) |
public Double(double x, double y, double w, double h) |
182 |
{ |
{ |
183 |
this.x = x; |
this.x = x; |
186 |
width = w; |
width = w; |
187 |
} |
} |
188 |
|
|
189 |
|
/** |
190 |
|
* Returns the bounding-box of the ellipse. |
191 |
|
*/ |
192 |
public Rectangle2D getBounds2D() |
public Rectangle2D getBounds2D() |
193 |
{ |
{ |
194 |
return new Rectangle2D.Double(x, y, width, height); |
return new Rectangle2D.Double(x, y, width, height); |
195 |
} |
} |
196 |
|
|
197 |
|
/** |
198 |
|
* Returns the height of the ellipse. |
199 |
|
*/ |
200 |
public double getHeight() |
public double getHeight() |
201 |
{ |
{ |
202 |
return height; |
return height; |
203 |
} |
} |
204 |
|
|
205 |
|
/** |
206 |
|
* Returns the width of the ellipse. |
207 |
|
*/ |
208 |
public double getWidth() |
public double getWidth() |
209 |
{ |
{ |
210 |
return width; |
return width; |
211 |
} |
} |
212 |
|
|
213 |
|
/** |
214 |
|
* Returns x coordinate of the upper-left corner of |
215 |
|
* the ellipse's bounding-box. |
216 |
|
*/ |
217 |
public double getX() |
public double getX() |
218 |
{ |
{ |
219 |
return x; |
return x; |
220 |
} |
} |
221 |
|
|
222 |
|
/** |
223 |
|
* Returns y coordinate of the upper-left corner of |
224 |
|
* the ellipse's bounding-box. |
225 |
|
*/ |
226 |
public double getY() |
public double getY() |
227 |
{ |
{ |
228 |
return y; |
return y; |
229 |
} |
} |
230 |
|
|
231 |
|
/** |
232 |
|
* Returns true if the ellipse encloses any area. |
233 |
|
*/ |
234 |
public boolean isEmpty() |
public boolean isEmpty() |
235 |
{ |
{ |
236 |
return height <= 0 || width <= 0; |
return height <= 0 || width <= 0; |
237 |
} |
} |
238 |
|
|
239 |
|
/** |
240 |
|
* Sets the geometry of the ellipse's bounding box.<P> |
241 |
|
* |
242 |
|
* @param x - x coordinate of the upper-right of the bounding rectangle |
243 |
|
* @param y - y coordinate of the upper-right of the bounding rectangle |
244 |
|
* @param w - width of the ellipse |
245 |
|
* @param h - height of the ellipse |
246 |
|
*/ |
247 |
public void setFrame(double x, double y, double w, double h) |
public void setFrame(double x, double y, double w, double h) |
248 |
{ |
{ |
249 |
this.x = x; |
this.x = x; |
255 |
|
|
256 |
public static class Float extends Ellipse2D |
public static class Float extends Ellipse2D |
257 |
{ |
{ |
258 |
|
/** |
259 |
|
* The height of the ellipse. |
260 |
|
*/ |
261 |
public float height; |
public float height; |
262 |
|
|
263 |
|
/** |
264 |
|
* The width of the ellipse. |
265 |
|
*/ |
266 |
public float width; |
public float width; |
267 |
|
|
268 |
|
/** |
269 |
|
* The upper-left x coordinate of the bounding-box |
270 |
|
*/ |
271 |
public float x; |
public float x; |
272 |
|
|
273 |
|
/** |
274 |
|
* The upper-left y coordinate of the bounding-box |
275 |
|
*/ |
276 |
public float y; |
public float y; |
277 |
|
|
278 |
|
/** |
279 |
|
* Creates a new Ellipse2D with an upper-right coordinate of (0,0) |
280 |
|
* and a zero size. |
281 |
|
*/ |
282 |
public Float() |
public Float() |
283 |
{ |
{ |
284 |
} |
} |
285 |
|
|
286 |
|
/** |
287 |
|
* Creates a new Ellipse2D within a given rectangle |
288 |
|
* using floating-point precision.<P> |
289 |
|
* @param x - x coordinate of the upper-right of the bounding rectangle |
290 |
|
* @param y - y coordinate of the upper-right of the bounding rectangle |
291 |
|
* @param w - width of the ellipse |
292 |
|
* @param h - height of the ellipse |
293 |
|
* |
294 |
|
*/ |
295 |
public Float(float x, float y, float w, float h) |
public Float(float x, float y, float w, float h) |
296 |
{ |
{ |
297 |
this.x = x; |
this.x = x; |
300 |
this.width = w; |
this.width = w; |
301 |
} |
} |
302 |
|
|
303 |
|
/** |
304 |
|
* Returns the bounding-box of the ellipse. |
305 |
|
*/ |
306 |
public Rectangle2D getBounds2D() |
public Rectangle2D getBounds2D() |
307 |
{ |
{ |
308 |
return new Rectangle2D.Float(x, y, width, height); |
return new Rectangle2D.Float(x, y, width, height); |
309 |
} |
} |
310 |
|
|
311 |
|
/** |
312 |
|
* Returns the height of the ellipse. |
313 |
|
*/ |
314 |
public double getHeight() |
public double getHeight() |
315 |
{ |
{ |
316 |
return height; |
return height; |
317 |
} |
} |
318 |
|
|
319 |
|
/** |
320 |
|
* Returns the width of the ellipse. |
321 |
|
*/ |
322 |
public double getWidth() |
public double getWidth() |
323 |
{ |
{ |
324 |
return width; |
return width; |
325 |
} |
} |
326 |
|
|
327 |
|
/** |
328 |
|
* Returns x coordinate of the upper-left corner of |
329 |
|
* the ellipse's bounding-box. |
330 |
|
*/ |
331 |
public double getX() |
public double getX() |
332 |
{ |
{ |
333 |
return x; |
return x; |
334 |
} |
} |
335 |
|
|
336 |
|
/** |
337 |
|
* Returns y coordinate of the upper-left corner of |
338 |
|
* the ellipse's bounding-box. |
339 |
|
*/ |
340 |
public double getY() |
public double getY() |
341 |
{ |
{ |
342 |
return y; |
return y; |
343 |
} |
} |
344 |
|
|
345 |
|
/** |
346 |
|
* Returns true if the ellipse encloses any area. |
347 |
|
*/ |
348 |
public boolean isEmpty() |
public boolean isEmpty() |
349 |
{ |
{ |
350 |
return height <= 0 || width <= 0; |
return height <= 0 || width <= 0; |
351 |
} |
} |
352 |
|
|
353 |
|
/** |
354 |
|
* Sets the geometry of the ellipse's bounding box.<P> |
355 |
|
* |
356 |
|
* @param x - x coordinate of the upper-right of the bounding rectangle |
357 |
|
* @param y - y coordinate of the upper-right of the bounding rectangle |
358 |
|
* @param w - width of the ellipse |
359 |
|
* @param h - height of the ellipse |
360 |
|
*/ |
361 |
public void setFrame(float x, float y, float w, float h) |
public void setFrame(float x, float y, float w, float h) |
362 |
{ |
{ |
363 |
this.x = x; |
this.x = x; |
366 |
width = w; |
width = w; |
367 |
} |
} |
368 |
|
|
369 |
|
/** |
370 |
|
* Sets the geometry of the ellipse's bounding box. |
371 |
|
* |
372 |
|
* Note: This leads to a loss of precision.<P> |
373 |
|
* |
374 |
|
* @param x - x coordinate of the upper-right of the bounding rectangle |
375 |
|
* @param y - y coordinate of the upper-right of the bounding rectangle |
376 |
|
* @param w - width of the ellipse |
377 |
|
* @param h - height of the ellipse |
378 |
|
*/ |
379 |
public void setFrame(double x, double y, double w, double h) |
public void setFrame(double x, double y, double w, double h) |
380 |
{ |
{ |
381 |
this.x = (float) x; |
this.x = (float) x; |