# Whats the best way to render (2D) parametric curves with uniform stroke width?

I want to create a rendering engine that mostly renders parametric curves on a 2d screen. As far as it seems, todays graphics are all dependent on vertices and matrices, so not sure where to start or merge things.

Lets say I want to draw a ellipse(starting simple), So my object is defined as (a cos(t), b sin(t)). Where t ranges from 0 to 2pi. Now whats the best approach to put this information to screen, (or say a png for example). I also want a uniform smooth path, of which i can control the stroke width, and thats being the hard piece of the puzzle.

Is there exists a rendering method that does this? Where objects are defined and rendered mathematicaly ? And will it be possible to extend that to 3D as well?

Also, I want my object to be defined the way it is; and not want to approximate points and then render them the opengl way. I want the process to feel more procedural.

I would also really appreciate any advice or resources on this topic, its being really hard to even start.

Your question implies that everything has to be rendered with a vertex representing each small piece of a curve. This simply isn't accurate. It also implies that device rendering space is in some way continuous.

In computer graphics all the devices we draw to are discreet. There is no continuous space so any rendering is going to be an approximation. Even the underlying math is discreet, in floating point math most computations will still be approximations of the actual answer since most results can not be fully represented.

Having set an appropriate expectation we can still get pretty good approximations of most curves.

For example, a curve can be rendered inside a shape that is defined by vertex attributes which bound the curve. Once the curve is bounded each pixel inside the bounding area is tested for it's distance to the curve and its color set appropriately. The vertex data simply limits the region that is tested. When used properly this approach can greatly enhance the performance of a drawing algorithm.

Throw away the strictly binary notion of "on" or "off" the curve. The curve itself is infinitesimally small and we can never hope to hit it exactly no matter the zoom. Instead it is a question of "how far away" is the center of this pixel from the curve. The center of a pixel can be thought of as being infinitesimally small so it has the potential of being exactly on the curve. But when the pixel is set, it takes up real space which will can't come close to representing a continuous curve. Instead combine "at what distance" do we want to start showing the curve with "how far away" is the center of the pixel from the curve to get an approximation for the color of the pixel.

These two ideas allow curves to be render very thick, or very thin. It also allows the color of the pixels to be blended with the background color so that the pixels themselves have some representation of distance. Get to far away and the pixel is the background color, get close enough and the pixel is the foreground color. Everything in between not only helps with aliasing, but also helps us visualize the distances involved.

This breaks the problem of rendering the curve down into two pieces.

1. Calculate the bounding region for the curve and generate appropriate vertex data. Typically done either on the CPU or in a compute shader.

2. Draw the curve by computing the distance from the center of each pixel to the curve. Typically done with a vertex/fragment shader combo.

• Well you could have a vector display (laser show for example) or cnc machine that you drive that would be continuous of sorts. But yes your not wrong generally though. Apr 9, 2023 at 18:59

Here is a method that is "best" is some sense.

Scan the image, and for every pixel at $$(x,y)$$ compute the quantity

$$t=\min\left(1,\theta-\frac12\left|\sqrt{(x-x_c)^2+(y-y_c)^2}-r\right|\right)=\min\left(1,|\theta-2d|\right)$$

where $$(x_c,y_c)$$ is the center of the circle, $$r$$ the radius, and $$\theta$$ the desired thickness. ($$d$$ is the distance of the pixel to the circle.)

Now if $$t\ge 0$$ assign this pixel a color that is the linear interpolation between the foreground and the background color, with weights $$t$$ and $$1-t$$.

This method is not efficient as is scans the whole image. You can fix that by using the parametric equation with a suitable step, and for every distinct pixel, apply the above computation in a neighborhood large enough to cover the stroke width.

For other shapes, the procedure is the same, replacing $$d$$ by the distance to the given curve.

• Your 1st alg only should only work on circles, as the d , or distance from curve wont be as simple as a -r , especially when the equation is complex...2nd method doesn't mention the issue o calculating same pixel twice, tho we can use the "more foreground" color, but still no way to calculate distance from curve. (editing the question example from circle to ellipse)
– user19809
Apr 7, 2023 at 0:22
• @NKabir: if you knew all this, don't ask. For the distance, use a minimization algorithm. For the duplicate pixels, use an incremental approach.
– user1703
Apr 7, 2023 at 6:42