Started by Oct 27 2012 01:45 PM

,
13 replies to this topic

Posted 27 October 2012 - 01:45 PM

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?

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?

Posted 27 October 2012 - 02:28 PM

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.

Posted 27 October 2012 - 02:59 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.

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.

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' or 'SotL' 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] **

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 |

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.

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.

Posted 28 October 2012 - 06:04 AM

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.

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

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

Posted 28 October 2012 - 11:21 AM

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:

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

Posted 31 October 2012 - 08:42 AM

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

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

Posted 31 October 2012 - 11:35 AM

about a month ago I made this based on those papers:

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?

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?

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