Hacker News new | ask | show | jobs
by andrewnc 1597 days ago
So, let's say I want to implement these (or other) curves into a drawing program. How do I go about doing that? I mean to say, I want to implement a brush on canvas type interface - how do Bézier curves fit into that?

Do I collect points when mouse is down and build a curve with those points? Do I get some other path information and rasterize?

I'm trying to write my own drawing program (for fun) but given my lack of background in this I feel myself floundering.

2 comments

You can use the very close relative of the Bezier, the uniform B-spline, for drawing curves, it’s a more natural fit. Yes, this way you can collect a sequence of mouse events and use them as-is as the control points of a curve. Depending on how you render your curve, in 2d, you might want a “normal” for the curve in order to rasterize a thick and/or textured stroke. You can do it easily and unambiguously by rotating the tangent (derivative) of the curve by 90 degrees. In 3d, it’s harder.

The B-spline is just a math name for a connected sequence of curve segments, setup so that the segments join nicely and so that there is a continuous “parameter” along the entire curve.

For a sketch interface, I personally prefer the quadratic B-spline to the cubic (but you can use any degree you want). The background for curves in general isn’t easy to pick up and understand quickly, but you might check out the “Chaikin” subdivision which is super simple math and equivalent to a quadratic curve.

I'd look at other programs for inspiration. Blender for example allows you to place the anchor point and then gives you the option for how many segments you want connecting the anchors. Turning the resolution up and down gives you a feel for how many subdivisions you'll need for a smooth curve.

At the very least you'll need a UI for placing the anchors and handles, but how the UI works is really subjective. Your next step is to take those anchors and desired resolution and generate a list of vertices. After that it's just an exercise in rasterization.