FSF GCC merge 02/23/03
[official-gcc.git] / libjava / java / awt / geom / CubicCurve2D.java
blob2d303c7f6a754e3f68af7f8134a4ae591e8076e0
1 /* CubicCurve2D.java -- represents a parameterized cubic curve in 2-D space
2 Copyright (C) 2002 Free Software Foundation
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package java.awt.geom;
41 import java.awt.Rectangle;
42 import java.awt.Shape;
43 import java.util.NoSuchElementException;
45 /**
46 * STUBS ONLY
47 * XXX Implement and document.
49 public abstract class CubicCurve2D implements Shape, Cloneable
51 protected CubicCurve2D()
55 public abstract double getX1();
56 public abstract double getY1();
57 public abstract Point2D getP1();
58 public abstract double getCtrlX1();
59 public abstract double getCtrlY1();
60 public abstract Point2D getCtrlP1();
61 public abstract double getCtrlX2();
62 public abstract double getCtrlY2();
63 public abstract Point2D getCtrlP2();
64 public abstract double getX2();
65 public abstract double getY2();
66 public abstract Point2D getP2();
68 public abstract void setCurve(double x1, double y1, double cx1, double cy1,
69 double cx2, double cy2, double x2, double y2);
70 public void setCurve(double[] coords, int offset)
72 setCurve(coords[offset++], coords[offset++],
73 coords[offset++], coords[offset++],
74 coords[offset++], coords[offset++],
75 coords[offset++], coords[offset++]);
77 public void setCurve(Point2D p1, Point2D c1, Point2D c2, Point2D p2)
79 setCurve(p1.getX(), p1.getY(), c1.getX(), c1.getY(),
80 c2.getX(), c2.getY(), p2.getX(), p2.getY());
82 public void setCurve(Point2D[] pts, int offset)
84 setCurve(pts[offset].getX(), pts[offset++].getY(),
85 pts[offset].getX(), pts[offset++].getY(),
86 pts[offset].getX(), pts[offset++].getY(),
87 pts[offset].getX(), pts[offset++].getY());
89 public void setCurve(CubicCurve2D c)
91 setCurve(c.getX1(), c.getY1(), c.getCtrlX1(), c.getCtrlY1(),
92 c.getCtrlX2(), c.getCtrlY2(), c.getX2(), c.getY2());
94 public static double getFlatnessSq(double x1, double y1, double cx1,
95 double cy1, double cx2, double cy2,
96 double x2, double y2)
98 // XXX Implement.
99 throw new Error("not implemented");
101 public static double getFlatness(double x1, double y1, double cx1,
102 double cy1, double cx2, double cy2,
103 double x2, double y2)
105 return Math.sqrt(getFlatnessSq(x1, y1, cx1, cy1, cx2, cy2, x2, y2));
107 public static double getFlatnessSq(double[] coords, int offset)
109 return getFlatnessSq(coords[offset++], coords[offset++],
110 coords[offset++], coords[offset++],
111 coords[offset++], coords[offset++],
112 coords[offset++], coords[offset++]);
114 public static double getFlatness(double[] coords, int offset)
116 return Math.sqrt(getFlatnessSq(coords[offset++], coords[offset++],
117 coords[offset++], coords[offset++],
118 coords[offset++], coords[offset++],
119 coords[offset++], coords[offset++]));
121 public double getFlatnessSq()
123 return getFlatnessSq(getX1(), getY1(), getCtrlX1(), getCtrlY1(),
124 getCtrlX2(), getCtrlY2(), getX2(), getY2());
126 public double getFlatness()
128 return Math.sqrt(getFlatnessSq(getX1(), getY1(), getCtrlX1(),
129 getCtrlY1(), getCtrlX2(), getCtrlY2(),
130 getX2(), getY2()));
133 public void subdivide(CubicCurve2D l, CubicCurve2D r)
135 if (l == null)
136 l = new CubicCurve2D.Double();
137 if (r == null)
138 r = new CubicCurve2D.Double();
139 // Use empty slots at end to share single array.
140 double[] d = new double[] { getX1(), getY1(), getCtrlX1(), getCtrlY1(),
141 getCtrlX2(), getCtrlY2(), getX2(), getY2(),
142 0, 0, 0, 0, 0, 0 };
143 subdivide(d, 0, d, 0, d, 6);
144 l.setCurve(d, 0);
145 r.setCurve(d, 6);
147 public static void subdivide(CubicCurve2D src,
148 CubicCurve2D l, CubicCurve2D r)
150 src.subdivide(l, r);
152 public static void subdivide(double[] src, int srcOff,
153 double[] left, int leftOff,
154 double[] right, int rightOff)
156 // XXX Implement.
157 throw new Error("not implemented");
159 public static int solveCubic(double[] eqn)
161 return solveCubic(eqn, eqn);
163 public static int solveCubic(double[] eqn, double[] res)
165 if (eqn[3] == 0)
166 return QuadCurve2D.solveQuadratic(eqn, res);
167 // XXX Implement.
168 throw new Error("not implemented");
171 public boolean contains(double x, double y)
173 // XXX Implement.
174 throw new Error("not implemented");
176 public boolean contains(Point2D p)
178 return contains(p.getX(), p.getY());
180 public boolean intersects(double x, double y, double w, double h)
182 // XXX Implement.
183 throw new Error("not implemented");
185 public boolean intersects(Rectangle2D r)
187 return intersects(r.getX(), r.getY(), r.getWidth(), r.getHeight());
189 public boolean contains(double x, double y, double w, double h)
191 // XXX Implement.
192 throw new Error("not implemented");
194 public boolean contains(Rectangle2D r)
196 return contains(r.getX(), r.getY(), r.getWidth(), r.getHeight());
198 public Rectangle getBounds()
200 return getBounds2D().getBounds();
202 public PathIterator getPathIterator(final AffineTransform at)
204 return new PathIterator()
206 /** Current coordinate. */
207 private int current;
209 public int getWindingRule()
211 return WIND_NON_ZERO;
214 public boolean isDone()
216 return current < 2;
219 public void next()
221 current++;
224 public int currentSegment(float[] coords)
226 if (current == 0)
228 coords[0] = (float) getX1();
229 coords[1] = (float) getY1();
230 if (at != null)
231 at.transform(coords, 0, coords, 0, 1);
232 return SEG_MOVETO;
234 if (current == 1)
236 coords[0] = (float) getCtrlX1();
237 coords[1] = (float) getCtrlY1();
238 coords[2] = (float) getCtrlX2();
239 coords[3] = (float) getCtrlY2();
240 coords[4] = (float) getX2();
241 coords[5] = (float) getY2();
242 if (at != null)
243 at.transform(coords, 0, coords, 0, 3);
244 return SEG_CUBICTO;
246 throw new NoSuchElementException("cubic iterator out of bounds");
249 public int currentSegment(double[] coords)
251 if (current == 0)
253 coords[0] = getX1();
254 coords[1] = getY1();
255 if (at != null)
256 at.transform(coords, 0, coords, 0, 1);
257 return SEG_MOVETO;
259 if (current == 1)
261 coords[0] = getCtrlX1();
262 coords[1] = getCtrlY1();
263 coords[2] = getCtrlX2();
264 coords[3] = getCtrlY2();
265 coords[4] = getX2();
266 coords[5] = getY2();
267 if (at != null)
268 at.transform(coords, 0, coords, 0, 3);
269 return SEG_CUBICTO;
271 throw new NoSuchElementException("cubic iterator out of bounds");
275 public PathIterator getPathIterator(AffineTransform at, double flatness)
277 return new FlatteningPathIterator(getPathIterator(at), flatness);
281 * Create a new curve of the same run-time type with the same contents as
282 * this one.
284 * @return the clone
286 public Object clone()
290 return super.clone();
292 catch (CloneNotSupportedException e)
294 throw (Error) new InternalError().initCause(e); // Impossible
299 * STUBS ONLY
301 public static class Double extends CubicCurve2D
303 public double x1;
304 public double y1;
305 public double ctrlx1;
306 public double ctrly1;
307 public double ctrlx2;
308 public double ctrly2;
309 public double x2;
310 public double y2;
312 public Double()
316 public Double(double x1, double y1, double cx1, double cy1,
317 double cx2, double cy2, double x2, double y2)
319 this.x1 = x1;
320 this.y1 = y1;
321 ctrlx1 = cx1;
322 ctrly1 = cy1;
323 ctrlx2 = cx2;
324 ctrly2 = cy2;
325 this.x2 = x2;
326 this.y2 = y2;
329 public double getX1()
331 return x1;
333 public double getY1()
335 return y1;
337 public Point2D getP1()
339 return new Point2D.Double(x1, y1);
342 public double getCtrlX1()
344 return ctrlx1;
346 public double getCtrlY1()
348 return ctrly1;
350 public Point2D getCtrlP1()
352 return new Point2D.Double(ctrlx1, ctrly1);
355 public double getCtrlX2()
357 return ctrlx2;
359 public double getCtrlY2()
361 return ctrly2;
363 public Point2D getCtrlP2()
365 return new Point2D.Double(ctrlx2, ctrly2);
368 public double getX2()
370 return x2;
372 public double getY2()
374 return y2;
376 public Point2D getP2()
378 return new Point2D.Double(x2, y2);
381 public void setCurve(double x1, double y1, double cx1, double cy1,
382 double cx2, double cy2, double x2, double y2)
384 this.x1 = x1;
385 this.y1 = y1;
386 ctrlx1 = cx1;
387 ctrly1 = cy1;
388 ctrlx2 = cx2;
389 ctrly2 = cy2;
390 this.x2 = x2;
391 this.y2 = y2;
393 public Rectangle2D getBounds2D()
395 double nx1 = Math.min(Math.min(x1, ctrlx1), Math.min(ctrlx2, x2));
396 double ny1 = Math.min(Math.min(y1, ctrly1), Math.min(ctrly2, y2));
397 double nx2 = Math.max(Math.max(x1, ctrlx1), Math.max(ctrlx2, x2));
398 double ny2 = Math.max(Math.max(y1, ctrly1), Math.max(ctrly2, y2));
399 return new Rectangle2D.Double(nx1, ny1, nx2 - nx1, ny2 - ny1);
401 } // class Double
404 * STUBS ONLY
406 public static class Float extends CubicCurve2D
408 public float x1;
409 public float y1;
410 public float ctrlx1;
411 public float ctrly1;
412 public float ctrlx2;
413 public float ctrly2;
414 public float x2;
415 public float y2;
417 public Float()
421 public Float(float x1, float y1, float cx1, float cy1,
422 float cx2, float cy2, float x2, float y2)
424 this.x1 = x1;
425 this.y1 = y1;
426 ctrlx1 = cx1;
427 ctrly1 = cy1;
428 ctrlx2 = cx2;
429 ctrly2 = cy2;
430 this.x2 = x2;
431 this.y2 = y2;
434 public double getX1()
436 return x1;
438 public double getY1()
440 return y1;
442 public Point2D getP1()
444 return new Point2D.Float(x1, y1);
447 public double getCtrlX1()
449 return ctrlx1;
451 public double getCtrlY1()
453 return ctrly1;
455 public Point2D getCtrlP1()
457 return new Point2D.Float(ctrlx1, ctrly1);
460 public double getCtrlX2()
462 return ctrlx2;
464 public double getCtrlY2()
466 return ctrly2;
468 public Point2D getCtrlP2()
470 return new Point2D.Float(ctrlx2, ctrly2);
473 public double getX2()
475 return x2;
477 public double getY2()
479 return y2;
481 public Point2D getP2()
483 return new Point2D.Float(x2, y2);
486 public void setCurve(double x1, double y1, double cx1, double cy1,
487 double cx2, double cy2, double x2, double y2)
489 this.x1 = (float) x1;
490 this.y1 = (float) y1;
491 ctrlx1 = (float) cx1;
492 ctrly1 = (float) cy1;
493 ctrlx2 = (float) cx2;
494 ctrly2 = (float) cy2;
495 this.x2 = (float) x2;
496 this.y2 = (float) y2;
498 public void setCurve(float x1, float y1, float cx1, float cy1,
499 float cx2, float cy2, float x2, float y2)
501 this.x1 = x1;
502 this.y1 = y1;
503 ctrlx1 = cx1;
504 ctrly1 = cy1;
505 ctrlx2 = cx2;
506 ctrly2 = cy2;
507 this.x2 = x2;
508 this.y2 = y2;
510 public Rectangle2D getBounds2D()
512 float nx1 = (float) Math.min(Math.min(x1, ctrlx1), Math.min(ctrlx2, x2));
513 float ny1 = (float) Math.min(Math.min(y1, ctrly1), Math.min(ctrly2, y2));
514 float nx2 = (float) Math.max(Math.max(x1, ctrlx1), Math.max(ctrlx2, x2));
515 float ny2 = (float) Math.max(Math.max(y1, ctrly1), Math.max(ctrly2, y2));
516 return new Rectangle2D.Float(nx1, ny1, nx2 - nx1, ny2 - ny1);
518 } // class Float
519 } // class CubicCurve2D