Correctly count all unique near-coincident hexagonal lattice pairs?

If you're trying to pick points geometrically you are working too hard.

here's how it's really done, with the example of finding a decent match for a size ratio of $x = \pi$.

In this we will be iterating over the Loeschain numbers, which is equivalent to considering points in order of euclidean distance from the origin.

We'll start with $a_2=1$, using $k=1$ and $\ell=0$. We now find lower and upper $a_{1-}$, $a_{1+}$ and corresponding $i_-,j_-, i_+, j_+$ by starting at $\lfloor x^2 \rfloor$ and $\lceil x^2 \rceil$ and going down and up respectively until we land on Loeschian numbers. Then $p = a_{1-}/a_2$ and $q = a_{1+}/a_2$ are our current best approximations -- well, the squares of the best approximations; working with integers is less of a pain in general.

Repeatedly: increment $a_2$ to the next Loeschian number. Calculate $a_2x^2$ again, and also $a_2p$ and $a_2q$. Starting at $\lfloor a_2x^2 \rfloor$ and $\lceil a_2x^2 \rceil$, again go down and up respectively to find Loeschian numbers... but if you reach $a_2p$ or $a_2q$ before that happens, give up: something earlier worked better. On the other hand, if you do find one, then you can update $p$ or $q$ as appropriate.

This can go on forever, so long as it's never true that $a_2x^2$ is itself a Loeschian number; in that case you've found an exact match.


So, for $\pi$:

  • $\pi^2 \approx 9.9$, so our starting point is $p=9$, $q=12$
  • $3\pi^2 \approx 29.6$, and we find $3\cdot9 < 28 < 3\pi^2 < 31 < 3\cdot12$, so now $p=28/3$, $q=31/3$
  • $4\pi^2 \approx 39.5$, $4\frac{28}{3} < 39 < 4\pi^2 < 4\frac{31}{3}$ -- this time, we don't find a better upper bound, so only $p$ changes, to $\frac{39}{4}$.
  • $7\pi^2 \approx 69.1$, and in this case we don't find any thing that improves the situation.
  • Proceeding in this fashion we get $q = \frac{91}{9}$, $q = \frac{121}{12}$, $p = \frac{127}{13}$, $q=\frac{129}{13}$, $p = \frac{157}{16}$, $q=\frac{208}{21}$, and $q=\frac{247}{25}$, and so on.

Not all the numbers we get are optimal: $\frac{157}{16}$ isn't as good as $\frac{129}{13}$, but it is on the low side instead of the high side so we get to keep it for now to keep us on track.