Assuming I’m using some graphic API which allows me to draw
bezier curves by specifying the 4 necessary points:
start, end, two control points.
Can I reuse this function to draw x percent of the ‘original’ curve
(by adjusting the control points and the end point)?
Or is it impossible?
Unnecessary information, should someone care:
- I need the whole thing to draw every n % of the original
bezier curve
with different color and/or line style -
I’m using Java’s Path2D to draw bezier curves:
Path2D p = new GeneralPath(); p.moveTo(x1, y1); p.curveTo(bx1, by1, bx2, by2, x2, y2); g2.draw(p);
What you need is the De Casteljau algorithm. This will allow you to split your curve into whatever segments you’d like.
However, since you’re dealing with just cubic curves, I’d like to suggest a slightly easier to use formulation that’ll give you a segment from
t0tot1where0 <= t0 <= t1 <= 1. Here’s some pseudocode:Then just draw the Bézier curve formed by
(xa,ya),(xb,yb),(xc,yc)and(xd,yd).Note that
t0andt1are not exactly percentages of the curve distance but rather the curves parameter space. If you absolutely must have distance then things are much more difficult. Try this out and see if it does what you need.Edit: It’s worth noting that these equations simplify quite a bit if either
t0ort1is 0 or 1 (i.e. you only want to trim from one side).Also, the relationship
0 <= t0 <= t1 <= 1isn’t a strict requirement. For examplet0 = 1andt1 = 0can be used to “flip” the curve backwards, ort0 = 0andt1 = 1.5could be used to extend the curve past the original end. However, the curve might look different than you expect if you try to extend it past the[0,1]range.Edit2: More than 3 years after my original answer, MvG pointed out an error in my equations. I forgot the last step (an extra linear interpolation to get the final control points). The equations above have been corrected.