3

This question is related to this other question.

I would like to write an algorithm to enumerate all subsets of the $n^2$ squares of an $n \times n$ grid such that, for each subset, there exists a straight line crossing the inner region of all and only the squares of that subset. Inner region means that the line cannot cross just the border or the corner of the square.

Here you can find an interactive implementation of these "discrete straight lines".

The obvious algorithm that comes to mind is to take any two point on the outer border of the grid, draw a line passing through the two points and calculate all crossed squares. The problem is an actual algorithm can try only a finite number of couple of points. Therefore a side question could be how many points in the outer border must be considered, e.g. $2n$ for each side? And with what disposition?

Any other idea?

  • Sorry, but I don't understand what are the lines you are interested in : if it is the horizontal and vertical lines, it looks trivial... – Jean Marie Aug 29 '23 at 15:54
  • 1
    @JeanMarie no, enter Lines: $1$, then try to pick and move the black bullets in the linked demonstration (hoping that your browser supports it). It is any line that crosses the grid, with whatever slope and intercept. – Fabius Wiesner Aug 29 '23 at 15:58

1 Answers1

1

The square will be the set $[0,n]\times [0,n]$ in the plane. A lattice point is a point $P=(x,y)$ where $x,y\in \{0,1,\dots,n\}$. I will show how to generate all sets of squares cut by lines which do not pass through any lattice points.

Given two lattice points, $P_1$ and $P_2$, let $\ell(P_1,P_2)$ be the line which passes just below $P_1$, and which passes just above $P_2$. For this to be well-defined, we must additionally assume that the line segment $\overline{P_1P_2}$ hits no other lattice points. Equivalently, if $P_1=(x_1,y_1)$ and $P_2=(x_2,y_2)$, we require $\gcd(x_2-x_1,y_2-y_1)=1$.

Lemma: For every efficient line, $m$, there exist lattice points $P_1$ and $P_2$ such that $m$ cuts the exact same set of squares as $\ell(P_1,P_2)$.

Proof: Here is an algorithm to find $P_1$ and $P_2$, given $m$.

  1. Translate $m$ upwards until just before it hits some lattice point. Call this point $P_1$.
  2. Rotate $m$ clockwise about $P_1$ until just before you hit some other lattice point. Call this point $P_2$.
  3. If $m$ is passing above $P_2$, then we have found the desired $(P_1,P_2)$. Otherwise, set $P_1$ to be the current value of $P_2$, and return to step $2$.

All that remains is to actually generate the set of squares cut by $\ell(P_1,P_2)$. The previous version of this algorithm had an algorithm which did not work, because I missed a lot of edge cases. I now have a working program, and I checked small cases by hand to see if it was generating the correct sets. However, it is a bit too complicated for me to fully describe in this answer.

Mike Earnest
  • 86,264
  • I might be mistaken, but consider a $4 \times 4$ square (you can use the demonstration link) with squares $(i,j)$ (row, column) and the square set ${(1,1), (2,1), (3,1), (4,1), (4,2)}$. There is an efficient line crossing these squares, but not an $\ell(P_1,P_2)$. Isn't it? – Fabius Wiesner Aug 29 '23 at 17:22
  • It is of the form $\ell(P_1,P_2)$, with $P_1=(0,0)$ and $P_2=(3,1)$. – Mike Earnest Aug 29 '23 at 17:25
  • That line crosses square $(3,2)$ as well I think. – Fabius Wiesner Aug 29 '23 at 17:32
  • @BillyJoe It does not cross $(3,2)$, because you need to translate down slightly. Here is the line I am envisioning: https://i.sstatic.net/d5VLM.png – Mike Earnest Aug 29 '23 at 17:38
  • Sorry, my bad, I was thinking only at $P_1,P_2$ on the outer perimeter of the grid, while they can be inside. – Fabius Wiesner Aug 29 '23 at 18:08
  • Interesting elements in this article. – Jean Marie Aug 29 '23 at 18:16
  • Is there a succinct way to compute the squares that are not near $(k,y_k)$? For example, consider $n=10$, $P_1=(4,10)$, and $P_2=(6,1)$. The recipe given yields the disconnected set ${(4,10),(5,6),(5,10),(6,1),(6,2),(6,6),(7,1)}$. – RobPratt Aug 31 '23 at 21:44
  • @RobPratt I will get back to you. There is actually a LOT wrong with this answer, and I will update it when I know how to fix it. First, my claim that every set is of the form $\ell(P_1,P_2)$ is false! For $n=4$, there is no way to generate ${((1,1),(1,2),(2,2),(3,2)(3,3),(4,3),(4,4)}$. Instead, every set can be generated by a line which goes just below $P_1,$ but just above $P_2$. Secondly, my line drawing algorithm only works for positively sloped lines with slope less than one (you can fix that by appropriately reflecting the input/output). Apologies for my sloppiness. – Mike Earnest Aug 31 '23 at 22:04
  • @RobPratt OK, now everything should be working. This time, I found 4236 sets of points for $n=10$. – Mike Earnest Sep 01 '23 at 22:25
  • Based on your new results, I updated my answer to the linked question. – RobPratt Sep 05 '23 at 21:04