# Is it possible to draw Bezier curves without using line segments?

## Recommended Posts

MarkS    3502
When I started programming at age eight on a 68000-based Mac, I was fascinated by QuickDraw's ability to draw geometric shapes so quickly. This led to years of searching as to how (I started well before the internet was established). I now have an admittedly pointless, but interesting cache of pixel-perfect (midpoint) geometric object drawing code (ellipses, elliptical arcs, lines, etc.). This kind of became a hobby of mine.

One thing that I have noticed is that I have never seen a similar method for drawing Bezier curves. I have seen countless algorithms for drawing them, but they all rely on calculating the end points for line segments and then drawing the line segments. Searching for "midpoint Bezier algorithm" returns vast amounts of results for Bezier subdivision, but nothing about drawing curves pixel by pixel.

I am just curious if anyone here has ever seen or heard of such an algorithm. Sadly, the math is a little beyond my abilities, so rolling my own is out. It just seems like this should be possible. If this cannot be done, what is the reason?

##### Share on other sites
JTippetts    12949
You can solve a Bezier curve at any location. Drawing it as a series of line segments provides an approximation of the curve, with the tradeoff being between drawing time and resolution. If you reduce your step between endpoints to a small enough increment you end up with pixel-sized segments in between endpoints, at the cost of having to draw more tiny little line segments, but at this level it becomes as close to "pixel perfect" as you can get. So there is nothing difficult or weird with what you want, you just have to use a small enough increment.

##### Share on other sites
MarkS    3502
I understand that I can subdivide the curve down so that I am drawing single pixel lines, but that is again drawing lines. What I am looking for is Bezier curve code that draws a Bezier similarly to Bresenham's line drawing code. In other words, the initial pixel is calculated and the rest are incrementally derived from some function. The curve would be plotted pixel by pixel by pixel.

My interest in this is really just personal. I have no need for it, but it always bothered me that I could not find any examples. I am just really curious if this has ever been done, and if so, I would like to see the code.

##### Share on other sites
[quote name='MarkS' timestamp='1351367116' post='4994488']
When I started programming at age eight on a 68000-based Mac, I was fascinated by QuickDraw's ability to draw geometric shapes so quickly. This led to years of searching as to how (I started well before the internet was established). I now have an admittedly pointless, but interesting cache of pixel-perfect (midpoint) geometric object drawing code (ellipses, elliptical arcs, lines, etc.). This kind of became a hobby of mine.[/quote]
That'd be an interesting bunch of code, regardless of the language they were written in. Do you them up on a website somewhere where I could peruse them?

##### Share on other sites
MarkS    3502
[quote name='Servant of the Lord' timestamp='1351372577' post='4994523']
That'd be an interesting bunch of code, regardless of the language they were written in. Do you them up on a website somewhere where I could peruse them?
[/quote]

I have them floating around my hard drive somewhere. They all came from the internet, either in whole or part and most are easy to find. The only difficult one was the elliptical arc code. It took years for me to find a reference on how to do that and modify my ellipse drawing function. If I have time, I'll clean them up and post them.

##### Share on other sites
JTippetts    12949
[quote name='MarkS' timestamp='1351371589' post='4994517']
I understand that I can subdivide the curve down so that I am drawing single pixel lines, but that is again drawing lines. What I am looking for is Bezier curve code that draws a Bezier similarly to Bresenham's line drawing code. In other words, the initial pixel is calculated and the rest are incrementally derived from some function. The curve would be plotted pixel by pixel by pixel.

My interest in this is really just personal. I have no need for it, but it always bothered me that I could not find any examples. I am just really curious if this has ever been done, and if so, I would like to see the code.
[/quote]

Well, if your line segments are only a single pixel long they're... uh.... just pixels. They're sequentially plotted from the function that is the Bezier curve. You're probably not going to find something as simple and elegant as a Bresenham algorithm for Bezier curves because the curvature is neither constant like a circle, nor easily predictable like an ellipse. You could possibly derive it to obtain the curvature (the curvature can be described as the rate of change of the tangent to the curve with respect to the arc-length, or dT/ds) and match curvature to a pixel pattern, but that just seems needlessly complex. Some quick googling turned up http://www.gamedev.net/topic/156180-curvature-of-a-bezier-solved/ in which a poster posted about how to find the curvature of a Bezier; couldn't say how accurate or correct it is, but it might be a place to start. Seems like, if you know the curvature at pixel N, then you should be able to use it to calculate pixel N+1. But, again, it's probably just easier to evaluate the curve for very small increments of t instead.

##### Share on other sites
MarkS    3502
That makes sense. Thanks.

##### Share on other sites
apatriarca    2365
All midpoint algorithms describe the curves with an equation in the form f(x, y) = 0 (with integer or rational* coefficients). A Bézier curve is not in this form. It is a parametric equation in the form (x, y) = P(t). However, it is always possible to transform a Bézier curve to the required form. In the case of a quadratic (or maybe also cubic) curve it may be practical. In general it is probably easier and faster to simply use the subdivision idea. Note that a quadratic Bézier curve is an arc of parabola, so you need an algorithm to draw it if you want to draw a quadratic Bézier curve.

* A polynomial with rational coefficients can be easily converted to a polynomial with integer coefficients by multiplication by an integer constant.

##### Share on other sites
Emergent    982
[quote name='apatriarca' timestamp='1351425855' post='4994675']
All midpoint algorithms describe the curves with an equation in the form f(x, y) = 0 (with integer or rational* coefficients).
[/quote]

I haven't seen this; I always just see lines, circles, and ellipses -- and I guess conic sections most generally. I assume you mean that [i]f[/i] is any [i]polynomial [/i]function of [i]x[/i] and [i]y[/i] (with rational coefficients)? Then is the idea that the state of the algorithm is all the [i]x[sup]i[/sup]y[sup]j[/sup][/i] products or something? I don't see how this works beyond the quadratic case, because the coefficients in Pascal's triangle stop being 1s and 2s (so you lose the fast bit-shift multiplication, and besides, the number of items in each row of Pascal's Triangle grows with O(n), whereas you can just compute the powers directly in O(log(n)) by repeated squaring). Maybe you could point me to a reference?

##### Share on other sites
apatriarca    2365
I do not know special algorithms outside lines, circles, ellipses and conic sections. However, I think any "midpoint algorithm" to rasterize a curve defined by the implicit equation f(x,y) = 0 follows the following steps:[list=1]
[*]Choose an initial point
[*]Choose the two candidate next points (from the points adjacent to the current one) based on the derivative of the curve at the current point
[*]Evaluates the function f at the midpoint of the two candidate points
[*]Choose the next point based on the sign of the value computed at 3
[*]Repeat steps 2-5 until the last point is rendered
[/list]
This general algorithm comes from my general understanding of the algorithms and not from some reference. However, looking for relevant papers on Google I have found "Rasterization of Nonparametric Curves" by John D. Hobby. I haven't read it but the abstract is the following:

[quote]We examine a class of algorithms for rasterizing algebraic curves based on an implicit form that can be evaluated cheaply in integer arithmetic using finite differences. These algorithms run fast and produce “optimal” digital output, where previously known algorithms have had serious limitations. We extend previous work on conic sections to the cubic and higher order curves, and we solve an important undersampling problem.[/quote]

##### Share on other sites
Krohm    5030
[url="http://research.microsoft.com/en-us/um/people/cloop/loopblinn05.pdf"]Let there be GPUs[/url].

##### Share on other sites
skytiger    294
Windows 7 performs font rasterization using the GPU directly
This does exactly what you want - which is drawing triangles and shading each pixel according to an implicit equation that derives from a Bezier curve
No slow and messy subdividing and drawing lines etc.
You can get a detailed description of how they do it in "Rendering Vector Art on the GPU" by Charles Loop and Jim Blinn
This is free online: [url="http://http.developer.nvidia.com/GPUGems3/gpugems3_ch25.html"]http://http.develope...gems3_ch25.html[/url]

##### Share on other sites
bwhiting    1562

http://bwhiting.co.uk/b3d/vector/

no AA though sadly as ddx and ddy unavailable in the shader (shader model 2.0), anyone know another way?

##### Share on other sites
Zlodo    654
[quote name='skytiger' timestamp='1351694531' post='4995834']
Windows 7 performs font rasterization using the GPU directly
This does exactly what you want - which is drawing triangles and shading each pixel according to an implicit equation that derives from a Bezier curve
No slow and messy subdividing and drawing lines etc.

You can get a detailed description of how they do it in "Rendering Vector Art on the GPU" by Charles Loop and Jim Blinn
This is free online: [url="http://http.developer.nvidia.com/GPUGems3/gpugems3_ch25.html"]http://http.develope...gems3_ch25.html[/url]
[/quote]
That remains quite complex though. For one, for cubic beziers it's much much easier to recursively split them into quadratic beziers than analyzing them to find out whether they're looping or whatnot to be able to rasterize them like in the paper. Quadratics are so much easier to work with, and approximating a cubic bezier with one or more quadratic is rather easy and doesn't really even result in any noticeable curvature error even when zooming in (while keeping the advantage of pixel perfect curve rendering at any zoom level).
Another thing is that you need to find overlapping "quadratic triangles" to subdivide one of them. Plus of course you need to perform tesselation of the whole thing too, but that's not really specific to using the GPU to rasterize curves.

A much easier to implement approach is to just use the stencil buffer instead of doing tesselation altogether. This also avoid the need to subdivide the "quadratic curve triangles". The downside is that each shape you need to render pretty much needs exclusive access to the stencil buffer, followed by rendering a bounding rectangle to fill the actual inside of the shape using the stencil so you pretty much require two render call for for each independent shape you want to render (and if you do stroking that's a second shape).

And then there's that, although I suspect it's quite heavy on fillrate (plus things like gradients would seem like it would be rather complex to do):
[url="http://ivanleben.blogspot.fr/2010/12/animated-random-access-vector-graphics.html"]http://ivanleben.blo...r-graphics.html[/url]

Otherwise, there is an algorithm similar to bresenham but for bezier curves, which I guess is more what the OP was looking for:
[url="http://smartech.gatech.edu/bitstream/handle/1853/3671/92-16.pdf;jsessionid=A796F2CEFBCE7A61CCC5179357E1970E.smart1?sequence=1"]http://smartech.gate...art1?sequence=1[/url] Edited by Zlodo