4
$\begingroup$

Let's say we have a line going through the centers of two cells on a 2D grid, and we define a "pixel-perfect rasterized line" as the minimal set of 8-connected cells that best approximate the original line.

Finding such a set of cells is usually done via the Bresenham's algorithm, but in some cases [1, 2, 3] it can be useful to have some closed-form formula e.g. $$\text{distance(point, line)} \,<\, \text{thickness}/2,$$that tells whether a given cell's center belongs to such "rasterized line" or not.

For this purpose, $\text{thickness} = 1$ doesn't always give correct results, but empirically I found that $$\text{thickness} = \max(|N_x|, |N_y|)$$ (where $N$ is the normalized direction of the line) seems to always work.

Illustration of the correct (green) and incorrect (red) results:

Unfortunately, I don't have enough mathematical background to understand why this ends up being the correct answer. How would one actually derive it?

$\endgroup$

1 Answer 1

0
$\begingroup$

I think I figured it out. Turned out to be much simpler than I assumed :-)

To get the desired result, we just have to make sure that no two adjacent cells along the slower-varying coordinate can pass the distance test at the same time. I.e. thickness along that coordinate must be equal to 1, so the "euclidean distance" thickness would be the projection of a unit vector along that coordinate on the direction perpendicular to the line.

$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .