1 /* File QuadBezier2D.java
2 *
3 * Project : geometry
4 *
5 * ===========================================
6 *
7 * This library is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as published by
9 * the Free Software Foundation, either version 2.1 of the License, or (at
10 * your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY, without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE.
15 *
16 * See the GNU Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this library. if not, write to :
20 * The Free Software Foundation, Inc., 59 Temple Place, Suite 330,
21 * Boston, MA 02111-1307, USA.
22 */
23
24 package math.geom2d.spline;
25
26 import math.geom2d.AffineTransform2D;
27 import math.geom2d.Box2D;
28 import math.geom2d.Point2D;
29 import math.geom2d.Shape2D;
30 import math.geom2d.Vector2D;
31 import math.geom2d.curve.Curve2D;
32 import math.geom2d.curve.Curve2DUtils;
33 import math.geom2d.curve.CurveSet2D;
34 import math.geom2d.curve.SmoothCurve2D;
35 import math.geom2d.domain.ContinuousOrientedCurve2D;
36 import math.geom2d.line.StraightLine2D;
37
38 /**
39 * An extension of the QuadCurve2D curve provided in java.awt.geom, with support
40 * for SmoothCurve2D and OrientedCurve2D.
41 *
42 * @deprecated replaced by QuadBezierCurve2D (0.7.1)
43 * @author Legland
44 */
45 @Deprecated
46 public class QuadBezier2D extends QuadBezierCurve2D
47 implements SmoothCurve2D, ContinuousOrientedCurve2D, Cloneable {
48
49 private static final long serialVersionUID = 1L;
50
51 // ===================================================================
52 // constructors
53
54 public QuadBezier2D() {
55 this(0, 0, 0, 0, 0, 0);
56 }
57
58 /**
59 * Build a new Bezier curve from its array of coefficients. The array must
60 * have size 2*3.
61 *
62 * @param coefs the coefficients of the QuadBezier2D.
63 */
64 public QuadBezier2D(double[][] coefs) {
65 this(coefs[0][0], coefs[1][0], coefs[0][0]+coefs[0][1]/2.0, coefs[1][0]
66 +coefs[1][1]/2.0, coefs[0][0]+coefs[0][1]+coefs[0][2],
67 coefs[1][0]+coefs[1][1]+coefs[1][2]);
68 }
69
70 /**
71 * Build a new quadratic Bezier curve by specifying position of extreme
72 * points and position of control point. The resulting curve is totally
73 * contained in the convex polygon formed by the 3 control points.
74 *
75 * @param p1 first point
76 * @param ctrl control point
77 * @param p2 last point
78 */
79 public QuadBezier2D(java.awt.geom.Point2D p1, java.awt.geom.Point2D ctrl,
80 java.awt.geom.Point2D p2) {
81 this(p1.getX(), p1.getY(), ctrl.getX(), ctrl.getY(), p2.getX(), p2
82 .getY());
83 }
84
85 public QuadBezier2D(java.awt.geom.Point2D[] pts) {
86 this(pts[0].getX(), pts[0].getY(), pts[1].getX(), pts[1].getY(), pts[2]
87 .getX(), pts[2].getY());
88 }
89
90 /**
91 * Build a new quadratic Bezier curve by specifying position of extreme
92 * points and position of control point. The resulting curve is totally
93 * contained in the convex polygon formed by the 3 control points.
94 */
95 public QuadBezier2D(double x1, double y1, double xctrl, double yctrl,
96 double x2, double y2) {
97 super(x1, y1, xctrl, yctrl, x2, y2);
98 }
99
100
101 /**
102 * Returns the bezier curve given by control points taken in reverse order.
103 */
104 @Override
105 public QuadBezier2D getReverseCurve() {
106 return new QuadBezier2D(this.getP2(), this.getControl(), this.getP1());
107 }
108
109 /**
110 * Computes portion of BezierCurve. If t1<t0, returns null.
111 */
112 @Override
113 public QuadBezier2D getSubCurve(double t0, double t1) {
114 t0 = Math.max(t0, 0);
115 t1 = Math.min(t1, 1);
116 if (t0>t1)
117 return null;
118
119 // Extreme points
120 Point2D p0 = getPoint(t0);
121 Point2D p1 = getPoint(t1);
122
123 // tangent vectors at extreme points
124 Vector2D v0 = getTangent(t0);
125 Vector2D v1 = getTangent(t1);
126
127 // compute position of control point as intersection of tangent lines
128 StraightLine2D tan0 = new StraightLine2D(p0, v0);
129 StraightLine2D tan1 = new StraightLine2D(p1, v1);
130 Point2D control = tan0.getIntersection(tan1);
131
132 // build the new quad curve
133 return new QuadBezier2D(p0, control, p1);
134 }
135
136 /**
137 * Clip the circle arc by a box. The result is an instance of
138 * ContinuousOrientedCurveSet2D<QuadBezier2D>, which contains only
139 * instances of EllipseArc2D. If the ellipse arc is not clipped, the result
140 * is an instance of ContinuousOrientedCurveSet2D<QuadBezier2D>
141 * which contains 0 curves.
142 */
143 @Override
144 public CurveSet2D<? extends QuadBezier2D> clip(Box2D box) {
145 // Clip the curve
146 CurveSet2D<SmoothCurve2D> set = Curve2DUtils.clipSmoothCurve(this, box);
147
148 // Stores the result in appropriate structure
149 CurveSet2D<QuadBezier2D> result = new CurveSet2D<QuadBezier2D>();
150
151 // convert the result
152 for (Curve2D curve : set.getCurves()) {
153 if (curve instanceof QuadBezier2D)
154 result.addCurve((QuadBezier2D) curve);
155 }
156 return result;
157 }
158
159 /**
160 * Returns the Bezier Curve transformed by the given AffineTransform2D. This
161 * is simply done by transforming control points of the curve.
162 */
163 @Override
164 public QuadBezier2D transform(AffineTransform2D trans) {
165 return new QuadBezier2D(
166 trans.transform(this.getP1()),
167 trans.transform(this.getControl()),
168 trans.transform(this.getP2()));
169 }
170
171 @Override
172 public boolean equals(Object obj) {
173 if(!(obj instanceof java.awt.geom.QuadCurve2D.Double))
174 return false;
175
176 java.awt.geom.QuadCurve2D.Double bezier =
177 (java.awt.geom.QuadCurve2D.Double) obj;
178 if(Math.abs(this.x1-bezier.x1)>Shape2D.ACCURACY) return false;
179 if(Math.abs(this.y1-bezier.y1)>Shape2D.ACCURACY) return false;
180 if(Math.abs(this.ctrlx-bezier.ctrlx)>Shape2D.ACCURACY) return false;
181 if(Math.abs(this.ctrly-bezier.ctrly)>Shape2D.ACCURACY) return false;
182 if(Math.abs(this.x2-bezier.x2)>Shape2D.ACCURACY) return false;
183 if(Math.abs(this.y2-bezier.y2)>Shape2D.ACCURACY) return false;
184
185 return true;
186 }
187
188 @Override
189 public QuadBezier2D clone() {
190 return new QuadBezier2D(x1, y1, ctrlx, ctrly, x2, y2);
191 }
192 }