1

Say I have a line segment with known length L and a grid of pixels with dimension dX and dY along the X and Y axes. The line can begin from any arbitrary point within the grid and can have any arbitrary angle w.r.t the X and Y axes but its length is always the same (L). I would like to know what the maximum number of cells in the grid is that can be intersected with the line?

I was thinking something like this but am not sure:

ceil(L/dX) + ceil(L/dY) + 1

Sample Grid


Numerical Simulations

In order to actually find what the correct answer is, I did a numerical simulation experiment where basically hundreds of millions of randomly generated lines with length L were started from a random point and fired randomly along various directions and the exact number of intersected grid pixels was counted to see what the max value is (since this was numerical simulations, I cannot say for sure that these values are correct):

  1. L = 200, dX = 5, dY = 10 => Max no of intersected voxels = 47
  2. L = 200, dX = 15, dY = 2 => Max no of intersected voxels = 103
  3. L = 200, dX = 10, dY = 10 => Max no of intersected voxels = 31
  • Does "intersect with the line" mean that the line passes through the interior, and not the boundary of the cell? – Chickenmancer Mar 05 '21 at 22:47
  • The boundaries are not of great concern really. If a line begins from a cell boundary or ends at a cell boundary such that its intersection length with the cell is 0, we can discard the cell as there is really no intersection. – If_You_Say_So Mar 06 '21 at 02:09

2 Answers2

3

If a line segment from one corner of a pixel to another corner has length less than $L$, we can move the endpoints a tiny bit to avoid ever intersecting a corner between pixels, and still have length less than $L$.

In this case, if the original corners were at horizontal distance $x \cdot dX$ and vertical distance $y \cdot dY$, the new segment will cross $x+1$ vertical cell boundaries and $y+1$ horizontal cell boundaries; since it will cross $x+y+2$ cell boundaries total, it will pass through $x+y+3$ cells.

So we want to maximize $x+y+3$ subject to $(x \cdot dX)^2 + (y \cdot dY)^2 < L^2$; the $<$ condition will usually not bother us, but it is there to make sure that a line segment beginning at a corner of a cell does not count as intersecting the cell.

If we forget about the integer constraint (that $x,y \in \mathbb Z$), then the method of Lagrange multipliers tells us that the maximum occurs close to the point where the ellipse $(x \cdot dX)^2 + (y \cdot dY)^2 = L^2$ has a tangent line with slope $-1$ (equal to the slope of $x+y+3=k$). This solution is $$ (x,y) = \left(\frac{dY}{dX} \cdot \frac{L}{\sqrt{dX^2 + dY^2}}, \frac{dX}{dY} \cdot \frac{L}{\sqrt{dX^2 + dY^2}}\right) $$ with $x+y+3 = L \sqrt{\frac1{dX^2} + \frac1{dY^2}} + 3$.

If we replace $x,y$ by $\lceil x\rceil - 1$ and $\lceil y \rceil-1$, we get an integer point where $(x \cdot dX)^2 + (y \cdot dY)^2 < L^2$, and the value $x+y+3$ decreases by at most $2$. (If it decreased by exactly $2$, then the original value $x+y+3$ was already unachievable, since it required having $(x \cdot dX)^2 + (y \cdot dY)^2 = L^2$ exactly.) Therefore this gives us the correct answer up to an error of $1$.

Unfortunately, fixing this error would require a search of integer points in some elliptical sliver of the form $(x \cdot dX)^2 + (y \cdot dY)^2 < L^2$ and $x+y \ge k$. Essentially, we are looking for fractional solutions that are worse than the optimal one but "round better". This is something that has to be done case-by-case.

But if you are okay with being off by $1$ sometimes, then we do have a closed-form solution: we get $$\left\lceil \frac{dY}{dX} \cdot \frac{L}{ \sqrt{dX^2+dY^2}}\right\rceil +\left\lceil \frac{dX}{dY} \cdot \frac{L}{ \sqrt{dX^2+dY^2}}\right\rceil +1$$ (or possibly $+2$) intersections.

Misha Lavrov
  • 142,276
  • This is a very tight estimate indeed. Maybe you can tighten it even further by removing the $+1$ at the end, since on average in simulations the underestimate occurs extremely rarely (read that never). – V.S.e.H. Mar 07 '21 at 14:22
  • 1
    I have not tried simulations, but I did try some problems with $dX=dY=1$ and small integer values of $L$, and I get that the correct answer is the lower value for $L=5,6,9,10,12,13$ and the higher value for $L=4,7,8,11,14$ which seems like a pretty even split. – Misha Lavrov Mar 07 '21 at 15:09
  • I ran an experiment over the weekend where I basically tried to find what the correct number should be (please see the edit in my question). Your method and answer seem to be very reasonable and consistent with the results of my little simulation experiment. Thank you for taking the time to write an answer! :) – If_You_Say_So Mar 08 '21 at 14:32
0

Let us switch to polar coordinates.

$$x=L\cdot \cos(\phi)-x_1\\y=L\cdot\sin(\phi)-y_1$$ where $(x_0,y_0)$ is a known end-point and where: $$x_1 = \min(x_0,d_X-x_0)\\y_1 = \min(y_0,d_Y-y_0)$$

This corresponds to choosing the quandrant which points in direction closest to a corner.

We can now express the number as a function of $\phi\in[0,\pi/4]$: $$1+\left\lfloor\frac{x}{d_X}\right\rfloor+\left\lfloor\frac{ y}{d_Y}\right\rfloor=1+\left\lfloor\frac{L\cdot \cos(\phi)-x_1}{d_X}\right\rfloor+\left\lfloor\frac{L\cdot \sin(\phi)-y_1}{d_Y}\right\rfloor$$

More specifically, what we seek will be:

$$1+\max_{\phi}\left\{ \left\lfloor\frac{L\cdot \cos(\phi)-x_1}{d_X}\right\rfloor+\left\lfloor\frac{L\cdot \sin(\phi)-y_1}{d_Y}\right\rfloor \right\}$$

In most cases I suppose this will be related to the maximum Manhattan distance given a known Euclidean distance and a known distance between consecutive streets and avenues.

I think max shall be somehwere near $\phi \approx \arctan(d_Y/d_X)$.

In the special case if $d_X=d_Y$, then a tighter estimate shall be $\phi\approx$ 45 degrees and a factor $\sin(\pi/4)=\cos(\pi/4) = \frac{1}{\sqrt{2}}$ smaller than your estimate. There do as you mention exist isolated $\phi$ values for where the line passes precisely through integer coordinates on the lattice. But arbitrarily close to every such $\phi$ will be one with larger value.

But your estimate should work good enough as a not-as-tight upper bound.

mathreadler
  • 25,824
  • Thanks for the answer. Quick follow-up questions:1- Did you mean "(x0, y0)" is a known end point (and not (x0,x0))? 2- Also, I am wondering why you use the flooring operation symbol and not the ceiling operation? 3- Also, are you sure about the arctan(dy/dx) part? Assuming that dX = dY, for a line starting from (0,0) , do we necessarily have the highest number of intersected pixels for a 45-degree line? – If_You_Say_So Mar 06 '21 at 14:58
  • Yep you are right about $(x_0,y_0)$ and that special case that 45 degrees will not always give the highest number, but there will always exist a neighborhood around 45degrees which will give the optimum. I will rephrase it. – mathreadler Mar 06 '21 at 15:53
  • Thanks. Also, why did you use the the flooring operator in your eqs? Is that a typo or do you actually mean it? – If_You_Say_So Mar 06 '21 at 16:02
  • Try $L = 0.05$ and $(x_0,y_0) = (0.5,0.5)$ and $d_X=d_Y=1$. If we used ceil we would get $1+1+1=3$ pixels, but it is clearly within $1$ pixel. Or do you mean that we are not allowed to know what $(x_0,y_0)$ is beforehand? – mathreadler Mar 06 '21 at 16:12
  • Not really. Sticking to your example: Think of a line with L = 0.05 starting from [0.98 0.99] at angle 45-degree toward the positive x and y values. i.e. line equation: y = x + 0.01. This line actually crosses 3 pixels when L = 0.05. – If_You_Say_So Mar 06 '21 at 20:00
  • @Person.Woman.Man.Camera.TV Okay. That was not given in the question. – mathreadler Mar 06 '21 at 20:08
  • Yes it is good to be as clear as possible with such details as people might waste their time trying to answer the wrong question otherwise. I have edited the question so that other's don't fall into the same trap. – mathreadler Mar 06 '21 at 21:44
  • I am not sure if I agree that this sentence needs clarification: "The line can begin from any arbitrary point within the grid and can have any arbitrary angle w.r.t the X and Y axes but its length is always the same (L)". – If_You_Say_So Mar 07 '21 at 04:20
  • Yes an arbitrary point, but it does not say if it is a known or unknown arbitrary point. – mathreadler Mar 07 '21 at 08:13