Seeking variable width line drawing algorithms

Started by
1 comment, last by C0lumbo 11 years, 1 month ago

I have a 2D array of values that map to coordinates, and a set of lines that pass through some of those coordinates. I want to be able to change the values in the array based on their proximity to these lines. So basically I need a line-drawing algorithm, but which supports arbitrary widths.

An algorithm like the Bresenham line-drawing algorithm would be fine if the line had essentially zero thickness, but the thickness varies from line to line and I'm not sure how I'd extend it to handle that.

I'm also aware that I could treat the line as a rectangle and fill that in, but it sounds suspiciously like implementing half of a software renderer which I'm not keen on doing, unless there is some very simple code or pseudocode that I can pretty much just drop in.

I currently have an interesting inverted solution, where I take each point in the array and calculate the distance from that point to the nearest line, and if that distance is less than the line's thickness, then the point is filled in. This has the beneficial effect of letting me taper off the values further from the line, giving me a nice gradient at the edges and avoiding aliasing as well. It also has the detrimental effect of being very slow, even with all the square roots removed from the distance calculations. There are too many redundant distance calculations against lines which turn out not to be the nearest one and thus not contributing to the final distance. But if I could speed this up I'd be happy to stick with it.

Any suggestions?

Advertisement
Some sort of spatial data structure for the lines would speed up the search. A grid would probably do fine. And I hestitate to recommend it, but a BSP tree would be an elegant solution.

Slightly hacky suggestion, but should be simple to implement and may be good enough depending on your precision/speed requirements:

You could use something like Bresenham but 'render' a square or circle at each point along the line instead of just a single pixel. For very thick lines you would be doing a lot of overdraw, so you could modify it to only stamp a square/circle once every n steps (where n depends on thickness). If you want to maintain your nice smooth gradients you could conceptually make your sphere more transparent at the edges, and blend with a max function.

This topic is closed to new replies.

Advertisement