Reputation: 115
I am developing a graphical editor application. It has a Bezier tool. I want to implement it similar to the bezier control in Paint.NET application, where two handles of the curve (in addition to the end points) are placed on the curve itself, by which the user can control the curvature. Placement of these handles on the curve, gives a better sense and feel to the graphist, as is shown in this figure:
But my problem is that DrawBezier method in .NET, gets two control points that are not guaranteed to be always placed on the curve. Do you know how can I use the coordinates of these two on-curve handles to draw a Bezier?
Upvotes: 1
Views: 1787
Reputation: 53598
We can use standard polynomial curve fitting to find a single cubic Bezier curve through any set of four points, but you're always going to be left with a free parameter problem: two of the points are going to be the start and end of the curve, so we know their time values are 0 and 1, but the two points are entirely free in terms of time value, and so you're going to have to come up with a reasonable assumption on what they should be before you can evaluate your curve fit.
See https://pomax.github.io/bezierinfo/#curvefitting for the maths if you want to implement this yourself, or find a library that does polynomial fitting (which means any half-decent statistics package), and then for timing values you have a few options:
Of these, obviously 2 is going to give "reasonable" results with the least amount of effort, but if you're writing a graphics application, "reasonable" depends on what your users need, not what is easy for you.
Upvotes: 1
Reputation: 115
The DrawCurve method of the Graphics class does the job. That is, instead of using Bezier curves, you should use Canonical Spline.
I found it in Charles Petzold's book (Programming Windows with C#):
"The Canonical Spline - The Graphic class includes a second type of spline called the canonical spline, meaning a standard or normal spline..."
Upvotes: 0