Lines 11-18
Link Here
|
11 |
*******************************************************************************/ |
11 |
*******************************************************************************/ |
12 |
package org.eclipse.gef4.geometry.shapes; |
12 |
package org.eclipse.gef4.geometry.shapes; |
13 |
|
13 |
|
|
|
14 |
import java.io.Serializable; |
15 |
import java.util.Arrays; |
16 |
import java.util.HashSet; |
17 |
|
14 |
import org.eclipse.gef4.geometry.Point; |
18 |
import org.eclipse.gef4.geometry.Point; |
15 |
import org.eclipse.gef4.geometry.transform.AffineTransform; |
19 |
import org.eclipse.gef4.geometry.transform.AffineTransform; |
|
|
20 |
import org.eclipse.gef4.geometry.utils.PolynomCalculations; |
21 |
import org.eclipse.gef4.geometry.utils.PrecisionUtils; |
16 |
|
22 |
|
17 |
/** |
23 |
/** |
18 |
* Represents the geometric shape of a cubic Bézier curve. |
24 |
* Represents the geometric shape of a cubic Bézier curve. |
Lines 20-31
Link Here
|
20 |
* @author anyssen |
26 |
* @author anyssen |
21 |
* |
27 |
* |
22 |
*/ |
28 |
*/ |
23 |
public class CubicCurve implements Geometry { |
29 |
public class CubicCurve implements Geometry, Serializable { |
24 |
|
30 |
|
25 |
private static final long serialVersionUID = 1L; |
31 |
private static final long serialVersionUID = 1L; |
26 |
|
32 |
|
27 |
private double x1, y1, ctrl1X, ctrl1Y, ctrl2X, ctrl2Y, x2, y2; |
33 |
private double x1, y1, ctrl1X, ctrl1Y, ctrl2X, ctrl2Y, x2, y2; |
28 |
|
34 |
|
|
|
35 |
/** |
36 |
* Constructs a new {@link CubicCurve} object with the given control point |
37 |
* coordinates. |
38 |
* |
39 |
* @param x1 |
40 |
* x-coordinate of the start point |
41 |
* @param y1 |
42 |
* y-coordinate of the start point |
43 |
* @param ctrl1X |
44 |
* x-coordinate of the first control point |
45 |
* @param ctrl1Y |
46 |
* y-coordinate of the first control point |
47 |
* @param ctrl2X |
48 |
* x-coordinate of the second control point |
49 |
* @param ctrl2Y |
50 |
* y-coordinate of the second control point |
51 |
* @param x2 |
52 |
* x-coordinate of the end point |
53 |
* @param y2 |
54 |
* y-coordinate of the end point |
55 |
*/ |
29 |
public CubicCurve(double x1, double y1, double ctrl1X, double ctrl1Y, |
56 |
public CubicCurve(double x1, double y1, double ctrl1X, double ctrl1Y, |
30 |
double ctrl2X, double ctrl2Y, double x2, double y2) { |
57 |
double ctrl2X, double ctrl2Y, double x2, double y2) { |
31 |
this.x1 = x1; |
58 |
this.x1 = x1; |
Lines 38-43
Link Here
|
38 |
this.y2 = y2; |
65 |
this.y2 = y2; |
39 |
} |
66 |
} |
40 |
|
67 |
|
|
|
68 |
/** |
69 |
* Constructs a new {@link CubicCurve} object with the given control |
70 |
* {@link Point}s. |
71 |
* |
72 |
* @param start |
73 |
* the start point |
74 |
* @param ctrl1 |
75 |
* the first control point |
76 |
* @param ctrl2 |
77 |
* the second control point |
78 |
* @param end |
79 |
* the end point |
80 |
*/ |
41 |
public CubicCurve(Point start, Point ctrl1, Point ctrl2, Point end) { |
81 |
public CubicCurve(Point start, Point ctrl1, Point ctrl2, Point end) { |
42 |
this.x1 = start.x; |
82 |
this.x1 = start.x; |
43 |
this.y1 = start.y; |
83 |
this.y1 = start.y; |
Lines 49-186
Link Here
|
49 |
this.y2 = end.y; |
89 |
this.y2 = end.y; |
50 |
} |
90 |
} |
51 |
|
91 |
|
52 |
/* |
92 |
/** |
53 |
* (non-Javadoc) |
93 |
* @see Geometry#contains(Point) |
54 |
* |
|
|
55 |
* @see |
56 |
* org.eclipse.gef4.geometry.shapes.Geometry#contains(org.eclipse.gef4.geometry |
57 |
* .Point) |
58 |
*/ |
94 |
*/ |
59 |
public boolean contains(Point p) { |
95 |
public boolean contains(Point p) { |
60 |
// TODO Auto-generated method stub |
96 |
// find roots of the x(t) - p.x function: |
|
|
97 |
double D = getX1() - p.x; |
98 |
double C = 3 * (getCtrl1X() - getX1()); |
99 |
double B = 3 * (getCtrl2X() - getCtrl1X()) - C; |
100 |
double A = getX2() - getX1() - B - C; |
101 |
double[] xts = PolynomCalculations.getCubicRoots(A, B, C, D); |
102 |
|
103 |
for (double t : xts) { |
104 |
// t = PrecisionUtils.round(t); |
105 |
if (PrecisionUtils.greaterEqual(t, 0) |
106 |
&& PrecisionUtils.smallerEqual(t, 1) |
107 |
&& PrecisionUtils.equal(get(t).y, p.y)) { |
108 |
return true; |
109 |
} |
110 |
} |
61 |
return false; |
111 |
return false; |
62 |
} |
112 |
} |
63 |
|
113 |
|
64 |
/* |
114 |
/** |
65 |
* (non-Javadoc) |
115 |
* @see Geometry#contains(Rectangle) |
66 |
* |
|
|
67 |
* @see |
68 |
* org.eclipse.gef4.geometry.shapes.Geometry#contains(org.eclipse.gef4.geometry |
69 |
* .shapes.Rectangle) |
70 |
*/ |
116 |
*/ |
71 |
public boolean contains(Rectangle r) { |
117 |
public boolean contains(Rectangle r) { |
72 |
// TODO Auto-generated method stub |
|
|
73 |
return false; |
118 |
return false; |
74 |
} |
119 |
} |
75 |
|
120 |
|
76 |
/* |
121 |
@Override |
77 |
* (non-Javadoc) |
122 |
public boolean equals(Object other) { |
78 |
* |
123 |
CubicCurve o = (CubicCurve) other; |
79 |
* @see org.eclipse.gef4.geometry.shapes.Geometry#getBounds() |
124 |
|
|
|
125 |
Polygon myPoly = getControlPolygon(); |
126 |
Polygon otherPoly = o.getControlPolygon(); |
127 |
|
128 |
return myPoly.equals(otherPoly); |
129 |
} |
130 |
|
131 |
/** |
132 |
* @see Geometry#getBounds() |
80 |
*/ |
133 |
*/ |
81 |
public Rectangle getBounds() { |
134 |
public Rectangle getBounds() { |
82 |
// TODO Auto-generated method stub |
135 |
// extremes of the x(t) and y(t) functions: |
83 |
return null; |
136 |
double[] xts; |
|
|
137 |
try { |
138 |
xts = PolynomCalculations.getQuadraticRoots(-3 * getX1() + 9 |
139 |
* getCtrl1X() - 9 * getCtrl2X() + 3 * getX2(), 6 * getX1() |
140 |
- 12 * getCtrl1X() + 6 * getCtrl2X(), 3 * getCtrl1X() - 3 |
141 |
* getX1()); |
142 |
} catch (ArithmeticException x) { |
143 |
return new Rectangle(getP1(), getP2()); |
144 |
} |
145 |
|
146 |
double xmin = getX1(), xmax = getX1(); |
147 |
if (getX2() < xmin) { |
148 |
xmin = getX2(); |
149 |
} else { |
150 |
xmax = getX2(); |
151 |
} |
152 |
|
153 |
for (double t : xts) { |
154 |
if (t >= 0 && t <= 1) { |
155 |
double x = get(t).x; |
156 |
if (x < xmin) { |
157 |
xmin = x; |
158 |
} else if (x > xmax) { |
159 |
xmax = x; |
160 |
} |
161 |
} |
162 |
} |
163 |
|
164 |
double[] yts; |
165 |
try { |
166 |
yts = PolynomCalculations.getQuadraticRoots(-3 * getY1() + 9 |
167 |
* getCtrl1Y() - 9 * getCtrl2Y() + 3 * getY2(), 6 * getY1() |
168 |
- 12 * getCtrl1Y() + 6 * getCtrl2Y(), 3 * getCtrl1Y() - 3 |
169 |
* getY1()); |
170 |
} catch (ArithmeticException x) { |
171 |
return new Rectangle(new Point(xmin, getP1().y), new Point(xmax, |
172 |
getP2().y)); |
173 |
} |
174 |
|
175 |
double ymin = getY1(), ymax = getY1(); |
176 |
if (getY2() < ymin) { |
177 |
ymin = getY2(); |
178 |
} else { |
179 |
ymax = getY2(); |
180 |
} |
181 |
|
182 |
for (double t : yts) { |
183 |
if (t >= 0 && t <= 1) { |
184 |
double y = get(t).y; |
185 |
if (y < ymin) { |
186 |
ymin = y; |
187 |
} else if (y > ymax) { |
188 |
ymax = y; |
189 |
} |
190 |
} |
191 |
} |
192 |
|
193 |
return new Rectangle(new Point(xmin, ymin), new Point(xmax, ymax)); |
194 |
} |
195 |
|
196 |
private Point ratioPoint(Point p, Point q, double ratio) { |
197 |
return p.getTranslated(q.getTranslated(p.getNegated()).getScaled(ratio)); |
198 |
} |
199 |
|
200 |
/** |
201 |
* Subdivides this {@link CubicCurve} into two {@link CubicCurve}s on the |
202 |
* intervals [0, t] and [t, 1] using the de-Casteljau-algorithm. |
203 |
* |
204 |
* @param t |
205 |
* split point's parameter value |
206 |
* @return the two {@link CubicCurve}s |
207 |
*/ |
208 |
public CubicCurve[] split(double t) { |
209 |
if (t < 0 || t > 1) { |
210 |
throw new IllegalArgumentException( |
211 |
"Paramter t is out of range! t = " + t + " !in_range(0,1)"); |
212 |
} |
213 |
|
214 |
Point p10 = ratioPoint(getP1(), getCtrl1(), t); |
215 |
Point p11 = ratioPoint(getCtrl1(), getCtrl2(), t); |
216 |
Point p12 = ratioPoint(getCtrl2(), getP2(), t); |
217 |
Point p20 = ratioPoint(p10, p11, t); |
218 |
Point p21 = ratioPoint(p11, p12, t); |
219 |
Point p30 = ratioPoint(p20, p21, t); |
220 |
|
221 |
CubicCurve left = new CubicCurve(getP1(), p10, p20, p30); |
222 |
CubicCurve right = new CubicCurve(p30, p21, p12, getP2()); |
223 |
|
224 |
return new CubicCurve[] { left, right }; |
225 |
} |
226 |
|
227 |
/** |
228 |
* Clips this {@link CubicCurve} at parameter values t1 and t2 so that the |
229 |
* resulting {@link CubicCurve} is the section of the original |
230 |
* {@link CubicCurve} for the parameter interval [t1, t2]. |
231 |
* |
232 |
* @param t1 |
233 |
* @param t2 |
234 |
* @return the {@link CubicCurve} on the interval [t1, t2] |
235 |
*/ |
236 |
public CubicCurve clip(double t1, double t2) { |
237 |
if (t1 < 0 || t1 > 1) { |
238 |
throw new IllegalArgumentException( |
239 |
"Paramter t1 is out of range! t1 = " + t1 |
240 |
+ " !in_range(0,1)"); |
241 |
} |
242 |
if (t2 < 0 || t2 > 1) { |
243 |
throw new IllegalArgumentException( |
244 |
"Paramter t2 is out of range! t2 = " + t2 |
245 |
+ " !in_range(0,1)"); |
246 |
} |
247 |
|
248 |
CubicCurve right = split(t1)[1]; |
249 |
double rightT2 = (t2 - t1) / (1 - t1); |
250 |
return right.split(rightT2)[0]; |
251 |
} |
252 |
|
253 |
private static double getArea(Polygon p) { |
254 |
Rectangle r = p.getBounds(); |
255 |
return r.getWidth() * r.getHeight(); |
256 |
} |
257 |
|
258 |
private Polygon getControlPolygon() { |
259 |
return new Polygon(getP1(), getCtrl1(), getCtrl2(), getP2()); |
260 |
} |
261 |
|
262 |
private static Point[] getIntersections(CubicCurve p, double ps, double pe, |
263 |
Line l) { |
264 |
// parameter convergence test |
265 |
double pm = (ps + pe) / 2; |
266 |
|
267 |
if (PrecisionUtils.equal(ps, pe, -2)) { |
268 |
return new Point[] { p.get(pm) }; |
269 |
} |
270 |
|
271 |
// no parameter convergence |
272 |
// clip the curve |
273 |
CubicCurve pc = p.clip(ps, pe); |
274 |
|
275 |
// check the control polygon |
276 |
Polygon polygon = pc.getControlPolygon(); |
277 |
|
278 |
if (polygon.intersects(l)) { |
279 |
// area test |
280 |
if (PrecisionUtils.equal(getArea(polygon), 0, -2)) { |
281 |
// line/line intersection fallback for such small curves |
282 |
Point poi = new Line(pc.getP1(), pc.getP2()).getIntersection(l); |
283 |
if (poi != null) { |
284 |
return new Point[] { poi }; |
285 |
} |
286 |
return new Point[] {}; |
287 |
} |
288 |
|
289 |
// "split" the curve to get precise intersections |
290 |
HashSet<Point> intersections = new HashSet<Point>(); |
291 |
|
292 |
intersections.addAll(Arrays.asList(getIntersections(p, ps, pm, l))); |
293 |
intersections.addAll(Arrays.asList(getIntersections(p, pm, pe, l))); |
294 |
|
295 |
return intersections.toArray(new Point[] {}); |
296 |
} |
297 |
|
298 |
// no intersections |
299 |
return new Point[] {}; |
300 |
} |
301 |
|
302 |
private static Point[] getIntersections(CubicCurve p, double ps, double pe, |
303 |
CubicCurve q, double qs, double qe) { |
304 |
double pm = (ps + pe) / 2; |
305 |
double qm = (qs + qe) / 2; |
306 |
|
307 |
// point convergence test |
308 |
Point pPoi = p.get(pm); |
309 |
Point qPoi = q.get(qm); |
310 |
|
311 |
if (pPoi != null && qPoi != null && pPoi.equals(qPoi)) { |
312 |
return new Point[] { pPoi }; |
313 |
} |
314 |
|
315 |
// no point convergence yet |
316 |
// clip to parameter ranges |
317 |
CubicCurve pc = p.clip(ps, pe); |
318 |
CubicCurve qc = q.clip(qs, qe); |
319 |
|
320 |
// check the control polygons |
321 |
Polygon pPoly = pc.getControlPolygon(); |
322 |
Polygon qPoly = qc.getControlPolygon(); |
323 |
|
324 |
if (pPoly.intersects(qPoly)) { |
325 |
// check the polygon's areas |
326 |
double pArea = getArea(pPoly); |
327 |
double qArea = getArea(qPoly); |
328 |
|
329 |
if (PrecisionUtils.equal(pArea, 0, +2) |
330 |
&& PrecisionUtils.equal(qArea, 0, +2)) { |
331 |
// return line/line intersection |
332 |
Point poi = new Line(pc.getP1(), pc.getP2()) |
333 |
.getIntersection(new Line(qc.getP1(), qc.getP2())); |
334 |
if (poi != null) { |
335 |
return new Point[] { poi }; |
336 |
} |
337 |
return new Point[] {}; |
338 |
} |
339 |
|
340 |
// areas not small enough |
341 |
|
342 |
// do not try to find the intersections of two equal curves |
343 |
if (pc.equals(qc)) { |
344 |
return new Point[] {}; |
345 |
} |
346 |
|
347 |
// "split" the curves and do the intersection test recursively |
348 |
HashSet<Point> intersections = new HashSet<Point>(); |
349 |
|
350 |
intersections.addAll(Arrays.asList(getIntersections(p.clip(ps, pm), |
351 |
0, 1, q.clip(qs, qm), 0, 1))); |
352 |
intersections.addAll(Arrays.asList(getIntersections(p.clip(ps, pm), |
353 |
0, 1, q.clip(qm, qe), 0, 1))); |
354 |
intersections.addAll(Arrays.asList(getIntersections(p.clip(pm, pe), |
355 |
0, 1, q.clip(qs, qm), 0, 1))); |
356 |
intersections.addAll(Arrays.asList(getIntersections(p.clip(pm, pe), |
357 |
0, 1, q.clip(qm, qe), 0, 1))); |
358 |
|
359 |
// intersections.addAll(Arrays.asList(getIntersections(p, ps, pm, q, |
360 |
// qs, qm))); |
361 |
// intersections.addAll(Arrays.asList(getIntersections(p, ps, pm, q, |
362 |
// qm, qe))); |
363 |
// intersections.addAll(Arrays.asList(getIntersections(p, pm, pe, q, |
364 |
// qs, qm))); |
365 |
// intersections.addAll(Arrays.asList(getIntersections(p, pm, pe, q, |
366 |
// qm, qe))); |
367 |
|
368 |
return intersections.toArray(new Point[] {}); |
369 |
} |
370 |
|
371 |
// no intersections |
372 |
return new Point[] {}; |
373 |
} |
374 |
|
375 |
/** |
376 |
* Returns the points of intersection between this {@link CubicCurve} and |
377 |
* the given other {@link CubicCurve}. |
378 |
* |
379 |
* @param other |
380 |
* @return the points of intersection |
381 |
*/ |
382 |
public Point[] getIntersections(CubicCurve other) { |
383 |
return getIntersections(this, 0, 1, other, 0, 1); |
84 |
} |
384 |
} |
85 |
|
385 |
|
|
|
386 |
/** |
387 |
* Returns the points of intersection between this {@link CubicCurve} and |
388 |
* the given {@link Line} l. |
389 |
* |
390 |
* @param l |
391 |
* @return the points of intersection |
392 |
*/ |
393 |
public Point[] getIntersections(Line l) { |
394 |
return getIntersections(this, 0, 1, l); |
395 |
} |
396 |
|
397 |
/** |
398 |
* Returns the first control {@link Point}. |
399 |
* |
400 |
* @return the first control {@link Point}. |
401 |
*/ |
86 |
public Point getCtrl1() { |
402 |
public Point getCtrl1() { |
87 |
return new Point(ctrl1X, ctrl1Y); |
403 |
return new Point(ctrl1X, ctrl1Y); |
88 |
} |
404 |
} |
89 |
|
405 |
|
|
|
406 |
/** |
407 |
* Returns the first control {@link Point}'s x-coordinate. |
408 |
* |
409 |
* @return the first control {@link Point}'s x-coordinate. |
410 |
*/ |
90 |
public double getCtrl1X() { |
411 |
public double getCtrl1X() { |
91 |
return ctrl1X; |
412 |
return ctrl1X; |
92 |
} |
413 |
} |
93 |
|
414 |
|
|
|
415 |
/** |
416 |
* Returns the first control {@link Point}'s y-coordinate. |
417 |
* |
418 |
* @return the first control {@link Point}'s y-coordinate. |
419 |
*/ |
94 |
public double getCtrl1Y() { |
420 |
public double getCtrl1Y() { |
95 |
return ctrl1Y; |
421 |
return ctrl1Y; |
96 |
} |
422 |
} |
97 |
|
423 |
|
|
|
424 |
/** |
425 |
* Returns the second control {@link Point}. |
426 |
* |
427 |
* @return the second control {@link Point}. |
428 |
*/ |
98 |
public Point getCtrl2() { |
429 |
public Point getCtrl2() { |
99 |
return new Point(ctrl2X, ctrl2Y); |
430 |
return new Point(ctrl2X, ctrl2Y); |
100 |
} |
431 |
} |
101 |
|
432 |
|
|
|
433 |
/** |
434 |
* Returns the second control {@link Point}'s x-coordinate. |
435 |
* |
436 |
* @return the second control {@link Point}'s x-coordinate. |
437 |
*/ |
102 |
public double getCtrl2X() { |
438 |
public double getCtrl2X() { |
103 |
return ctrl2X; |
439 |
return ctrl2X; |
104 |
} |
440 |
} |
105 |
|
441 |
|
|
|
442 |
/** |
443 |
* Returns the second control {@link Point}'s y-coordinate. |
444 |
* |
445 |
* @return the second control {@link Point}'s y-coordinate. |
446 |
*/ |
106 |
public double getCtrl2Y() { |
447 |
public double getCtrl2Y() { |
107 |
return ctrl2Y; |
448 |
return ctrl2Y; |
108 |
} |
449 |
} |
109 |
|
450 |
|
|
|
451 |
/** |
452 |
* Returns the start {@link Point}. |
453 |
* |
454 |
* @return the start {@link Point}. |
455 |
*/ |
110 |
public Point getP1() { |
456 |
public Point getP1() { |
111 |
return new Point(x1, y1); |
457 |
return new Point(x1, y1); |
112 |
} |
458 |
} |
113 |
|
459 |
|
|
|
460 |
/** |
461 |
* Returns the end {@link Point}. |
462 |
* |
463 |
* @return the end {@link Point}. |
464 |
*/ |
114 |
public Point getP2() { |
465 |
public Point getP2() { |
115 |
return new Point(x1, y1); |
466 |
return new Point(x2, y2); |
116 |
} |
467 |
} |
117 |
|
468 |
|
118 |
/* |
469 |
/** |
119 |
* (non-Javadoc) |
470 |
* @see Geometry#getTransformed(AffineTransform) |
120 |
* |
|
|
121 |
* @see |
122 |
* org.eclipse.gef4.geometry.shapes.Geometry#getTransformed(org.eclipse. |
123 |
* gef4.geometry.transform.AffineTransform) |
124 |
*/ |
471 |
*/ |
125 |
public Geometry getTransformed(AffineTransform t) { |
472 |
public Geometry getTransformed(AffineTransform t) { |
126 |
// TODO Auto-generated method stub |
|
|
127 |
return null; |
473 |
return null; |
128 |
} |
474 |
} |
129 |
|
475 |
|
|
|
476 |
/** |
477 |
* Returns the start {@link Point}'s x-coordinate. |
478 |
* |
479 |
* @return the start {@link Point}'s x-coordinate. |
480 |
*/ |
130 |
public double getX1() { |
481 |
public double getX1() { |
131 |
return x1; |
482 |
return x1; |
132 |
} |
483 |
} |
133 |
|
484 |
|
|
|
485 |
/** |
486 |
* Returns the end {@link Point}'s x-coordinate. |
487 |
* |
488 |
* @return the end {@link Point}'s x-coordinate. |
489 |
*/ |
134 |
public double getX2() { |
490 |
public double getX2() { |
135 |
return x2; |
491 |
return x2; |
136 |
} |
492 |
} |
137 |
|
493 |
|
|
|
494 |
/** |
495 |
* Returns the start {@link Point}'s y-coordinate. |
496 |
* |
497 |
* @return the start {@link Point}'s y-coordinate. |
498 |
*/ |
138 |
public double getY1() { |
499 |
public double getY1() { |
139 |
return y1; |
500 |
return y1; |
140 |
} |
501 |
} |
141 |
|
502 |
|
|
|
503 |
/** |
504 |
* Returns the end {@link Point}'s y-coordinate. |
505 |
* |
506 |
* @return the end {@link Point}'s y-coordinate. |
507 |
*/ |
142 |
public double getY2() { |
508 |
public double getY2() { |
143 |
return y2; |
509 |
return y2; |
144 |
} |
510 |
} |
145 |
|
511 |
|
146 |
/* |
512 |
/** |
147 |
* (non-Javadoc) |
513 |
* @see org.eclipse.gef4.geometry.shapes.Geometry#intersects(Rectangle) |
148 |
* |
|
|
149 |
* @see |
150 |
* org.eclipse.gef4.geometry.shapes.Geometry#intersects(org.eclipse.gef4 |
151 |
* .geometry.shapes.Rectangle) |
152 |
*/ |
514 |
*/ |
153 |
public boolean intersects(Rectangle r) { |
515 |
public boolean intersects(Rectangle r) { |
154 |
// TODO Auto-generated method stub |
|
|
155 |
return false; |
516 |
return false; |
156 |
} |
517 |
} |
157 |
|
518 |
|
|
|
519 |
/** |
520 |
* Tests if this {@link CubicCurve} intersects the given {@link Line} r. |
521 |
* |
522 |
* @param r |
523 |
* @return true if they intersect, false otherwise |
524 |
*/ |
525 |
public boolean intersects(Line r) { |
526 |
return getIntersections(r).length > 0; |
527 |
} |
528 |
|
529 |
/** |
530 |
* Sets the first control {@link Point} to the given {@link Point} ctrl1. |
531 |
* |
532 |
* @param ctrl1 |
533 |
* the new first control {@link Point} |
534 |
*/ |
158 |
public void setCtrl1(Point ctrl1) { |
535 |
public void setCtrl1(Point ctrl1) { |
159 |
this.ctrl1X = ctrl1.x; |
536 |
this.ctrl1X = ctrl1.x; |
160 |
this.ctrl1Y = ctrl1.y; |
537 |
this.ctrl1Y = ctrl1.y; |
161 |
} |
538 |
} |
162 |
|
539 |
|
|
|
540 |
/** |
541 |
* Sets the first control {@link Point}'s x-coordinate to the given |
542 |
* x-coordinate ctrl1x. |
543 |
* |
544 |
* @param ctrl1x |
545 |
* the new first control {@link Point}'s x-coordinate |
546 |
*/ |
163 |
public void setCtrl1X(double ctrl1x) { |
547 |
public void setCtrl1X(double ctrl1x) { |
164 |
ctrl1X = ctrl1x; |
548 |
ctrl1X = ctrl1x; |
165 |
} |
549 |
} |
166 |
|
550 |
|
|
|
551 |
/** |
552 |
* Sets the first control {@link Point}'s y-coordinate to the given |
553 |
* y-coordinate ctrl1y. |
554 |
* |
555 |
* @param ctrl1y |
556 |
* the new first control {@link Point}'s y-coordinate |
557 |
*/ |
167 |
public void setCtrl1Y(double ctrl1y) { |
558 |
public void setCtrl1Y(double ctrl1y) { |
168 |
ctrl1Y = ctrl1y; |
559 |
ctrl1Y = ctrl1y; |
169 |
} |
560 |
} |
170 |
|
561 |
|
|
|
562 |
/** |
563 |
* Sets the second control {@link Point} to the given {@link Point} ctrl2. |
564 |
* |
565 |
* @param ctrl2 |
566 |
* the new second control {@link Point} |
567 |
*/ |
171 |
public void setCtrl2(Point ctrl2) { |
568 |
public void setCtrl2(Point ctrl2) { |
172 |
this.ctrl2X = ctrl2.x; |
569 |
this.ctrl2X = ctrl2.x; |
173 |
this.ctrl2Y = ctrl2.y; |
570 |
this.ctrl2Y = ctrl2.y; |
174 |
} |
571 |
} |
175 |
|
572 |
|
|
|
573 |
/** |
574 |
* Sets the second control {@link Point}'s x-coordinate to the given |
575 |
* x-coordinate ctrl2x. |
576 |
* |
577 |
* @param ctrl2x |
578 |
* the new second control {@link Point}'s x-coordinate |
579 |
*/ |
176 |
public void setCtrl2X(double ctrl2x) { |
580 |
public void setCtrl2X(double ctrl2x) { |
177 |
ctrl2X = ctrl2x; |
581 |
ctrl2X = ctrl2x; |
178 |
} |
582 |
} |
179 |
|
583 |
|
|
|
584 |
/** |
585 |
* Sets the second control {@link Point}'s y-coordinate to the given |
586 |
* y-coordinate ctrl2y. |
587 |
* |
588 |
* @param ctrl2y |
589 |
* the new second control {@link Point}'s y-coordinate |
590 |
*/ |
180 |
public void setCtrl2Y(double ctrl2y) { |
591 |
public void setCtrl2Y(double ctrl2y) { |
181 |
ctrl2Y = ctrl2y; |
592 |
ctrl2Y = ctrl2y; |
182 |
} |
593 |
} |
183 |
|
594 |
|
|
|
595 |
/** |
596 |
* Sets all control points of this {@link CubicCurve} to the given control |
597 |
* {@link Point}s. |
598 |
* |
599 |
* @param p1 |
600 |
* the new start {@link Point} |
601 |
* @param ctrl1 |
602 |
* the new first control {@link Point} |
603 |
* @param ctrl2 |
604 |
* the new second control {@link Point} |
605 |
* @param p2 |
606 |
* the new end {@link Point} |
607 |
*/ |
184 |
public void setCurve(Point p1, Point ctrl1, Point ctrl2, Point p2) { |
608 |
public void setCurve(Point p1, Point ctrl1, Point ctrl2, Point p2) { |
185 |
setP1(p1); |
609 |
setP1(p1); |
186 |
setCtrl1(ctrl1); |
610 |
setCtrl1(ctrl1); |
Lines 188-222
Link Here
|
188 |
setP2(p2); |
612 |
setP2(p2); |
189 |
} |
613 |
} |
190 |
|
614 |
|
|
|
615 |
/** |
616 |
* Sets the start {@link Point} of this {@link CubicCurve} to the given |
617 |
* {@link Point} p1. |
618 |
* |
619 |
* @param p1 |
620 |
* the new start {@link Point} |
621 |
*/ |
191 |
public void setP1(Point p1) { |
622 |
public void setP1(Point p1) { |
192 |
this.x1 = p1.x; |
623 |
this.x1 = p1.x; |
193 |
this.y1 = p1.y; |
624 |
this.y1 = p1.y; |
194 |
} |
625 |
} |
195 |
|
626 |
|
|
|
627 |
/** |
628 |
* Sets the end {@link Point} of this {@link CubicCurve} to the given |
629 |
* {@link Point} p2. |
630 |
* |
631 |
* @param p2 |
632 |
* the new end {@link Point} |
633 |
*/ |
196 |
public void setP2(Point p2) { |
634 |
public void setP2(Point p2) { |
197 |
this.x2 = p2.x; |
635 |
this.x2 = p2.x; |
198 |
this.y2 = p2.y; |
636 |
this.y2 = p2.y; |
199 |
} |
637 |
} |
200 |
|
638 |
|
|
|
639 |
/** |
640 |
* Sets the x-coordinate of the start {@link Point} of this |
641 |
* {@link CubicCurve} to x1. |
642 |
* |
643 |
* @param x1 |
644 |
* the new start {@link Point}'s x-coordinate |
645 |
*/ |
201 |
public void setX1(double x1) { |
646 |
public void setX1(double x1) { |
202 |
this.x1 = x1; |
647 |
this.x1 = x1; |
203 |
} |
648 |
} |
204 |
|
649 |
|
|
|
650 |
/** |
651 |
* Sets the x-coordinate of the end {@link Point} of this {@link CubicCurve} |
652 |
* to x2. |
653 |
* |
654 |
* @param x2 |
655 |
* the new end {@link Point}'s x-coordinate |
656 |
*/ |
205 |
public void setX2(double x2) { |
657 |
public void setX2(double x2) { |
206 |
this.x2 = x2; |
658 |
this.x2 = x2; |
207 |
} |
659 |
} |
208 |
|
660 |
|
|
|
661 |
/** |
662 |
* Sets the y-coordinate of the start {@link Point} of this |
663 |
* {@link CubicCurve} to y1. |
664 |
* |
665 |
* @param y1 |
666 |
* the new start {@link Point}'s y-coordinate |
667 |
*/ |
209 |
public void setY1(double y1) { |
668 |
public void setY1(double y1) { |
210 |
this.y1 = y1; |
669 |
this.y1 = y1; |
211 |
} |
670 |
} |
212 |
|
671 |
|
|
|
672 |
/** |
673 |
* Sets the y-coordinate of the end {@link Point} of this {@link CubicCurve} |
674 |
* to y2. |
675 |
* |
676 |
* @param y2 |
677 |
* the new end {@link Point}'s y-coordinate |
678 |
*/ |
213 |
public void setY2(double y2) { |
679 |
public void setY2(double y2) { |
214 |
this.y2 = y2; |
680 |
this.y2 = y2; |
215 |
} |
681 |
} |
216 |
|
682 |
|
217 |
/* |
683 |
/** |
218 |
* (non-Javadoc) |
|
|
219 |
* |
220 |
* @see org.eclipse.gef4.geometry.shapes.Geometry#toPath() |
684 |
* @see org.eclipse.gef4.geometry.shapes.Geometry#toPath() |
221 |
*/ |
685 |
*/ |
222 |
public Path toPath() { |
686 |
public Path toPath() { |
Lines 226-229
Link Here
|
226 |
return p; |
690 |
return p; |
227 |
} |
691 |
} |
228 |
|
692 |
|
|
|
693 |
/** |
694 |
* Get a single {@link Point} on this CubicCurve at parameter t. |
695 |
* |
696 |
* @param t |
697 |
* in range [0,1] |
698 |
* @return the {@link Point} at parameter t |
699 |
*/ |
700 |
public Point get(double t) { |
701 |
if (!(PrecisionUtils.greaterEqual(t, 0) && PrecisionUtils.smallerEqual( |
702 |
t, 1))) { |
703 |
throw new IllegalArgumentException( |
704 |
"Paramter t is out of range! t = " + t + " !in_range(0,1)"); |
705 |
} |
706 |
|
707 |
// compensate rounding effects |
708 |
if (t < 0) { |
709 |
t = 0; |
710 |
} else if (t > 1) { |
711 |
t = 1; |
712 |
} |
713 |
|
714 |
double d = 1 - t; |
715 |
return getP1().getScaled(d * d * d) |
716 |
.getTranslated(getCtrl1().getScaled(3 * t * d * d)) |
717 |
.getTranslated(getCtrl2().getScaled(3 * t * t * d)) |
718 |
.getTranslated(getP2().getScaled(t * t * t)); |
719 |
} |
720 |
|
229 |
} |
721 |
} |