**0**

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

###
#1
GDNet+ - Reputation: **1978**

Posted 27 October 2012 - 01:45 PM

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?

###
#2
Moderators - Reputation: **10097**

Posted 27 October 2012 - 02:28 PM

###
#3
GDNet+ - Reputation: **1978**

Posted 27 October 2012 - 02:59 PM

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.

###
#4
Crossbones+ - Reputation: **26105**

Posted 27 October 2012 - 03:16 PM

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?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.

It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.

All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.

Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal | **[Fly with me on Twitter] **

###
#5
GDNet+ - Reputation: **1978**

Posted 27 October 2012 - 03:22 PM

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?

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.

###
#6
Moderators - Reputation: **10097**

Posted 27 October 2012 - 04:08 PM

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.

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.

###
#8
Crossbones+ - Reputation: **2010**

Posted 28 October 2012 - 06:04 AM

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

###
#9
Members - Reputation: **977**

Posted 28 October 2012 - 09:57 AM

All midpoint algorithms describe the curves with an equation in the form f(x, y) = 0 (with integer or rational* coefficients).

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

*f*is any

*polynomial*function of

*x*and

*y*(with rational coefficients)? Then is the idea that the state of the algorithm is all the

*x*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?

^{i}y^{j}###
#10
Crossbones+ - Reputation: **2010**

Posted 28 October 2012 - 11:21 AM

- 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

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.

###
#12
Members - Reputation: **278**

Posted 31 October 2012 - 08:42 AM

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: http://http.develope...gems3_ch25.html

###
#13
Members - Reputation: **1106**

Posted 31 October 2012 - 11:35 AM

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?

###
#14
Members - Reputation: **504**

Posted 31 October 2012 - 04:33 PM

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).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: http://http.develope...gems3_ch25.html

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):

http://ivanleben.blo...r-graphics.html

Otherwise, there is an algorithm similar to bresenham but for bezier curves, which I guess is more what the OP was looking for:

http://smartech.gate...art1?sequence=1

**Edited by Zlodo, 31 October 2012 - 04:38 PM.**