-
I've been looking for ages for some code to convert Cubic Bezier to Quadratic. I've only found some code on OpenSWF but is incomplete in some point (most methods in the CDoublePoint class like WhichSide or Intersection) are undocumented and not obvious. Another implementation is in the Ming library but doesn't work in most cases. I've found some explanation in cryptic mathematical language in some other site, but apparently nobody else care about this problem. My question: is it possible that nobody can point me out to some working code? (Language doesn't really matter).
Thanx in advance,
Lev
-
Here you go
if: Bezier 1 = (x1, y1), (x2,y2), (x3, y3)
then: Bezier 2 = (x1, y1), (x1 + 2/3 (x2 - x1) ,y1 + 2/3 (y2 - y1)), (x2 + 1/3 (x3 - x2) , y2 + 1/3 (y3 - y2))(x3,y3)
[http://www.icce.rug.nl/erikjan/bluefuzz/beziers/beziers/node2.html]
other refs:
http://www.research.microsoft.com/~h...uadspline.html
http://www.cubic.org/~submissive/sourcerer/bezier.htm
--Jesse
-
Hi Leviathan,
cubic beziers (type1 outlines) can only be converted to quadratic beziers (truetype or flash outlines) by a piecewise approximation.
I did this in a project I just referred to in http://www.flashkit.com/showthread.hp?threadid=104182
(and it was careful to avoid a specific rendering error that happened when using flash with type1 fonts)
If you are interested, I could send you a couple of lines of mostly uncommented C source.
Musicman
-
So...are you suggesting that the equations behind the proof at the posted location are incorrect? (the link to the equations and proof is straight from Macromedia)
--Jesse
-
Hi Jaezell,
I will have a look at that macromedia source, but I cant do it a least during this week. I will come back on that later
Musicman
-
Cool. I'll be waiting for your answer. If you can show that the proof on the site doesn't work that would be enough to convince me. :-)
--Jesse
-
Hi Jaezell,
I forgot to mention that this formula is correct. It is transforming any
quadratic curve into an equivalent cubic one, which is an exact
transformation. The question here was the other way round, i.e.
converting an arbitrary cubic into a quadratic curve
Musicman
-
Originally posted by Musicman
Hi Leviathan,
cubic beziers (type1 outlines) can only be converted to quadratic beziers (truetype or flash outlines) by a piecewise approximation.
I did this in a project I just referred to in http://www.flashkit.com/showthread.hp?threadid=104182
(and it was careful to avoid a specific rendering error that happened when using flash with type1 fonts)
If you are interested, I could send you a couple of lines of mostly uncommented C source.
Musicman
Yes, please, send the code to emanuele@hillsdon.com
Thanx,
Lev
-
Can this code be posted in this forum? I'd be interested
in seeing this as well.
-
lol
lol. next time I should read more carefully :-).
--Jesse
-
this is not a proof, just trying to give some evidence...
To introduce some mathematical terms: a cubic curve is a function
(x,y)=(fx(t),fy(t)) of some parameter t of the form
(x,y)=(x3,y3)*t**3+(x2,y2)*t**2+(x1,y1)*t+(x0,y0)
with coefficients x3,y3....x0,y0
It is fairly obvious that it is invariant under linear transformations of both the x,y coordinate system as well as the parameter t - in plain words this means that any such transformation results in a cubic curve with different
coefficients.
There is another way of specifying a cubic curve - defining end points and control points. The control points are defined such that the line from start point to first control point coincides with the tangent in the start point and the line from first to second control point also is a tangent to the curve. Since you can always do a linear transform, there is no loss of generality by assigning the start point to t=0 and the end point to t=1. This finally leaves a set of equations which can be solved for the (x0,y0) ... coefficients.
Similar considerations apply to quadratic curves; there is only one control point that determines tangents in both end points. One can actually consider a quadratic curve as a special case of a cubic, with zero coefficients in the cubic term.
I am going to utilize the invariance to linear transformations of the coordinate systems: just pick two arbitrary points as endpoints, and any arbitrary curve can be transfermed into one that starts and ends at these selected points. In the first image there are a few curves, all with the same end points. The (+) crosses indicate control points of quadratic curves, the (x) marks the control points of equivalent cubic curves as determined by the formula posted here previously.
It is obvious from the diagram that the line between two control points is parallel to a line between the end points.
Now, what about true cubic curves? In the diagram, I start with a quadratic curve (black) and I also show there a few others that obviously cannot be represented by a quadratic. How could these be converted to quadratics?
Both quadratics and cubics have the interesting property that they can easily be rendered by a recursive subdivision approach: one can split a curve into pieces which are of the same type and connect smoothly (i.e. have a common tangent). Processing will normally go on until a segment collapses into a point or a short line. Do the same here but stop early (to not generate too many curve points) based on some level of tolerance between a cubic and a quadratic through the same set of points.
I have written this as part of a flash page generator (sort of a printer driver) before. Since the code is a bit too large to be posted here, I have uploaded it to http://www.fontimages.f2s.com/private/genfont_details.c
The original program was reading (one or more) page files in a proprietary encoded format and was also using type1 fonts in a proprietary manner. It also includes a partial type1 font decoder, and it was specifically designed to work in a CGI context as well.
To use this code in a different environment, one would have to a) rewrite the font access code in a suitable way or otherwise utilize a platform-specific way to access the font outlines and b) write an input reader that calls a showchar() function for every character.
Have fun
Musicman
-
Wow!
This is better than your average thread at FlashKit!
Hi there Lev, I mean Charles, I mean Emanuele... (I'm confused)
Cheers, David.
-
Ming now contains the functionality for this. You can download the source from opaque.net if you want to check out its approximation method.
--Jesse
-
No it doesn't work in all cases, for example, when the line segment passing between the two control points is parallel to the one passing between the two ancro points.
Lev
Originally posted by JAEzell
Ming now contains the functionality for this. You can download the source from opaque.net if you want to check out its approximation method.
--Jesse
-
Yes. The Ming site mentions that it isn't 100% working yet, but that it works "for most practical cases." Regardless, it is a start for anyone that is looking for some example code.
--Jesse
-
cubic to quadratic?
This thread was from 2000. Was any progress every made? Is there reliable code somewhere that converts cubics to quadratics?
Thanks
Bobito
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|