source: trunk/gcc/libjava/java/awt/geom/QuadCurve2D.java

Last change on this file was 1389, checked in by bird, 21 years ago

Initial revision

  • Property cvs2svn:cvs-rev set to 1.1
  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 12.6 KB
Line 
1/* QuadCurve2D.java -- represents a parameterized quadratic curve in 2-D space
2 Copyright (C) 2002 Free Software Foundation
3
4This file is part of GNU Classpath.
5
6GNU Classpath is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2, or (at your option)
9any later version.
10
11GNU Classpath is distributed in the hope that it will be useful, but
12WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU Classpath; see the file COPYING. If not, write to the
18Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
1902111-1307 USA.
20
21Linking this library statically or dynamically with other modules is
22making a combined work based on this library. Thus, the terms and
23conditions of the GNU General Public License cover the whole
24combination.
25
26As a special exception, the copyright holders of this library give you
27permission to link this library with independent modules to produce an
28executable, regardless of the license terms of these independent
29modules, and to copy and distribute the resulting executable under
30terms of your choice, provided that you also meet, for each linked
31independent module, the terms and conditions of the license of that
32module. An independent module is a module which is not derived from
33or based on this library. If you modify this library, you may extend
34this exception to your version of the library, but you are not
35obligated to do so. If you do not wish to do so, delete this
36exception statement from your version. */
37
38
39package java.awt.geom;
40
41import java.awt.Rectangle;
42import java.awt.Shape;
43import java.util.NoSuchElementException;
44
45/**
46 * STUBS ONLY
47 * XXX Implement and document.
48 */
49public abstract class QuadCurve2D implements Shape, Cloneable
50{
51 protected QuadCurve2D()
52 {
53 }
54
55 public abstract double getX1();
56 public abstract double getY1();
57 public abstract Point2D getP1();
58 public abstract double getCtrlX();
59 public abstract double getCtrlY();
60 public abstract Point2D getCtrlPt();
61 public abstract double getX2();
62 public abstract double getY2();
63 public abstract Point2D getP2();
64
65 public abstract void setCurve(double x1, double y1, double cx, double cy,
66 double x2, double y2);
67 public void setCurve(double[] coords, int offset)
68 {
69 setCurve(coords[offset++], coords[offset++],
70 coords[offset++], coords[offset++],
71 coords[offset++], coords[offset++]);
72 }
73 public void setCurve(Point2D p1, Point2D c, Point2D p2)
74 {
75 setCurve(p1.getX(), p1.getY(), c.getX(), c.getY(),
76 p2.getX(), p2.getY());
77 }
78 public void setCurve(Point2D[] pts, int offset)
79 {
80 setCurve(pts[offset].getX(), pts[offset++].getY(),
81 pts[offset].getX(), pts[offset++].getY(),
82 pts[offset].getX(), pts[offset++].getY());
83 }
84 public void setCurve(QuadCurve2D c)
85 {
86 setCurve(c.getX1(), c.getY1(), c.getCtrlX(), c.getCtrlY(),
87 c.getX2(), c.getY2());
88 }
89 public static double getFlatnessSq(double x1, double y1, double cx,
90 double cy, double x2, double y2)
91 {
92 // XXX Implement.
93 throw new Error("not implemented");
94 }
95 public static double getFlatness(double x1, double y1, double cx, double cy,
96 double x2, double y2)
97 {
98 return Math.sqrt(getFlatnessSq(x1, y1, cx, cy, x2, y2));
99 }
100 public static double getFlatnessSq(double[] coords, int offset)
101 {
102 return getFlatnessSq(coords[offset++], coords[offset++],
103 coords[offset++], coords[offset++],
104 coords[offset++], coords[offset++]);
105 }
106 public static double getFlatness(double[] coords, int offset)
107 {
108 return Math.sqrt(getFlatnessSq(coords[offset++], coords[offset++],
109 coords[offset++], coords[offset++],
110 coords[offset++], coords[offset++]));
111 }
112 public double getFlatnessSq()
113 {
114 return getFlatnessSq(getX1(), getY1(), getCtrlX(), getCtrlY(),
115 getX2(), getY2());
116 }
117 public double getFlatness()
118 {
119 return Math.sqrt(getFlatnessSq(getX1(), getY1(), getCtrlX(), getCtrlY(),
120 getX2(), getY2()));
121 }
122
123 public void subdivide(QuadCurve2D l, QuadCurve2D r)
124 {
125 if (l == null)
126 l = new QuadCurve2D.Double();
127 if (r == null)
128 r = new QuadCurve2D.Double();
129 // Use empty slots at end to share single array.
130 double[] d = new double[] { getX1(), getY1(), getCtrlX(), getCtrlY(),
131 getX2(), getY2(), 0, 0, 0, 0 };
132 subdivide(d, 0, d, 0, d, 4);
133 l.setCurve(d, 0);
134 r.setCurve(d, 4);
135 }
136 public static void subdivide(QuadCurve2D src, QuadCurve2D l, QuadCurve2D r)
137 {
138 src.subdivide(l, r);
139 }
140 public static void subdivide(double[] src, int srcOff,
141 double[] left, int leftOff,
142 double[] right, int rightOff)
143 {
144 // XXX Implement.
145 throw new Error("not implemented");
146 }
147 public static int solveQuadratic(double[] eqn)
148 {
149 return solveQuadratic(eqn, eqn);
150 }
151 public static int solveQuadratic(double[] eqn, double[] res)
152 {
153 double c = eqn[0];
154 double b = eqn[1];
155 double a = eqn[2];
156 if (a == 0)
157 {
158 if (b == 0)
159 return -1;
160 res[0] = -c / b;
161 return 1;
162 }
163 c /= a;
164 b /= a * 2;
165 double det = Math.sqrt(b * b - c);
166 if (det != det)
167 return 0;
168 // For fewer rounding errors, we calculate the two roots differently.
169 if (b > 0)
170 {
171 res[0] = -b - det;
172 res[1] = -c / (b + det);
173 }
174 else
175 {
176 res[0] = -c / (b - det);
177 res[1] = -b + det;
178 }
179 return 2;
180 }
181
182 public boolean contains(double x, double y)
183 {
184 // XXX Implement.
185 throw new Error("not implemented");
186 }
187 public boolean contains(Point2D p)
188 {
189 return contains(p.getX(), p.getY());
190 }
191 public boolean intersects(double x, double y, double w, double h)
192 {
193 // XXX Implement.
194 throw new Error("not implemented");
195 }
196 public boolean intersects(Rectangle2D r)
197 {
198 return intersects(r.getX(), r.getY(), r.getWidth(), r.getHeight());
199 }
200 public boolean contains(double x, double y, double w, double h)
201 {
202 // XXX Implement.
203 throw new Error("not implemented");
204 }
205 public boolean contains(Rectangle2D r)
206 {
207 return contains(r.getX(), r.getY(), r.getWidth(), r.getHeight());
208 }
209 public Rectangle getBounds()
210 {
211 return getBounds2D().getBounds();
212 }
213 public PathIterator getPathIterator(final AffineTransform at)
214 {
215 return new PathIterator()
216 {
217 /** Current coordinate. */
218 private int current;
219
220 public int getWindingRule()
221 {
222 return WIND_NON_ZERO;
223 }
224
225 public boolean isDone()
226 {
227 return current < 2;
228 }
229
230 public void next()
231 {
232 current++;
233 }
234
235 public int currentSegment(float[] coords)
236 {
237 if (current == 0)
238 {
239 coords[0] = (float) getX1();
240 coords[1] = (float) getY1();
241 if (at != null)
242 at.transform(coords, 0, coords, 0, 1);
243 return SEG_MOVETO;
244 }
245 if (current == 1)
246 {
247 coords[0] = (float) getCtrlX();
248 coords[1] = (float) getCtrlY();
249 coords[2] = (float) getX2();
250 coords[3] = (float) getY2();
251 if (at != null)
252 at.transform(coords, 0, coords, 0, 2);
253 return SEG_QUADTO;
254 }
255 throw new NoSuchElementException("quad iterator out of bounds");
256 }
257
258 public int currentSegment(double[] coords)
259 {
260 if (current == 0)
261 {
262 coords[0] = getX1();
263 coords[1] = getY1();
264 if (at != null)
265 at.transform(coords, 0, coords, 0, 1);
266 return SEG_MOVETO;
267 }
268 if (current == 1)
269 {
270 coords[0] = getCtrlX();
271 coords[1] = getCtrlY();
272 coords[2] = getX2();
273 coords[3] = getY2();
274 if (at != null)
275 at.transform(coords, 0, coords, 0, 2);
276 return SEG_QUADTO;
277 }
278 throw new NoSuchElementException("quad iterator out of bounds");
279 }
280 };
281 }
282 public PathIterator getPathIterator(AffineTransform at, double flatness)
283 {
284 return new FlatteningPathIterator(getPathIterator(at), flatness);
285 }
286
287 /**
288 * Create a new curve of the same run-time type with the same contents as
289 * this one.
290 *
291 * @return the clone
292 *
293 * @exception OutOfMemoryError If there is not enough memory available.
294 *
295 * @since 1.2
296 */
297 public Object clone()
298 {
299 try
300 {
301 return super.clone();
302 }
303 catch (CloneNotSupportedException e)
304 {
305 throw (Error) new InternalError().initCause(e); // Impossible
306 }
307 }
308
309 /**
310 * STUBS ONLY
311 */
312 public static class Double extends QuadCurve2D
313 {
314 public double x1;
315 public double y1;
316 public double ctrlx;
317 public double ctrly;
318 public double x2;
319 public double y2;
320
321 public Double()
322 {
323 }
324
325 public Double(double x1, double y1, double cx, double cy,
326 double x2, double y2)
327 {
328 this.x1 = x1;
329 this.y1 = y1;
330 ctrlx = cx;
331 ctrly = cy;
332 this.x2 = x2;
333 this.y2 = y2;
334 }
335
336 public double getX1()
337 {
338 return x1;
339 }
340 public double getY1()
341 {
342 return y1;
343 }
344 public Point2D getP1()
345 {
346 return new Point2D.Double(x1, y1);
347 }
348
349 public double getCtrlX()
350 {
351 return ctrlx;
352 }
353 public double getCtrlY()
354 {
355 return ctrly;
356 }
357 public Point2D getCtrlPt()
358 {
359 return new Point2D.Double(ctrlx, ctrly);
360 }
361
362 public double getX2()
363 {
364 return x2;
365 }
366 public double getY2()
367 {
368 return y2;
369 }
370 public Point2D getP2()
371 {
372 return new Point2D.Double(x2, y2);
373 }
374
375 public void setCurve(double x1, double y1, double cx, double cy,
376 double x2, double y2)
377 {
378 this.x1 = x1;
379 this.y1 = y1;
380 ctrlx = cx;
381 ctrly = cy;
382 this.x2 = x2;
383 this.y2 = y2;
384 }
385 public Rectangle2D getBounds2D()
386 {
387 double nx1 = Math.min(Math.min(x1, ctrlx), x2);
388 double ny1 = Math.min(Math.min(y1, ctrly), y2);
389 double nx2 = Math.max(Math.max(x1, ctrlx), x2);
390 double ny2 = Math.max(Math.max(y1, ctrly), y2);
391 return new Rectangle2D.Double(nx1, ny1, nx2 - nx1, ny2 - ny1);
392 }
393 } // class Double
394
395 /**
396 * STUBS ONLY
397 */
398 public static class Float extends QuadCurve2D
399 {
400 public float x1;
401 public float y1;
402 public float ctrlx;
403 public float ctrly;
404 public float x2;
405 public float y2;
406
407 public Float()
408 {
409 }
410
411 public Float(float x1, float y1, float cx, float cy,
412 float x2, float y2)
413 {
414 this.x1 = x1;
415 this.y1 = y1;
416 ctrlx = cx;
417 ctrly = cy;
418 this.x2 = x2;
419 this.y2 = y2;
420 }
421
422 public double getX1()
423 {
424 return x1;
425 }
426 public double getY1()
427 {
428 return y1;
429 }
430 public Point2D getP1()
431 {
432 return new Point2D.Float(x1, y1);
433 }
434
435 public double getCtrlX()
436 {
437 return ctrlx;
438 }
439 public double getCtrlY()
440 {
441 return ctrly;
442 }
443 public Point2D getCtrlPt()
444 {
445 return new Point2D.Float(ctrlx, ctrly);
446 }
447
448 public double getX2()
449 {
450 return x2;
451 }
452 public double getY2()
453 {
454 return y2;
455 }
456 public Point2D getP2()
457 {
458 return new Point2D.Float(x2, y2);
459 }
460
461 public void setCurve(double x1, double y1, double cx, double cy,
462 double x2, double y2)
463 {
464 this.x1 = (float) x1;
465 this.y1 = (float) y1;
466 ctrlx = (float) cx;
467 ctrly = (float) cy;
468 this.x2 = (float) x2;
469 this.y2 = (float) y2;
470 }
471 public void setCurve(float x1, float y1, float cx, float cy,
472 float x2, float y2)
473 {
474 this.x1 = x1;
475 this.y1 = y1;
476 ctrlx = cx;
477 ctrly = cy;
478 this.x2 = x2;
479 this.y2 = y2;
480 }
481 public Rectangle2D getBounds2D()
482 {
483 float nx1 = (float) Math.min(Math.min(x1, ctrlx), x2);
484 float ny1 = (float) Math.min(Math.min(y1, ctrly), y2);
485 float nx2 = (float) Math.max(Math.max(x1, ctrlx), x2);
486 float ny2 = (float) Math.max(Math.max(y1, ctrly), y2);
487 return new Rectangle2D.Float(nx1, ny1, nx2 - nx1, ny2 - ny1);
488 }
489 } // class Float
490} // class CubicCurve2D
Note: See TracBrowser for help on using the repository browser.