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)
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
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
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
;
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
,
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(),
133 public void subdivide(CubicCurve2D l
, CubicCurve2D r
)
136 l
= new CubicCurve2D
.Double();
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(),
143 subdivide(d
, 0, d
, 0, d
, 6);
147 public static void subdivide(CubicCurve2D src
,
148 CubicCurve2D l
, CubicCurve2D r
)
152 public static void subdivide(double[] src
, int srcOff
,
153 double[] left
, int leftOff
,
154 double[] right
, int rightOff
)
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
)
166 return QuadCurve2D
.solveQuadratic(eqn
, res
);
168 throw new Error("not implemented");
171 public boolean contains(double x
, double y
)
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
)
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
)
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. */
209 public int getWindingRule()
211 return WIND_NON_ZERO
;
214 public boolean isDone()
224 public int currentSegment(float[] coords
)
228 coords
[0] = (float) getX1();
229 coords
[1] = (float) getY1();
231 at
.transform(coords
, 0, coords
, 0, 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();
243 at
.transform(coords
, 0, coords
, 0, 3);
246 throw new NoSuchElementException("cubic iterator out of bounds");
249 public int currentSegment(double[] coords
)
256 at
.transform(coords
, 0, coords
, 0, 1);
261 coords
[0] = getCtrlX1();
262 coords
[1] = getCtrlY1();
263 coords
[2] = getCtrlX2();
264 coords
[3] = getCtrlY2();
268 at
.transform(coords
, 0, coords
, 0, 3);
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
286 public Object
clone()
290 return super.clone();
292 catch (CloneNotSupportedException e
)
294 throw (Error
) new InternalError().initCause(e
); // Impossible
301 public static class Double
extends CubicCurve2D
305 public double ctrlx1
;
306 public double ctrly1
;
307 public double ctrlx2
;
308 public double ctrly2
;
316 public Double(double x1
, double y1
, double cx1
, double cy1
,
317 double cx2
, double cy2
, double x2
, double y2
)
329 public double getX1()
333 public double getY1()
337 public Point2D
getP1()
339 return new Point2D
.Double(x1
, y1
);
342 public double getCtrlX1()
346 public double getCtrlY1()
350 public Point2D
getCtrlP1()
352 return new Point2D
.Double(ctrlx1
, ctrly1
);
355 public double getCtrlX2()
359 public double getCtrlY2()
363 public Point2D
getCtrlP2()
365 return new Point2D
.Double(ctrlx2
, ctrly2
);
368 public double getX2()
372 public double getY2()
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
)
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
);
406 public static class Float
extends CubicCurve2D
421 public Float(float x1
, float y1
, float cx1
, float cy1
,
422 float cx2
, float cy2
, float x2
, float y2
)
434 public double getX1()
438 public double getY1()
442 public Point2D
getP1()
444 return new Point2D
.Float(x1
, y1
);
447 public double getCtrlX1()
451 public double getCtrlY1()
455 public Point2D
getCtrlP1()
457 return new Point2D
.Float(ctrlx1
, ctrly1
);
460 public double getCtrlX2()
464 public double getCtrlY2()
468 public Point2D
getCtrlP2()
470 return new Point2D
.Float(ctrlx2
, ctrly2
);
473 public double getX2()
477 public double getY2()
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
)
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
);
519 } // class CubicCurve2D