source: trunk/gcc/libjava/java/awt/geom/CubicCurve2D.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: 14.0 KB
Line 
1/* CubicCurve2D.java -- represents a parameterized cubic 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 CubicCurve2D implements Shape, Cloneable
50{
51 protected CubicCurve2D()
52 {
53 }
54
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();
67
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)
71 {
72 setCurve(coords[offset++], coords[offset++],
73 coords[offset++], coords[offset++],
74 coords[offset++], coords[offset++],
75 coords[offset++], coords[offset++]);
76 }
77 public void setCurve(Point2D p1, Point2D c1, Point2D c2, Point2D p2)
78 {
79 setCurve(p1.getX(), p1.getY(), c1.getX(), c1.getY(),
80 c2.getX(), c2.getY(), p2.getX(), p2.getY());
81 }
82 public void setCurve(Point2D[] pts, int offset)
83 {
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());
88 }
89 public void setCurve(CubicCurve2D c)
90 {
91 setCurve(c.getX1(), c.getY1(), c.getCtrlX1(), c.getCtrlY1(),
92 c.getCtrlX2(), c.getCtrlY2(), c.getX2(), c.getY2());
93 }
94 public static double getFlatnessSq(double x1, double y1, double cx1,
95 double cy1, double cx2, double cy2,
96 double x2, double y2)
97 {
98 // XXX Implement.
99 throw new Error("not implemented");
100 }
101 public static double getFlatness(double x1, double y1, double cx1,
102 double cy1, double cx2, double cy2,
103 double x2, double y2)
104 {
105 return Math.sqrt(getFlatnessSq(x1, y1, cx1, cy1, cx2, cy2, x2, y2));
106 }
107 public static double getFlatnessSq(double[] coords, int offset)
108 {
109 return getFlatnessSq(coords[offset++], coords[offset++],
110 coords[offset++], coords[offset++],
111 coords[offset++], coords[offset++],
112 coords[offset++], coords[offset++]);
113 }
114 public static double getFlatness(double[] coords, int offset)
115 {
116 return Math.sqrt(getFlatnessSq(coords[offset++], coords[offset++],
117 coords[offset++], coords[offset++],
118 coords[offset++], coords[offset++],
119 coords[offset++], coords[offset++]));
120 }
121 public double getFlatnessSq()
122 {
123 return getFlatnessSq(getX1(), getY1(), getCtrlX1(), getCtrlY1(),
124 getCtrlX2(), getCtrlY2(), getX2(), getY2());
125 }
126 public double getFlatness()
127 {
128 return Math.sqrt(getFlatnessSq(getX1(), getY1(), getCtrlX1(),
129 getCtrlY1(), getCtrlX2(), getCtrlY2(),
130 getX2(), getY2()));
131 }
132
133 public void subdivide(CubicCurve2D l, CubicCurve2D r)
134 {
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);
146 }
147 public static void subdivide(CubicCurve2D src,
148 CubicCurve2D l, CubicCurve2D r)
149 {
150 src.subdivide(l, r);
151 }
152 public static void subdivide(double[] src, int srcOff,
153 double[] left, int leftOff,
154 double[] right, int rightOff)
155 {
156 // XXX Implement.
157 throw new Error("not implemented");
158 }
159 public static int solveCubic(double[] eqn)
160 {
161 return solveCubic(eqn, eqn);
162 }
163 public static int solveCubic(double[] eqn, double[] res)
164 {
165 if (eqn[3] == 0)
166 return QuadCurve2D.solveQuadratic(eqn, res);
167 // XXX Implement.
168 throw new Error("not implemented");
169 }
170
171 public boolean contains(double x, double y)
172 {
173 // XXX Implement.
174 throw new Error("not implemented");
175 }
176 public boolean contains(Point2D p)
177 {
178 return contains(p.getX(), p.getY());
179 }
180 public boolean intersects(double x, double y, double w, double h)
181 {
182 // XXX Implement.
183 throw new Error("not implemented");
184 }
185 public boolean intersects(Rectangle2D r)
186 {
187 return intersects(r.getX(), r.getY(), r.getWidth(), r.getHeight());
188 }
189 public boolean contains(double x, double y, double w, double h)
190 {
191 // XXX Implement.
192 throw new Error("not implemented");
193 }
194 public boolean contains(Rectangle2D r)
195 {
196 return contains(r.getX(), r.getY(), r.getWidth(), r.getHeight());
197 }
198 public Rectangle getBounds()
199 {
200 return getBounds2D().getBounds();
201 }
202 public PathIterator getPathIterator(final AffineTransform at)
203 {
204 return new PathIterator()
205 {
206 /** Current coordinate. */
207 private int current;
208
209 public int getWindingRule()
210 {
211 return WIND_NON_ZERO;
212 }
213
214 public boolean isDone()
215 {
216 return current < 2;
217 }
218
219 public void next()
220 {
221 current++;
222 }
223
224 public int currentSegment(float[] coords)
225 {
226 if (current == 0)
227 {
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;
233 }
234 if (current == 1)
235 {
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;
245 }
246 throw new NoSuchElementException("cubic iterator out of bounds");
247 }
248
249 public int currentSegment(double[] coords)
250 {
251 if (current == 0)
252 {
253 coords[0] = getX1();
254 coords[1] = getY1();
255 if (at != null)
256 at.transform(coords, 0, coords, 0, 1);
257 return SEG_MOVETO;
258 }
259 if (current == 1)
260 {
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;
270 }
271 throw new NoSuchElementException("cubic iterator out of bounds");
272 }
273 };
274 }
275 public PathIterator getPathIterator(AffineTransform at, double flatness)
276 {
277 return new FlatteningPathIterator(getPathIterator(at), flatness);
278 }
279
280 /**
281 * Create a new curve of the same run-time type with the same contents as
282 * this one.
283 *
284 * @return the clone
285 */
286 public Object clone()
287 {
288 try
289 {
290 return super.clone();
291 }
292 catch (CloneNotSupportedException e)
293 {
294 throw (Error) new InternalError().initCause(e); // Impossible
295 }
296 }
297
298 /**
299 * STUBS ONLY
300 */
301 public static class Double extends CubicCurve2D
302 {
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;
311
312 public Double()
313 {
314 }
315
316 public Double(double x1, double y1, double cx1, double cy1,
317 double cx2, double cy2, double x2, double y2)
318 {
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;
327 }
328
329 public double getX1()
330 {
331 return x1;
332 }
333 public double getY1()
334 {
335 return y1;
336 }
337 public Point2D getP1()
338 {
339 return new Point2D.Double(x1, y1);
340 }
341
342 public double getCtrlX1()
343 {
344 return ctrlx1;
345 }
346 public double getCtrlY1()
347 {
348 return ctrly1;
349 }
350 public Point2D getCtrlP1()
351 {
352 return new Point2D.Double(ctrlx1, ctrly1);
353 }
354
355 public double getCtrlX2()
356 {
357 return ctrlx2;
358 }
359 public double getCtrlY2()
360 {
361 return ctrly2;
362 }
363 public Point2D getCtrlP2()
364 {
365 return new Point2D.Double(ctrlx2, ctrly2);
366 }
367
368 public double getX2()
369 {
370 return x2;
371 }
372 public double getY2()
373 {
374 return y2;
375 }
376 public Point2D getP2()
377 {
378 return new Point2D.Double(x2, y2);
379 }
380
381 public void setCurve(double x1, double y1, double cx1, double cy1,
382 double cx2, double cy2, double x2, double y2)
383 {
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;
392 }
393 public Rectangle2D getBounds2D()
394 {
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);
400 }
401 } // class Double
402
403 /**
404 * STUBS ONLY
405 */
406 public static class Float extends CubicCurve2D
407 {
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;
416
417 public Float()
418 {
419 }
420
421 public Float(float x1, float y1, float cx1, float cy1,
422 float cx2, float cy2, float x2, float y2)
423 {
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;
432 }
433
434 public double getX1()
435 {
436 return x1;
437 }
438 public double getY1()
439 {
440 return y1;
441 }
442 public Point2D getP1()
443 {
444 return new Point2D.Float(x1, y1);
445 }
446
447 public double getCtrlX1()
448 {
449 return ctrlx1;
450 }
451 public double getCtrlY1()
452 {
453 return ctrly1;
454 }
455 public Point2D getCtrlP1()
456 {
457 return new Point2D.Float(ctrlx1, ctrly1);
458 }
459
460 public double getCtrlX2()
461 {
462 return ctrlx2;
463 }
464 public double getCtrlY2()
465 {
466 return ctrly2;
467 }
468 public Point2D getCtrlP2()
469 {
470 return new Point2D.Float(ctrlx2, ctrly2);
471 }
472
473 public double getX2()
474 {
475 return x2;
476 }
477 public double getY2()
478 {
479 return y2;
480 }
481 public Point2D getP2()
482 {
483 return new Point2D.Float(x2, y2);
484 }
485
486 public void setCurve(double x1, double y1, double cx1, double cy1,
487 double cx2, double cy2, double x2, double y2)
488 {
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;
497 }
498 public void setCurve(float x1, float y1, float cx1, float cy1,
499 float cx2, float cy2, float x2, float y2)
500 {
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;
509 }
510 public Rectangle2D getBounds2D()
511 {
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);
517 }
518 } // class Float
519} // class CubicCurve2D
Note: See TracBrowser for help on using the repository browser.