maltaisn / svg-equations

Utility to convert SVG paths to mathematical equations
Apache License 2.0
89 stars 10 forks source link

Quadratic Bézier Curve #1

Open graphemecluster opened 4 years ago

graphemecluster commented 4 years ago

"Cartesian equation type only supports straight lines. Cartesian equations for bezier curves are theoretically possible but not simple, and performance when plotted would be bad."

After simplifying a quadratic curve is actually just a simple parabola:

Implicit Function of a quadratic Bézier curve with points (A, D), (B, E) and (C, F)  (x(D-2E+F)-y(A-2B+C))² +2x((A+C)(DF-E²)-A(E-F)²+2B(DE-2DF+EF)-C(E-D)²) +2y((D+F)(AC-B²)-D(B-C)²+2E(AB-2AC+BC)-F(B-A)²) +4(BD-AE)(BF-CE) +(AF-CD)² =0

And the 5 constants could be pre-calculated if necessary.

Also, it would be great if something like this could be made. Desmos Link

Thank you for reading and sorry for disturbing.

maltaisn commented 4 years ago

After simplifying a quadratic curve is actually just a simple parabola:

Implicit Function of a quadratic Bézier curve with points (A, D), (B, E) and (C, F)  (x(D-2E+F)-y(A-2B+C))² +2x((A+C)(DF-E²)-A(E-F)²+2B(DE-2DF+EF)-C(E-D)²) +2y((D+F)(AC-B²)-D(B-C)²+2E(AB-2AC+BC)-F(B-A)²) +4(BD-AE)(BF-CE) +(AF-CD)² =0

The implicit equation I came up for quadratic bezier curves with was (it's the same as the one mentioned in the readme):

It seems a bit different than yours. I won't show the one for cubic bezier curves here, it has over a hundred terms. So I consider that the "direct" approach consisting of calculating each term is not very viable, unless maybe converting all cubic curves to quadratic curves beforehand.

I had also tried another method which used a number of points from the bezier curve (6 for quadratic, 10 for cubic) to make a curve fitting, essentially reducing the problem to solving a system of linear equations. It worked great, however I was using arbitrary precision numbers and I'm not sure how well it would work with floating point numbers. I have some Maple worksheets on that if you're interested.

There are other problems with implicit equations. For example finding the bounds becomes a lot more complicated. For parametric equations it's as easy as limiting the range of . For implicit equations you get complex bounds like (this one was made up but you get the point). I didn't look much into it though.

Performance also becomes a problem. Thousands of parametric equations can be plotted just fine since it's a matter of entering various values for . Implicit equations on the other hand are a lot harder to plot. You can already see that when comparing the performance in Desmos for plotting the generated cartesian equations vs the parametric ones for a relatively complex SVG made of only straight lines. (like a world map)

So yes, you are correct in saying this is possible. However I believe it goes beyond the intent of this project. The complexity of resulting equations makes it unappealing.

Also, it would be great if something like this could be made. Desmos Link

Adding fill would certainly be an interesting challenge. I don't have much time right now unfortunately. I encourage you to make your own research!

graphemecluster commented 4 years ago

Thank you for the reply!

I believe that the equation I gave would be the same as yours after expanding unless I made any mistake. Actually I am interested in the implicit equation of cubic Bézier curve, and I would be glad if you could provide more information on it.

Also, if performance is your concern I suggest you to add an option to plot straight lines by explicit functions.

Luckily Desmos support filling of parametric functions, so it would be easy to reproduce the SVG path (additionally, by setting the color by JavaScript).

maltaisn commented 4 years ago

Here's PDF renders of the Maple worksheets I had made:

The worksheets in case you have Maple: bezier-maple.zip