-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpaper.tex
1577 lines (1362 loc) · 117 KB
/
paper.tex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
\documentclass[12pt, a4paper]{article}
\usepackage{mathptmx} % this doesn't have \jmath
%\usepackage{newtxtext, newtxmath} % I can't make this work
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage[colorlinks,citecolor=blue,urlcolor=blue,linkcolor=blue,linktocpage=true]{hyperref}
%\RequirePackage{hypernat}
\usepackage{amssymb}
\usepackage{graphicx}
\usepackage[tight]{subfigure}
\usepackage{authblk}
\usepackage{bigints}
\addtolength{\textheight}{38mm}
\addtolength{\voffset}{-22mm}
\addtolength{\textwidth}{-3mm}
\addtolength{\hoffset}{1.5mm}
%\arxiv{arXiv: ***}
\newcommand{\mr}{\mathrm}
\newcommand{\st}{\mid}%{\,|\,}
\newcommand{\cond}{\,|\,} % less spacing than \mid
\newcommand{\diff}{\mathrm d}
\let\Re\relax % remove the command...
\DeclareMathOperator{\Re}{Re} % ...to define it differently
\let\Im\relax
\DeclareMathOperator{\Im}{Im}
\DeclareMathOperator{\E}{E}
\newcommand{\funt}{\tau} % function that gives maximum number of touched tiles for real-valued length
\newcommand{\funl}{\lambda} % function that gives infimum real-valued length for a given number of touched tiles
\newcommand{\funti}{T} % function that gives maximum number of touched tiles for integer-valued length
\newcommand{\funli}{\Lambda} % function that gives minimum integer-valued length for a given number of touched tiles
\newcommand{\funta}{\varphi} % function that gives average number of touched tiles for real-valued length
%\newcommand{\funca}{\psi} % function that gives average number of grid line crossings for real-valued length
\newcommand{\probmax}{\rho} % function that gives the probability of achieving the maximum number of touched tiles in the square case for real-valued length
\newcommand{\ras}{\sigma}
\newcommand{\len}{\ell} % length variable, real-valued
\newcommand{\leni}{\ell} % length variable, integer-valued. Same notation, but I keep the different command just in case
\newcommand{\tiles}{t} % number of tiles (integer-valued)
\newcommand{\isolr}{i^+}
\newcommand{\jsolr}{j^+}
\newcommand{\isoli}{i^\ast}
\newcommand{\jsoli}{j^\ast}
\newcommand{\genfun}{q}
\newcommand{\genvar}{s}
\newcommand{\roundterm}{r}
\newcommand{\mss}{M}
%\newcommand{\nextrat}{K}
\newcommand{\funcirc}{\Omega}
\newcommand{\funcircesc}{\bar \Omega}
\newcommand{\fdp}{p}
\newcommand{\lra}{\alpha}
\newcommand{\lrb}{\beta}
\newcommand{\intervx}{I}
\newtheorem{proposition}{Proposition}%[section]
\newtheorem{theorem}{Theorem}%[section]
\newtheorem{corollary}{Corollary}%[section]
%\newtheorem{lemma}{Lemma}%[section]
\makeatletter
\renewcommand\theenumi{(\@roman\c@enumi)} % this affects \ref's too
\renewcommand\labelenumi{\theenumi} % this doesn't affect \ref's
\makeatother
\graphicspath{{figures/}}
% !TeX spellcheck = en_GB
% This is so that TexStudio sets language automatically
\begin{document}
\title{
On the number of tiles visited by a\\
line segment on a rectangular grid
}
% Authors with different affiliations: from https://tex.stackexchange.com/a/370813/90222
\author[1]{Alex Arkhipov}
\author[2]{Luis Mendo}
\affil[1]{\small E-mail: \texttt{[email protected]}}
\affil[2]{\small Universidad Polit\'ecnica de Madrid. E-mail: \texttt{[email protected]}}
%Information Processing and Telecommunications Center, Universidad Polit\'ecnica de Madrid\\
%Avenida Complutense, 30. 28040 Madrid, Spain.\\
%ORCID: Luis Mendo: 0000-0001-5691-714X
\maketitle
\begin{abstract}
Consider a line segment placed on a two-dimensional grid of rectangular tiles. This paper addresses the relationship between the length of the segment and the number of tiles it visits (i.e.~has intersection with). The square grid is also considered explicitly, as some of the specific problems studied are more tractable in that particular case. The segment position and orientation can be modelled as either deterministic or random. In the deterministic setting, the maximum possible number of visited tiles is characterized for a given length, and conversely, the infimum segment length needed to visit a desired number of tiles is analyzed. In the random setting, the average number of visited tiles and the probability of visiting the maximum number of tiles on a square grid are studied as a function of segment length. These questions are related to Buffon's needle problem and its extension by Laplace.
\emph{Keywords:} Discrete geometry, Geometric probability, Rectangular lattice, Buffon's needle problem.
\emph{MSC2020:} 52C99, 60D05.
% 52C99: Discrete geometry: None of the above, but in this section
% 60D05: Geometric probability and stochastic geometry
\end{abstract}
\section{Introduction}
\label{part: intro}
Given $a, b \in \mathbb R^+$, consider a grid on $\mathbb R^2$ formed by rectangular \emph{tiles} of width $a$ and height $b$. A line segment of length $\len \in \mathbb R^+$ is located on the plane with arbitrary position and orientation. The segment is said to \emph{visit} a tile if it intersects its interior.\footnote{
The definition uses the interior of the tile, excluding the border, to avoid uninteresting results such as a ``zero-length'' segment visiting (a vertex of) $4$ tiles.}
This paper studies the relationship between the length of the segment and the number of visited tiles. The motivation comes from the classical Buffon-Laplace needle problem (i.e.~a segment with random position and orientation on a rectangular grid), of which a modified version is considered, wherein the segment position and orientation are parameters that can be chosen to maximize the number of visited tiles. In addition, the probability of visiting that maximum number of tiles in the classical (random) setting is studied.
Specifically, two different settings are considered, which correspond to the segment position and orientation being deterministic or random, respectively. In the \emph{deterministic} case, the relevant questions are:
\begin{enumerate}
\renewcommand{\labelenumi}{(1\alph{enumi})}
\renewcommand{\theenumi}{(1\alph{enumi})}
\item
\label{prob: determ dir}
What is the maximum number of tiles that the segment can visit given its length?
\item
\label{prob: determ inv}
Conversely, what length should a segment have to visit a given number of tiles?
\end{enumerate}
In the \emph{random} setting, if the segment position and orientation are uniformly distributed (this will be precisely defined later),
\begin{enumerate}
\renewcommand{\labelenumi}{(2\alph{enumi})}
\renewcommand{\theenumi}{(2\alph{enumi})}
\item
\label{prob: rand ave}
What is the average number of tiles visited by a segment of a given length?
\item
\label{prob: rand prob}
How often does the random segment visit the maximum number of tiles?
\end{enumerate}
As an example of question~\ref{prob: determ dir}, consider $a=1.35$, $b=1$. A segment of unit length can be placed as shown in Figure~\ref{fig: examples} (left) to make it visit $3$ tiles. In fact, this is the maximum number for $\len=1$. The figure also illustrates that the solution for length $2.4$ is $5$ (center), and for $4.7$ it is $8$ (right).
\begin{figure}
\centering%
\includegraphics[width=.85\textwidth]{examples_1p35}%
\caption{Examples for $a=1.35$, $b=1$; $\len=1$, $\len=2.4$ and $\len=4.7$
}%
\label{fig: examples}%
\end{figure}%
An equivalent formulation of the problem is obtained by allowing segments of length $\len$ \emph{or smaller}. The equivalence is clear from the fact that reducing the length cannot increase the number of visited tiles. Either of these formulations will be referred to as the \emph{direct} problem.
The \emph{inverse} problem~\ref{prob: determ inv} is, given $\tiles \in \mathbb N$, to determine the infimum length of all segments that visit at least $\tiles$ tiles. If the length can take any real value the infimum is not a minimum, because given any segment it can be shortened by some small amount without changing the number of visited tiles. This is a consequence of the interior of each tile being an open set.
The direct and inverse problems are closely related. Namely, if $\len$ is the infimum of all lengths that allow visiting at least $\tiles$ tiles (inverse problem), $\tiles$ is the maximum number of tiles that can be visited with lengths slightly greater than $\len$ (direct problem).
To address the remaining two questions, the notion of a \emph{random} segment of a given length needs to be precisely defined. This is done as follows. By symmetry, one endpoint of the segment can be assumed to lie in a fixed, reference tile. The position of this endpoint is \emph{uniformly} distributed on the tile. The segment orientation has a \emph{uniform} distribution on $[0,2\pi)$, and is \emph{independent} of the endpoint position. Solving the problem~\ref{prob: rand ave} of how many tiles the segment visits on average also answers, as will be seen, the inverse question (segment length to visit a given number of tiles on average). A natural, related problem~\ref{prob: rand prob} is with what probability the segment visits the maximum number of tiles.
% Do we need to discuss potential applications of this (if we can find any)? This probably depends on the journal
% Do we need a section with conclusions? Probably not; depends on the journal
The questions studied in this paper are related to Buffon's needle problem and Laplace's extension of it, as stated at the outset. Buffon's original problem considers a plane with vertical lines a distance $a$ apart. A needle is placed on the plane with uniformly random position\footnote{It suffices to define position using a horizontal coordinate modulo $a$, for which a uniform distribution can be defined.} and orientation, and the probability of the needle crossing a line is studied. For a needle of length $\len = a$, this probability is $2/\pi$ (and hence repeated trials of this experiment can be used to estimate $\pi$). This is generalized in \cite{Ramaley69} to $2\len /(\pi a)$ for the expected number of crossings of a needle with arbitrary length.
The Buffon-Laplace needle problem \cite[section~1.1]{Mathai99} considers a needle randomly placed on a grid of rectangular tiles of width $a$ and height $b$. The number of visited tiles equals one plus the number of crossings almost surely. The probability of the needle staying within a single tile is computed in \cite{Arnow94} for the case where $\len < \min\{a,b\}$. One of the problems considered in this paper, as mentioned above, is the complementary question~\ref{prob: rand prob} of the probability that the needle visits the maximum number of tiles possible for its length.
The rest of the paper is organized as follows. Fundamental results are presented in \S\ref{part: fund results}, which form the basis of both the deterministic and probabilistic analyses. The direct and inverse problems for a deterministic segment are considered in \S\ref{part: max}, first for arbitrary grids and then for a square grid. The analysis for the random segment is carried out in \S\ref{part: rand}. The average number of tiles is computed for arbitrary grids, and the probability that the segment visits the maximum number of tiles is obtained for a square grid.
The symbols $\lfloor x \rfloor$ and $\lceil x \rceil$ respectively denote the floor (rounding down) and ceiling (rounding up) operations. The functions $\arcsin x$ and $\arccos x$ are defined as their principal branches in the usual way. In particular, the output angles are in $[0, \pi/2]$ for $x \in [0,1]$.
% The notation $[\mathsf S]$ will be used for the Iverson function, which equals $1$ if statement $\mathsf S$ is true and $0$ otherwise.
\section{Fundamentals}
\label{part: fund results}
For a grid with horizontal spacing $a$ and vertical spacing $b$, lines $x = ka$ or $y = kb$ with $k \in \mathbb Z$ will be called \emph{grid lines}. A \emph{tile} is delimited by two pairs of consecutive horizontal and vertical grid lines. The intersection points of horizontal and vertical grid lines will be called \emph{grid points}. These correspond to vertices of the tiles.
Every segment has an associated \emph{discrete bounding rectangle}, which is the minimum-size rectangle that is formed by grid lines and contains the segment. More specifically, if the segment has endpoints $(x_1,y_1)$, $(x_2,y_2) \in \mathbb R^2$, its discrete bounding rectangle has lower-left and upper-right corners respectively given as
\begin{align*}
& (\lfloor\min\{x_1, x_2\}/a\rfloor a, \lfloor\min\{y_1,y_2\}/b\rfloor b), \\
& (\lceil\max\{x_1, x_2\}/a \rceil a, \lceil\max\{y_1,y_2\}/b \rceil b).
\end{align*}
The dimensions of the discrete bounding rectangle, \emph{normalized} to the tile width and height respectively, are two integer numbers $i$, $j$. Two examples are illustrated in Figure~\ref{fig: discrete bounding rectangle and touched tiles}, both with $i=5$, $j=4$. All tiles visited by the segment are contained in the discrete bounding rectangle. Note also that the rectangle can have $i=0$ or $j=0$ if the segment coincides with part of a grid line.
\begin{figure}
\centering%
\subfigure[The segment does not pass through any interior grid points]{%
\label{fig: S_nogridpoints}%
\includegraphics[width=.49\textwidth]{S_nogridpoints_1p35}%
}\hfill%
\subfigure[The segment passes through some interior grid points]{%
\label{fig: S_gridpoints}%
\includegraphics[width=.49\textwidth]{S_gridpoints_1p35}%
}%
\caption{Discrete bounding rectangle and visited tiles
}%
\label{fig: discrete bounding rectangle and touched tiles}%
\end{figure}%
\begin{proposition}
\label{prop: i+j-1}
Consider an arbitrary segment, and let $i, j$ respectively denote the normalized width and height of its discrete bounding rectangle. If $i, j \geq 1$, the number of tiles visited by the segment is at most $i+j-1$. This bound is attained if and only if the segment does not pass through any grid point in the interior of the rectangle.
\end{proposition}
\begin{proof}
The segment visits, by definition, two tiles in opposite corners of the discrete bounding rectangle. It can be assumed, without loss of generality, that those tiles are in the lower-left and upper-right corners of the rectangle, as in Figure~\ref{fig: discrete bounding rectangle and touched tiles}. The visited tiles can be thought of as following a path within the discrete bounding rectangle. Starting at the lower-left tile, the next tile can be the one to the left, the one above, or the one above and to the left. The latter case occurs if and only if the segment passes through the grid point between those two tiles.
Since the segment follows a straight line, once it ``leaves'' a row of tiles in its path from the lower-left to the upper-right corner, it can never visit any more tiles from that row. The same observation applies to the columns.
This implies that the maximum number of visited tiles is $i+j-1$, which is attained if and only if the segment avoids all grid points in the interior of the discrete bounding rectangle, as in Figure~\ref{fig: S_nogridpoints}. Note that grid points at the corners of the rectangle do not count for this; and that the segment cannot pass through any other grid points on the rectangle border, because that would imply $i=0$ or $j=0$. Figure~\ref{fig: S_gridpoints} illustrates a case where the maximum is not attained.
\end{proof}
\begin{proposition}
\label{prop: len ineq i j}
Consider $a, b, \len \in \mathbb R^+$ and $i, j \in \mathbb N$, $i, j \geq 2$ arbitrary.
\begin{enumerate}
\item
\label{prop: len ineq i j: ineqs}
The following inequalities hold for any segment with length $\len$ whose discrete bounding rectangle has normalized dimensions $i, j$:
\begin{align}
\label{eq: len > sqrt}
\len &> \sqrt{(i-2)^2 a^2 + (j-2)^2 b^2}, \\
\label{eq: len leq sqrt}
\len &\leq \sqrt{i^2 a^2 + j^2 b^2}.
\end{align}
\item
\label{prop: len ineq i j: exist}
Conversely, if $\len$, $i$, $j$ satisfy \eqref{eq: len > sqrt} and \eqref{eq: len leq sqrt} there exists a segment of length $\len$ whose discrete bounding rectangle has normalized dimensions $i$ and $j$.
\item
\label{prop: len ineq i j: ineq, exist}
There is a segment of length not exceeding $\len$ that has a discrete bounding rectangle with normalized dimensions $i, j$ if and only if \eqref{eq: len > sqrt} holds.
\end{enumerate}
\end{proposition}
\begin{proof}
\ref*{prop: len ineq i j: ineqs} The inequalities follow from the fact that the segment endpoints lie in the interiors or on the outer edges of two tiles in opposite corners of the discrete bounding rectangle. This is illustrated in Figure~\ref{fig: len ineq i j} for two specific $(i,j)$ pairs. For each $(i,j)$, segments are shown with lengths close to either of the two bounds. Note that inequality \eqref{eq: len > sqrt} is valid even for $i=2$, $j=2$, in which case it reduces to $\len>0$.
\ref*{prop: len ineq i j: exist} For $a$, $b$, $\len$, $i$, $j$ satisfying the two inequalities, a segment of length $\len$ can be found that has its endpoints in the interiors or on the outer edges of the two shaded tiles of a rectangle with normalized dimensions $i$ and $j$ (see Figure~\ref{fig: len ineq i j}), which is thus the discrete bounding rectangle of that segment.
\begin{figure}
\centering%
\subfigure[$i=4$, $j=3$]{%
\label{fig: len ineq 4 3}%
\includegraphics[width=.49\textwidth]{L_ineq_4_3_1p35}%
}\hfill%
\subfigure[$i=4$, $j=2$]{%
\label{fig: len ineq 4 2}%
\includegraphics[width=.49\textwidth]{L_ineq_4_2_1p35}%
}%
\caption{Relationship between segment length $\len$ and dimensions $i$, $j$ of the discrete bounding rectangle
}%
\label{fig: len ineq i j}
\end{figure}%
\ref*{prop: len ineq i j: ineq, exist} ``\eqref{eq: len > sqrt} $\Rightarrow$ there is a segment\ldots'': Assume that \eqref{eq: len > sqrt} holds. It is always possible to choose a length equal to or smaller than $\len$ such that both \eqref{eq: len > sqrt} and \eqref{eq: len leq sqrt} hold. The result follows, for that length, from part~\ref{prop: len ineq i j: exist}.
``There is a segment\ldots $\Rightarrow$ \eqref{eq: len > sqrt}'': Assume that a segment exists with length $\len' \leq \len$ and with a discrete bounding rectangle of normalized dimensions $i$, $j$. From part~\ref{prop: len ineq i j: ineqs} it follows that inequality \eqref{eq: len > sqrt} holds for the length $\len'$, and thus for $\len$.
\end{proof}
Consider the problem of maximizing the number of visited tiles for a given length. According to Proposition~\ref{prop: i+j-1}, the position and orientation of the segment should be chosen to obtain $i+j-1$ as large as possible, where $i$ and $j$ are the normalized dimensions of its discrete bounding rectangle. On the other hand, Proposition~\ref{prop: len ineq i j} restricts the $i, j$ values that can be achieved with a given length. A relevant question is: are there any $(i,j)$ pairs that can be disregarded irrespective of the length $\len$? In other words, what is the ``smallest'' subset of $\mathbb N^2$ such that the $(i,j)$ pair that maximizes the number of tiles for any given length can always be found within that subset?
For instance, it is clear from Figure~\ref{fig: examples} that segment orientations near the vertical or horizontal directions (resulting in $i=1$ with large $j$, or $j=1$ with large $i$) will not maximize the number of visited tiles, and thus the corresponding $(i,j)$ pairs can be discarded. On the other hand, the set of optimal $(i,j)$ pairs must contain one such pair for each possible value of $i+j-1$, so that the set can produce that value as the solution (maximum number of visited tiles) for certain lengths. It is insightful to examine two specific examples (Figure~\ref{fig: ijLS}) before giving an explicit formula for the coordinates of the optimal pairs.
Consider $a=b=1$ first. This is illustrated in Figure~\ref{fig: ijLS_1}. Note that in this and in the next figures the axes represent $ia$ and $jb$ (not $i$ and $j$). In this graph, each dashed diagonal line contains points $(ia,jb)$ with the same $i+j-1$; and the lower bound \eqref{eq: len > sqrt} corresponds to an arc centered at $(2a,2b)$.
\begin{figure}
\centering%
\subfigure[$a=b$]{%
\label{fig: ijLS_1}
\includegraphics[width=.7\textwidth]{ijLS_1}%
}\\%
\centering%
\subfigure[$a=1.35b$]{%
\label{fig: ijLS_1p35}
\includegraphics[width=.7\textwidth]{ijLS_1p35}%
}%
\caption{Relationship of segment length and number of visited tiles with the width and height of the discrete bounding rectangle
}%
\label{fig: ijLS}%
\end{figure}%
For a given $\len \in \mathbb R^+$, the $(i,j)$ pairs that can be achieved with segments of length not exceeding $\len$ are, by Proposition~\ref{prop: len ineq i j}.\ref{prop: len ineq i j: ineq, exist}, those that satisfy \eqref{eq: len > sqrt}. Thus for a given value of $i+j-1$ the optimal $(i,j)$ pair (the one that can be achieved with the smallest length) is determined by the condition that the point $(ia,jb)$ minimizes the distance to $(2a,2b)$. Such pairs are depicted as filled circles in the figure, and the arcs represent the inequality \eqref{eq: len > sqrt} for each of the optimal pairs.
Conversely, given a length $\len$, the maximum number of visited tiles will be achieved with one of these pairs, namely the pair $(i,j)$ such that $(ia,jb)$ is on the uppermost (or rightmost) diagonal line while still being contained in the circle of radius $\len$ centered at $(2a,2b)$.
The optimal pairs in this specific case have the form $(i,i)$ or $(i,i-1)$, as seen in the figure. Due to symmetry, any pair $(i,i-1)$ could be replaced by $(i-1,i)$. This illustrates that the set of optimal pairs is not unique in general.
As a second example, consider $a=1.35$, $b=1$. This is depicted in Figure~\ref{fig: ijLS_1p35}. Again, the optimal pair $(i,j)$ for each diagonal is that for which the point $(ia,jb)$ is closest to $(2a,2b)$; but in this case the $i$, $j$ coordinates of these pairs do not follow a rule as simple as in the previous example.
The following proposition gives an explicit method to obtain a set of optimal $(i,j)$ pairs. This set will be denoted as $\mss = \{(i_3,j_3), (i_4.j_4), \ldots\}$, where the pair $(i_\tiles,j_\tiles)$ corresponds to $i+j-1 = \tiles$.
\begin{proposition}
\label{prop: min suff set, form}
Given $a, b \in \mathbb R^+$, a set of optimal pairs $\mss = \{(i_3,j_3), (i_4.j_4), \ldots\}$ can be obtained as
\begin{align}
\label{eq: min suff set, form, i}
i_\tiles &= \left\lfloor \frac{(\tiles-3) b^2}{a^2+b^2} + \frac 5 2 \right\rfloor, \\
\label{eq: min suff set, form, j}
j_\tiles &= \left\lceil \frac{(\tiles-3) a^2}{a^2+b^2} + \frac 3 2 \right\rceil,
\end{align}
where $i_\tiles + j_\tiles-1 = \tiles$. All pairs $(i_\tiles,j_\tiles)$ are strictly below the line
\begin{equation}
\label{eq: upper bound, line}
j = \frac{i a^2}{b^2} - \frac{3a^2}{2b^2} + \frac 5 2,
\end{equation}
and above or on the line
\begin{equation}
\label{eq: lower bound, line}
j = \frac{i a^2}{b^2} - \frac{5a^2}{2b^2} + \frac 3 2.
\end{equation}
\end{proposition}
\begin{proof}
For each $\tiles \geq 3$, the pair $(i_\tiles,j_\tiles)$ should be chosen as that on the line $i+j-1=\tiles$ which minimizes $(i-2)^2 a^2 + (j-2)^2 b^2$. This allows maximizing the sum $i+j-1$, and thus the number of visited tiles, for a given length restriction; or visiting a specified number of tiles with lengths as small as possible.
Consider, for the moment, $i$, $j$ as if they were real-valued variables, and denote $x=ia$, $y=jb$. The line $i+j-1 = \tiles$ then becomes
\begin{equation}
\label{eq: line 1}
\frac x a + \frac y b = \tiles + 1,
\end{equation}
and $(i-2)^2 a^2 + (j-2)^2 b^2$ is expressed as $(x-2a)^2+(y-2b)^2$. The point minimizing this quadratic function along the line \eqref{eq: line 1} is the intersection of the latter with the perpendicular line passing through $(2a,2b)$,
\begin{equation}
\label{eq: line 2}
y = \frac {a (x-2a)} b + 2b.
\end{equation}
An example with $a=1.35$, $b=1$, $\tiles=7$ is shown in Figure~\ref{fig: min suff set, form}, where \eqref{eq: line 2} is depicted as a solid line. Solving the system of equations \eqref{eq: line 1} and \eqref{eq: line 2} gives
\begin{align}
\frac x a - 2 &= \frac{(\tiles-3)b^2}{a^2+b^2}, \\
\frac y b - 2 &= \frac{(\tiles-3)a^2}{a^2+b^2}.
\end{align}
In terms of the real-valued variables $i$, $j$, the solution $(\isolr, \jsolr)$ is thus
\begin{align}
\label{eq: prop: min suff set, form: isolr}
\isolr &= \frac{(\tiles-3)b^2}{a^2+b^2} + 2, \\
\label{eq: prop: min suff set, form: jsolr}
\jsolr &= \frac{(\tiles-3)a^2}{a^2+b^2} + 2.
\end{align}
The corresponding point $(\isolr a, \jsolr b)$ is shown in Figure~\ref{fig: min suff set, form} with a square marker.
\begin{figure}%
\centering%
\includegraphics[width=.7\textwidth]{mss}%
\caption{Obtaining $(\isolr, \jsolr)$ and $(i_\tiles, j_\tiles)$ in Proposition~\ref{prop: min suff set, form}. Example with $a=1.35$, $b=1$, $\tiles=7$%
}%
\label{fig: min suff set, form}%
\end{figure}%
The variables $i$, $j$ are actually limited to integer values. The pair $(i_\tiles, j_\tiles) \in \mathbb Z^2$ that minimizes $(i-2)^2 a^2 + (j-2)^2 b^2$ along the line $i+j-1=\tiles$ is either $(\lceil \isolr \rceil, \lfloor \jsolr \rfloor)$ or $(\lfloor \isolr \rfloor, \lceil \jsolr \rceil)$, whichever gives $(i_\tiles a, j_\tiles b)$ closest to $(\isolr a, \jsolr b)$, as illustrated in Figure~\ref{fig: min suff set, form}. In case of a tie the first of the two options is (arbitrarily) chosen. This can be expressed as
\begin{align}
\label{eq: prop: min suff set, form: isoli}
i_\tiles &= \left\lfloor \isolr + \frac 1 2 \right\rfloor, \\
\label{eq: prop: min suff set, form: jsoli}
j_\tiles &= \left\lceil \jsolr - \frac 1 2 \right\rceil,
\end{align}
which corresponds to rounding $\isolr$ and $\jsolr$ to the closest integers, with ties resolved in opposite directions. Combining \eqref{eq: prop: min suff set, form: isolr}--\eqref{eq: prop: min suff set, form: jsoli}
% *!*
yields \eqref{eq: min suff set, form, i} and \eqref{eq: min suff set, form, j}.
From \eqref{eq: prop: min suff set, form: isolr} and \eqref{eq: prop: min suff set, form: isoli},
\begin{equation}
\label{eq: prop: min suff set, form: isoli ineq}
\frac{(\tiles-3)b^2}{a^2+b^2} + \frac 3 2 < i_\tiles \leq \frac{(\tiles-3)b^2}{a^2+b^2} + \frac 5 2,
\end{equation}
and similarly, from \eqref{eq: prop: min suff set, form: jsolr} and \eqref{eq: prop: min suff set, form: jsoli},
\begin{equation}
\label{eq: prop: min suff set, form: jsoli ineq}
\frac{(\tiles-3)a^2}{a^2+b^2} + \frac 3 2 \leq j_\tiles < \frac{(\tiles-3)a^2}{a^2+b^2} + \frac 5 2.
\end{equation}
Considering the first inequality in \eqref{eq: prop: min suff set, form: isoli ineq} and the second in \eqref{eq: prop: min suff set, form: jsoli ineq} as equalities and eliminating $\tiles$ gives \eqref{eq: upper bound, line}. The pair $(i_\tiles, j_\tiles)$ is strictly below the line \eqref{eq: upper bound, line} because the used inequalities are strict. Similarly, \eqref{eq: lower bound, line} results from the second inequality in \eqref{eq: prop: min suff set, form: isoli ineq} and the first in \eqref{eq: prop: min suff set, form: jsoli ineq}, and the fact that those inequalities are not strict implies that the bound \eqref{eq: lower bound, line} can actually be attained.
\end{proof}
The bounding lines in Proposition~\ref{prop: min suff set, form} are shown in Figure~\ref{fig: pairs_bounds}, using three different pairs of grid parameters $a$, $b$ as examples. Given $(i_\tiles,j_\tiles) \in \mss$, the next pair $(i_{\tiles+1},j_{\tiles+1})$ is obtained by incrementing $j$ if that results in a point below \eqref{eq: upper bound, line}. Else $i$ is incremented instead, and the new pair is guaranteed to be above or on \eqref{eq: lower bound, line}.
\begin{figure}
\centering%
\subfigure[$a = 1$, $b = 1$]{%
\label{fig: pairs_bounds_1}%
\includegraphics[height=.23\textheight]{pairs_bounds_1}% *!*
}\hfill%
\subfigure[$a = 1.35$, $b = 1$]{%
\label{fig: pairs_bounds_1p35}%
\includegraphics[height=.23\textheight]{pairs_bounds_1p35}% *!*
}\hfill%
\subfigure[$a = \sqrt{2}$, $b = 1$]{%
\label{fig: pairs_bounds_sqrt2}%
\includegraphics[height=.23\textheight]{pairs_bounds_sqrt2}% *!*
}%
\caption{Set of optimal pairs $\mss$, and bounding lines
}%
\label{fig: pairs_bounds}%
\end{figure}%
For $a^2/b^2$ arbitrary, the number of pairs in $\mss$ with the same $i$, or with the same $j$, is in general irregular, because the lines \eqref{eq: upper bound, line} and \eqref{eq: lower bound, line} do not follow a ``natural'' direction of the grid. This happens for instance in Figure~\ref{fig: pairs_bounds_1p35}, where the number of pairs for each $i$ equals either $2$ or $3$ without a clear pattern.\footnote{Strictly, there is a periodic pattern whenever $a^2/b^2$ is rational, which is the case in Figure~\ref{fig: pairs_bounds_1p35}. However, the pattern is not easily discernible unless $a^2/b^2$ is a ratio of small numbers.}
On the other hand, a simple pattern arises when $a^2/b^2$ or $b^2/a^2$ is a natural number, as seen in Figures~\ref{fig: pairs_bounds_1} and \ref{fig: pairs_bounds_sqrt2}.
A segment whose discrete bounding rectangle has normalized width $i$ and height $j$ is oriented with approximate slope $jb/(ia)$ with respect to the $x$ axis (see Figure~\ref{fig: len ineq i j}); and this approximation becomes better for greater segment lengths. From \eqref{eq: upper bound, line} and \eqref{eq: lower bound, line} it can be seen that the pairs $(i,j) \in \mss$ have $j/i \approx a^2/b^2$ for large $i, j$. Therefore the optimal slope for long segments is approximately $a/b$. This substantiates the intuition that to maximize the number of visited tiles, the segment direction should strike a balance between achieving a small perceived ``length'' of the tile on one hand, and crossing both horizontal and vertical grid lines on the other hand.
\section{Deterministic segment: direct and inverse problems}
\label{part: max}
The direct and inverse problems defined in \S\ref{part: intro}, considering the segment position and orientation as deterministic, are addressed in this section. The general case for rectangular grids with real-valued segment lengths is analyzed first, in \S\ref{part: max: arbitrary grid, real lengths}. The square grid with real-valued segment lengths is addressed in \S\ref{part: max: unit square grid, real lengths}, as it allows a specialized formula for the direct problem. Lastly, the analysis of a unit square grid with integer-valued lengths is presented in \S\ref{part: max: unit square grid, integer lengths}.
\subsection{Arbitrary grid with real-valued lengths}
\label{part: max: arbitrary grid, real lengths}
Given a grid with parameters $a, b \in \mathbb R^+$, the maximum number $\tiles$ of visited tiles for an arbitrary real-valued length $\len$ can be represented by a function $\funt: \mathbb R^+ \to \mathbb N$ such that $\tiles = \funt(\len)$. Similarly, for the inverse problem a function $\funl: \mathbb N \to \mathbb R^+$ can be defined such that $\funl(\tiles)$ gives the infimum length of all segments that visit at least $\tiles$ tiles. Clearly, these two functions are related as
\begin{align}
\label{eq: funt funl}
\funt(\len) &= \max \{\tiles \in \mathbb N \st \funl(\tiles)<\len\},\\
\label{eq: funl funt}
\funl(\tiles) &= \inf\{\len \in \mathbb R^+ \st \funt(\len) \geq \tiles\}.
\end{align}
For arbitrary $a, b \in \mathbb R^+$, the functions $\funt$ and $\funl$ can be computed using an iterative procedure, which exploits the fact that the pairs $(i_3,j_3), (i_4,j_4), \ldots$ of the set $\mss$ are sorted by increasing $i+j-1$, and also by increasing $(i-2)^2 a^2 + (j-2)^2 b^2$. Namely, for $\funt$ the following method yields the solution: generate successive pairs to find the last one, $(i_\tiles,j_\tiles)$, that satisfies \eqref{eq: len > sqrt}; then $\funt(\len) = \tiles$. For $\funl$ the analogous method gives a direct formula. In addition, it is possible to obtain a direct formula also for $\funt$ using a different approach. These formulas are given in Theorems~\ref{theo: funt, form} and \ref{theo: funl, form}.
\begin{theorem}
\label{theo: funt, form}
For $a, b \in \mathbb R^+$, $a \geq b$ and $\len \in \mathbb R^+$,
\begin{equation}
\label{eq: theo: funt, form; funt}
\funt(\len) = \isoli+\jsoli-1
\end{equation}
with
\begin{align}
\label{eq: theo: funt, form; i}
\isoli &= \left\lceil \frac 3 2 + \frac b a \Re \sqrt{\frac{\len^2}{a^2+b^2}-\frac 1 4} \right\rceil, \\
\label{eq: theo: funt, form; j}
\jsoli &= \left\lceil 1 + \frac{\sqrt{\len^2-(\isoli-2)^2a^2}}{b} \right\rceil.
\end{align}
The function $\funt$ is piecewise constant and left-continuous, with unit-height jumps. A jump occurs at $\len$ if and only if $\len = \funl(\tiles)$ for some $\tiles \in \mathbb N$, $\tiles \geq 4$; and then $\funt(\len) = \tiles-1$, $\lim_{\delta \rightarrow 0+} \funt(\len+\delta) = \tiles$.
\end{theorem}
\begin{proof}
The approach is similar to that used in the proof of Proposition~\ref{prop: min suff set, form}. First, the intersection point $(\isolr a, \jsolr b)$, $\isolr, \jsolr \geq 2$ between the line defined by \eqref{eq: lower bound, line} and the arc centered at $(2a,2b)$ with radius $\len$ is computed, if it exists. Then, based on either the values $\isolr, \jsolr$ or the non-existence of the intersection point, a pair of integer values $(\isoli, \jsoli)$ is obtained that maximizes the $i+j-1$ sum that can be achieved with segments of length up to $\len$.
As will be seen, the obtained pair $(\isoli,\jsoli)$ may belong to the set $\mss$ defined by Proposition~\ref{prop: min suff set, form} or not. However, in either case $\funt(\len)$ is given by $\isoli+\jsoli-1$. The two possibilities are respectively illustrated in Figures~\ref{fig: ijLS_1p35_detail_in} and \ref{fig: ijLS_1p35_detail_notin} for $a=1.35$, $b=1$. In each case, the displayed arc is centered at $(2a,2b)$ and has radius $\len$. The inner region defined by the arc contains all $(ia,jb)$ points such that the pair $(i,j)$ is achievable according to Proposition~\ref{prop: len ineq i j}.\ref{prop: len ineq i j: ineq, exist}. As in previous figures, filled circles represent points $(ia,jb)$ such that $(i,j) \in \mss$. The solid line is the bound \eqref{eq: lower bound, line}. The intersection point $(\isolr a, \jsolr b)$ is displayed with a square marker.
\begin{figure}
\centering%
\subfigure[$\len = 3.1$: $(\isoli,\jsoli) \in \mss$]{%
\label{fig: ijLS_1p35_detail_in}%
\includegraphics[width=.7\textwidth]{ijLS_1p35_detail_in}%
}\\%\hfill%
\subfigure[$\len = 3.7$: $(\isoli,\jsoli) \notin \mss$]{%
\label{fig: ijLS_1p35_detail_notin}%
\includegraphics[width=.7\textwidth]{ijLS_1p35_detail_notin}%
}%
\caption{Obtaining $(\isoli,\jsoli)$ in Theorem~\ref{theo: funt, form}. Examples with $a=1.35$, $b=1$%
}%
\label{fig: ijLS_1p35_detail}%
\end{figure}%
The pair $(\isolr, \jsolr)$ results from solving the equation system
\begin{align}
\label{eq: icont jcont 1}
(\isolr-2)^2 a^2 + (\jsolr-2)^2 b^2 &= \len^2, \\
\label{eq: icont jcont 2}
\jsolr = \frac{\isolr a^2}{b^2} - \frac{5a^2}{2b^2} + \frac 3 2.
\end{align}
Expressing these equations in terms of new variables $\isolr-5/2$ and $\jsolr-3/2$, the solutions are easily found to be
\begin{align}
\label{eq: isolr sol}
\isolr &= \frac 5 2 \pm \frac b a \sqrt{\frac{\len^2}{a^2+b^2} - \frac 1 4}, \\
\label{eq: jsolr sol}
\jsolr &= \frac 3 2 \pm \frac a b \sqrt{\frac{\len^2}{a^2+b^2} - \frac 1 4},
\end{align}
where the same sign (positive or negative) should be used in the two equations. This can yield zero, one or two real-valued solution pairs $(\isolr, \jsolr)$, which respectively corresponds to the solid line in Figure~\ref{fig: ijLS_1p35_detail} being exterior, tangent or secant to the circle (the figure depicts the latter situation).
A solution pair $(\isolr, \jsolr)$ given by \eqref{eq: isolr sol} and \eqref{eq: jsolr sol} is meaningful only if it is real-valued with $\isolr, \jsolr \geq 2$. This solution, if it exists, is always associated with the positive sign in those expressions. Since $a \geq b$, it is easily seen that $\jsolr \geq 2$ implies $\isolr \geq 2$, and thus it suffices to check the former condition. Three cases need to be distinguished: there are no real-valued solution pairs $(\isolr, \jsolr)$; there are one or two but none of them has $\jsolr \geq 2$; or there are one or two and and one of them satisfies that inequality. These cases correspond to different ranges of $\len$, as seen next.
For $\len < \sqrt{a^2+b^2}/2$, \eqref{eq: isolr sol} and \eqref{eq: jsolr sol} are not real-valued. Since $a \geq b$, from the inequality $\len < \sqrt{a^2+b^2}/2$ it follows that $\len < a/\sqrt{2} < a$. This means that any achievable $(i,j)$ pair, if any, will have $i=2$. Thus in this case $\isoli$ should be set to $2$.
For $\sqrt{a^2+b^2}/2 \leq \len < (a^2+b^2) / (2a)$, \eqref{eq: isolr sol} and \eqref{eq: jsolr sol} give either two real-valued solutions or one real-valued double solution for $(\isolr, \jsolr)$, with $\jsolr < 2$. This in turn implies, according to \eqref{eq: icont jcont 2}, that $\isolr < (b^2/a^2+5)/2 \leq 3$ for $a \geq b$. Thus only pairs with $i=2$ are achievable again for $\len$ in this range, and therefore $\isoli$ must be $2$.
Lastly, for $\len \geq (a^2+b^2) / (2a)$ the expressions \eqref{eq: isolr sol} and \eqref{eq: jsolr sol} with positive sign give $\isolr, \jsolr \geq 2$, and $\isoli$ should be taken as the greatest integer less than $\isolr$, i.e.~$\lceil \isolr \rceil-1$.
The three cases are unified, as can be easily checked, by taking the real part of the positive-sign version of \eqref{eq: isolr sol} and computing $\isoli = \lceil \isolr \rceil-1$. This yields \eqref{eq: theo: funt, form; i}. Once $\isoli$ is known, \eqref{eq: theo: funt, form; j} computes $\jsoli$ as the greatest integer such that $(\isoli a,\jsoli b)$ is within the circle with center $(2a,2b)$ and radius $\len$. This ensures that $(\isoli,\jsoli)$ is achievable with lengths less than $\len$.
To see that $\funt(\len) = \isoli+\jsoli-1$, the two situations stated at the outset need to be considered separately. The first possibility is that $(\isoli,\jsoli) \in \mss$ (upper part of Figure~\ref{fig: ijLS_1p35_detail}). Then, by construction $(\isoli,\jsoli)$ maximizes $i+j-1$ among all achievable pairs of $\mss$, and is therefore optimal.
The second possibility is that $(\isoli,\jsoli) \notin \mss$ (Figure~\ref{fig: ijLS_1p35_detail_notin}). This happens when the pair from $\mss$ that has $i = \isoli$ ($(4,5)$ in the figure) is outside the circle, i.e.~it would require a length greater than $\len$. The selected $(\isoli,\jsoli)$ ($(4,4)$ in the figure), however, has the same $i+j-1$ sum as the pair from $\mss$ that ``should'' be used, which is $(\isoli-1,\jsoli+1)$ ($(3,5)$ in the figure); and therefore gives the same result. This is always the case, because $(\isoli,\jsoli+1) \in \mss$ (it is above or on the bounding line) and $(\isoli,\jsoli) \notin \mss$ (it is below the line), and due to how $\mss$ has been constructed, this implies that $(\isoli-1,\jsoli+1) \in \mss$ and $(\isoli-1,\jsoli+k) \notin \mss$ for $k =2, 3, \ldots$. It follows that $(\isoli,\jsoli)$ is achievable and maximizes $i+j-1$, and thus $\isoli+\jsoli-1$ is the desired solution.
Therefore, regardless of whether $(\isoli,\jsoli)$ is in $\mss$ or not, \eqref{eq: theo: funt, form; i} and \eqref{eq: theo: funt, form; j} give $\isoli+\jsoli-1$ equal to $\funt(\len)$. This establishes \eqref{eq: theo: funt, form; funt}.
Interestingly, for the specific case that $a^2/b^2 = 2k-1$, $k \in \mathbb N$ the lower bounding line \eqref{eq: lower bound, line} becomes $j = (2k-1)i - 5k + 4$, which gives integer $j$ for integer $i$. This means that for each $i$ there is a pair $(i,j) \in \mss$ that is on that line (see for example Figure~\ref{fig: pairs_bounds_1}), and the case $(\isoli,\jsoli) \notin \mss$ never occurs.
% ^ If this is needed outside of the proof, it can be included at the end of the theorem statement
As for the properties of $\funt$, it stems from \eqref{eq: theo: funt, form; funt}--\eqref{eq: theo: funt, form; j} that this function is piecewise constant and left-continuous. From the procedure described in the previous paragraphs for obtaining $(\isoli, \jsoli)$ it is clear that $\isoli+\jsoli-1$ increases in steps of $1$ when $\len$ is increased continuously; that is, $\funt$ has jumps of unit height.
Consider an arbitrary $\len$ such that for some $\tiles \in \mathbb N$, $\tiles \geq 4$
\begin{equation}
\label{eq: theo funt, form: prop 1}
\funl(\tiles)=\len.
\end{equation}
To see that $\funt$ has a jump at $\len$, assume for the sake of contradiction that $\funt$ is continuous at that point. Therefore $\funt$ is constant on an interval containing that point, which implies that $\funt(\len-\epsilon) = \tiles = \funt(\len+\epsilon)$ for some $\epsilon > 0$. This means that there exists a segment with length $\len-\epsilon$ that visits $\tiles$ tiles, and thus $\funl(\tiles) \leq \len-\epsilon < \len$, in contradiction with \eqref{eq: theo funt, form: prop 1}. Therefore $\funt$ is discontinuous (from the right) at $\len$. By definition of $\funl$, from \eqref{eq: theo funt, form: prop 1} it follows that
\begin{equation}
\label{eq: theo funt, form: prop 2}
\funt(\len) < \tiles
\end{equation}
and there exists $\epsilon > 0$ such that $\funt(\len+\delta) = \tiles$ for $0 < \delta < \epsilon$. This implies that
\begin{equation}
\label{eq: theo funt, form: prop 3}
\lim_{\delta \rightarrow 0+} \funt(\len+\delta) = \tiles,
\end{equation}
that is, $\funt$ has a jump at $\len$. In addition, since the jump has unit height, it stems from \eqref{eq: theo funt, form: prop 2} and \eqref{eq: theo funt, form: prop 3} that $\funt(\len) = \tiles-1$.
Conversely, assume that $\funt$ has a jump from $\tiles-1$ to $\tiles$ at some $\len \in \mathbb R^+$. This means that \eqref{eq: theo funt, form: prop 2} and \eqref{eq: theo funt, form: prop 3} hold for those $\tiles$ and $\len$. From \eqref{eq: theo funt, form: prop 2} it follows that $\funl(\tiles) \geq \len$. On the other hand, \eqref{eq: theo funt, form: prop 3} implies that $\funl(\tiles) \leq \len$. Thus $\funl(\tiles)=\len$.
\end{proof}
Although Theorem~\ref{theo: funt, form} requires $a \geq b$, the result could obviously be applied for $a < b$ by swapping the values of $a$ and $b$.
%In other words, \eqref{eq: theo: funt, form; funt}--\eqref{eq: theo: funt, form; j}
% *!*
%can be used for any grid if $a$, $b$ are interpreted as the largest and smallest sides of a tile, respectively.
\begin{theorem}
\label{theo: funl, form}
For $a, b \in \mathbb R^+$ and $\tiles \in \mathbb N$,
\begin{equation}
\label{eq: theo: funl, form; funl}
\funl(\tiles) = \sqrt{(\isoli-2)^2 a^2 + (\jsoli-2)^2 b^2}
\end{equation}
with
\begin{align}
\label{eq: theo: funl, form; isolr}
\isolr &= \max\left\{ \frac{(\tiles-3) b^2}{a^2+b^2}, 0 \right\} + 2, \\
\label{eq: theo: funl, form; jsolr}
\jsolr &= \max\left\{ \frac{(\tiles-3) a^2}{a^2+b^2}, 0 \right\} + 2, \\
\label{eq: theo: funl, form; isoli}
\isoli &= \left\lfloor \isolr + \frac 1 2 \right\rfloor, \\
\label{eq: theo: funl, form; jsoli}
\jsoli &= \left\lceil \jsolr - \frac 1 2 \right\rceil.
\end{align}
Equivalently, for $\tiles \geq 3$,
\begin{equation}
\label{eq: theo: funl, form; funl 2}
\funl(\tiles) = \sqrt{\frac{(\tiles-3)^2 a^2 b^2}{a^2+b^2} + \roundterm^2(a^2+b^2)}
\end{equation}
with
\begin{equation}
\label{eq: roundterm}
\roundterm = |\isoli - \isolr| = |\jsoli - \jsolr|.
\end{equation}
This function is monotone increasing for $\tiles \geq 3$.
\end{theorem}
\begin{proof}
The $(i_\tiles,j_\tiles)$ pair in set $\mss$ defined in Proposition~\ref{prop: min suff set, form} corresponds to at most $\tiles$ visited tiles. By construction of this set, any segment that visits $\tiles$ tiles must have length greater than $\sqrt{(i_\tiles-2)^2a^2 + (j_\tiles-2)^2b^2}$. For $\tiles \geq 3$ the variables $\isoli$, $\jsoli$ computed in \eqref{eq: theo: funl, form; isolr}--\eqref{eq: theo: funl, form; jsoli}
% -- *!*
coincide with $i_\tiles$, $j_\tiles$ as given by \eqref{eq: min suff set, form, i} and \eqref{eq: min suff set, form, j}, and therefore \eqref{eq: theo: funl, form; funl} gives the correct result. For $\tiles \in \{1, 2\}$ both \eqref{eq: theo: funl, form; isoli} and \eqref{eq: theo: funl, form; jsoli} equal $2$, and \eqref{eq: theo: funl, form; funl} gives $0$, which is again the correct result.
For $\tiles \geq 3$, the term $\sqrt{(i_\tiles-2)^2a^2 + (j_\tiles-2)^2b^2}$ can be interpreted geometrically as the distance between $(i_\tiles a, j_\tiles a)$ and $(2a, 2b)$. As can be seen with the help of Figure~\ref{fig: min suff set, form}, this distance is the hypotenuse of a right triangle whose other two sides extend from $(2a, 2b)$ to $(\isolr a, \jsolr b)$ and from $(\isolr a, \jsolr b)$ to $(i_\tiles a, j_\tiles a)$ respectively. Therefore,
\begin{equation}
\label{eq: distance as hypotenuse}
\begin{split}
(i_\tiles-2)^2a^2 + (j_\tiles-2)^2b^2 &= (\isolr-2)^2a^2 + (\jsolr-2)^2b^2 \\
&\quad + (i_\tiles-\isolr)^2a^2 + (j_\tiles-\jsolr)^2b^2.
\end{split}
\end{equation}
For $\tiles \geq 3$ it stems from \eqref{eq: theo: funl, form; isolr}
and \eqref{eq: theo: funl, form; jsolr} that
\begin{equation}
\label{eq: distance as hypotenuse, first leg}
(\isolr-2)^2a^2 + (\jsolr-2)^2b^2 =
\frac{(\tiles-3)^2 (a^2b^4+a^4b^2)}{(a^2+b^2)^2} =
\frac{(\tiles-3)^2 a^2b^2}{a^2+b^2}.
\end{equation}
The fact that both $(\isolr, \jsolr)$ and $(i_\tiles, j_\tiles)$ are on the line $i+j-1=\tiles$ implies that $\isolr+\jsolr = i_\tiles+ j_\tiles$. Taking into account that $i_\tiles=\isoli$ and $j_\tiles=\jsoli$, it stems that $\vert i_\tiles-\isolr \vert = \vert j_\tiles-\jsolr \vert = \roundterm$ with $\roundterm$ given by \eqref{eq: roundterm}. Consequently,
\begin{equation}
\label{eq: distance as hypotenuse, second leg}
(i_\tiles-\isolr)^2a^2 + (j_\tiles-\jsolr)^2b^2 = \roundterm^2 (a^2+b^2).
\end{equation}
Substituting \eqref{eq: distance as hypotenuse, first leg} and \eqref{eq: distance as hypotenuse, second leg} into \eqref{eq: distance as hypotenuse} and using \eqref{eq: theo: funl, form; funl} yields \eqref{eq: theo: funl, form; funl 2}.
The definition of $\funl$ implies that $\funl(\tiles) \geq \funl(\tiles-1)$ for any $\tiles \in \mathbb N$. On the other hand, by Theorem~\ref{theo: funt, form}, $\funt$ is piecewise constant and has a unit-height jump from $\tiles-1$ to $\tiles$ at $\funl(\tiles)$, $\tiles \in \mathbb N$, $\tiles \geq 4$. This implies that $\funl(\tiles) > \funl(\tiles-1)$ for $\tiles \geq 4$.
\end{proof}
The expression \eqref{eq: theo: funl, form; funl 2} allows a neat interpretation of $\funl(\tiles)$ (as stems from the arguments used in the proof of Theorem~\ref{theo: funl, form}). Namely, $\funl^2(\tiles)$ is the sum of the two terms that appear in that expression. The first term is the squared distance from $(2a, 2b)$ to the diagonal line defined by $i+j-1 = \tiles$; and the second term is additional squared distance incurred from rounding $i$, $j$ to integer values.
Theorems~\ref{theo: funt, form} and \ref{theo: funl, form} not only give the solutions $\funt(\len)$ and $\funl(\tiles)$ to the first two questions posed in \S\ref{part: intro}; they also provide a way to actually position a segment of length $\len$ or slightly greater than $\funl(\tiles)$, respectively, so that it visits $\funt(\len)$ or $\tiles$ tiles. Namely, for $\isoli$, $\jsoli$ computed as in the corresponding theorem, the segment should have its endpoints in the interiors of two tiles shifted $\isoli-1$ steps horizontally and $\jsoli-1$ steps vertically with respect to each other, with the exact position and orientation of the segment adjusted to avoid any grid points.
It is interesting to consider the following particular cases: $\len \gg a,b$; $a \gg b$; and $a=b$. Regarding the first, from \eqref{eq: theo: funt, form; funt}--\eqref{eq: theo: funt, form; j}
% *!*
and from \eqref{eq: theo: funl, form; funl}--\eqref{eq: theo: funl, form; jsoli}
% *!*
it is seen that for long segments the number of visited tiles and the segment length are approximately proportional, with
\begin{equation}
\label{eq: asympt slope}
\lim_{\len \rightarrow \infty} \frac{\funt(\len)}{\len}
= \lim_{\tiles \rightarrow \infty} \frac{\tiles}{\funl(\tiles)}
= \sqrt{1/a^2 + 1/b^2}.
\end{equation}
As for $a \gg b$, in this case the optimal discrete bounding rectangle has $\isoli = 2$, and $\jsoli$ as large as allowed by $\len$ (direct problem) or as required by $\tiles$ (inverse problem), corresponding to an almost vertical segment. In other words, for $a \gg b$ the length of the segment is best invested in increasing the number of tiles traversed vertically (but the segment should be slightly tilted to cross a vertical edge), and the asymptotic ratio \eqref{eq: asympt slope} is approximately $1/b$.
For $a=b$, either from symmetry considerations or particularizing the results in the above theorems it stems that the optimal orientation of the segment is close to $45^\circ$. This case will be dealt with in \S\ref{part: max: unit square grid, real lengths}, as it lends itself to simplified formulas.
Figure~\ref{fig: funt funl examples} shows the functions $\funt$ and $\funl$ for several pairs of grid parameters $a$, $b$. The graphs illustrate some of the observations of the previous paragraphs. Indeed, the asymptotic slope in Figure~\ref{fig: funt examples}, or the inverse of the asymptotic slope in Figure~\ref{fig: funl examples}, is approximately $\sqrt{2}$ for $a=b=1$; and it is roughly $1/b$ for the case $a=5,b=1$, or even for $a=5,b=1.5$ or $a=10,b=3$. Comparing the latter two cases it is also seen that scaling $a$, $b$ and $\len$ by the same factor does not alter $\funt(\len)$, and results in $\funl(\tiles)$ being scaled by that factor.
\begin{figure}
\centering%
\subfigure[Function $\funt$]{%
\label{fig: funt examples}%
\includegraphics[width=.49\textwidth]{funt_examples}%
}\hfill%
\subfigure[Function $\funl$]{%
\label{fig: funl examples}%
\includegraphics[width=.49\textwidth]{funl_examples}%
}%
\caption{Functions $\funt$ and $\funl$ for several pairs $a, b$
}%
\label{fig: funt funl examples}%
\end{figure}%
\subsection{Unit square grid with real-valued lengths}
\label{part: max: unit square grid, real lengths}
A square grid has $a=b$. For real-valued segment lengths it can be further assumed that $a=1$ (unit square grid). For $a \neq 1$ the expressions to be obtained are valid with $\len$ and $\funl(\tiles)$ replaced by $\len/a$ and $\funl(\tiles)/a$ respectively.
Particularizing the results in \S\ref{part: max: arbitrary grid, real lengths} to $a=b=1$ obviously yields simpler formulas.
\begin{corollary}
\label{cor: funt, sq, form}
For a unit square grid with $\len \in \mathbb R^+$,
\begin{equation}
\label{eq: cor: funt, sq, form}
\funt(\len) = \isoli+\jsoli-1
\end{equation}
with
\begin{align}
\label{eq: cor: funt, sq, form; i}
\isoli &= \left\lceil \frac 3 2 + \Re\sqrt{\frac{\len^2}{2} - \frac 1 4} \right\rceil, \\
\label{eq: cor: funt, sq, form; j}
\jsoli &= \left\lceil 1 + \sqrt{\len^2-(\isoli-2)^2} \right\rceil.
\end{align}
\end{corollary}
\begin{corollary}
\label{theo: funl, sq, form}
For a unit square grid, and for $\tiles \in \mathbb N$,
\begin{equation}
\funl(\tiles) = \begin{cases}
\displaystyle
0 & \text{for } \tiles =1, 2 \\[1.4mm] % *!*
\displaystyle
\frac{\tiles-3}{\sqrt{2}} & \text{for } \tiles \text{ odd, } \tiles \geq 3 \\[4.5mm] % *!*
\displaystyle
\frac{\sqrt{(\tiles-4)^2+(\tiles-2)^2}} {2} & \text{for } \tiles \text{ even, } \tiles \geq 4,
\end{cases}
\end{equation}
or equivalently
\begin{equation}
\label{eq: theo: funt, sq, form; funl}
\funl(\tiles) = \begin{cases}
\displaystyle
0 & \text{for } \tiles =1, 2 \\[1.4mm] % *!*
\displaystyle
\sqrt{\left\lceil \frac{(\tiles-3)^2} {2} \right\rceil} & \text{for } \tiles \geq 3.
\end{cases}
\end{equation}
\end{corollary}
% Theorem~\ref{theo: funl, form} for $a=b=1$ gives $\sqrt{\frac{(\tiles-3)^2+1} {2}}$. Instead, this corollary uses the equivalent expression $\frac{\sqrt{(\tiles-4)^2+(\tiles-2)^2}} {2}$, which is better for subsequent analysis.
Furthermore, an even simpler formula can be obtained for $\funt$, as the next theorem shows.
\begin{theorem}
\label{theo: funt, sq, form}
For a unit square grid with $\len \in \mathbb R^+$,
\begin{equation}
\label{eq: theo: funt, sq, form; funt, with i, j}
\funt(\len) = \isoli+\jsoli-1
\end{equation}
with
\begin{align}
\label{eq: theo: funt, sq, form; i}
\isoli &= \left\lceil \frac{\len}{\sqrt{2}} \right\rceil + 1, \\
\label{eq: theo: funt, sq, form; j}
\jsoli &= \left\lceil \sqrt{\len^2-(\isoli-2)^2} \right\rceil + 1,
\end{align}
which gives either $\jsoli = \isoli$ or $\jsoli = \isoli+1$. Equivalently,
\begin{equation}
\label{eq: theo: funt, sq, form; funt, simpler}
\funt(\len) = \left\lfloor \sqrt{2 \left\lceil \len^2 \right\rceil - 2} \right\rfloor + 3.
\end{equation}
\end{theorem}
\begin{proof}
The proof of \eqref{eq: theo: funt, sq, form; funt, with i, j}--\eqref{eq: theo: funt, sq, form; j}
% *!* --
uses a variation of the set $\mss$ defined in Proposition~\ref{prop: min suff set, form} that is more suited to this situation.
For $a=b=1$, the set $\mss$ consists of points of the form $(i,i)$ and $(i,i-1)$, as is easily seen from Proposition~\ref{prop: min suff set, form}, and as illustrated in Figure~\ref{fig: pairs_bounds_1}. By symmetry, replacing each point $(i,i-1)$ by $(i-1,i)$ gives a set $\mss'$ that is also optimal. For this new set, the lower bounding line \eqref{eq: lower bound, line} can be replaced by the simpler $j=i$. The same approach followed in the proof of Theorem~\ref{theo: funt, form} can be applied here, but using this line. Thus $(\isolr, \jsolr)$ is obtained from
\begin{align}
(\isolr-2)^2 + (\jsolr-2)^2 &= \len^2, \\
\jsolr &= \isolr,
\end{align}
which gives
\begin{equation}
\label{eq: theo: funt, sq, form; proof 1}
\isolr = \jsolr = \len/\sqrt{2} + 2.
\end{equation}
As in Theorem~\ref{theo: funt, form}, $\isoli$ is obtained as $\lceil \isolr \rceil-1$; and then $\jsoli$ is chosen as the largest integer such that $(\isoli, \jsoli)$ is achievable, i.e.~its distance from $(2, 2)$ is less than $\len$. The resulting $\isoli$ and $\jsoli$ are given by \eqref{eq: theo: funt, sq, form; i} and \eqref{eq: theo: funt, sq, form; j}.
The above procedure for choosing $\jsoli$ given $\isoli = \lceil \isolr \rceil-1$ always results in $\jsoli$ being either $\isoli$ or $\isoli+1$. This can be seen as follows. If $\jsoli = \isoli$, the point $(\isoli, \jsoli)$ is closer to $(2,2)$ than $(\isolr, \jsolr)$ is, and is therefore achievable. This implies that values of $\jsoli$ smaller than $\isoli$ will never be chosen. On the other hand, $\jsoli = \isoli+2$ or larger values are not achievable, because they would produce a sum $i+j-1$ greater than $\isolr+\jsolr$, which is impossible.
The preceding analysis shows that the pair $(\isoli,\jsoli)$ is in $\mss'$ and maximizes $i+j-1$. Therefore \eqref{eq: theo: funt, sq, form; funt, with i, j} holds.
To show \eqref{eq: theo: funt, sq, form; funt, simpler}, it is first noted that for $\tiles \geq 3$ Corollary~\ref{theo: funl, sq, form} gives
\begin{equation}
\funl(\tiles) = \sqrt{\left\lceil \frac{(\tiles-3)^2} {2} \right\rceil}.
\end{equation}
According to \eqref{eq: funt funl}, $\funt(\len)$ is obtained by the largest positive integer $\tiles$ such that
\begin{equation}
\label{eq: theo: funt, sq, form; proof 2}
\left\lceil \frac{(\tiles-3)^2} {2} \right\rceil < \len ^ 2.
\end{equation}
Since the left-hand side of \eqref{eq: theo: funt, sq, form; proof 2} is an integer, the condition of being strictly less than $\len^2$ is equivalent to
\begin{equation}
\left\lceil \frac{(\tiles-3)^2} {2} \right\rceil \leq \left\lceil \len^2 \right\rceil - 1,
\end{equation}
which in turn is the same as
\begin{equation}
\frac{(\tiles-3)^2} {2}\leq \left\lceil \len^2 \right\rceil - 1.
\end{equation}
Solving for $\tiles$ gives
\begin{equation}
\label{eq: theo: funt, sq, form; proof 3}
\tiles \leq \sqrt{2 \left\lceil \len^2 \right\rceil -2 } + 3.
\end{equation}
The desired quantity $\funt(\len)$, that is the largest positive integer $\tiles$ satisfying \eqref{eq: theo: funt, sq, form; proof 3}, is thus the right-hand side rounded down, as given by \eqref{eq: theo: funt, sq, form; funt, simpler}.
\end{proof}
From Theorem~\ref{theo: funt, sq, form} it stems that odd values of $\funt(\len)$ correspond to $\isoli = \jsoli$, whereas even values are achieved with $\jsoli = \isoli+1$. In addition, noting that $\funt(\len) = \tiles$ is equivalent to $\funl(\tiles) < \len \leq \funl(\tiles+1)$ and using Corollary~\ref{theo: funl, sq, form} the following characterization of $\funt$ is obtained. For $\tiles \geq 3$ with $\tiles$ odd, $\funt(\len) = \tiles$ if and only if
\begin{equation}
\label{eq: square, odd max tiles, lengths}
\len \in \left( \frac{\tiles-3}{\sqrt{2}}, \frac{ \sqrt{(\tiles-3)^2+(\tiles-1)^2}}{2} \right].
\end{equation}
For $\tiles \geq 4$, $\tiles$ even, $\funt(\len) = \tiles$ if and only if
\begin{equation}
\label{eq: square, even max tiles, lengths}
\len \in \left(\frac{ \sqrt{(\tiles-4)^2+(\tiles-2)^2}}{2}, \frac{\tiles-2}{\sqrt{2}} \right].
\end{equation}
\subsection{Unit square grid with integer lengths}
\label{part: max: unit square grid, integer lengths}
A natural variation of the direct and inverse problems introduced in \S\ref{part: intro} is to consider $a=b=1$ with the additional restriction that the segment length can only be a positive integer (equivalently, the square grid has spacing $a$ and the segment lengths are restricted to integer multiples of $a$).
The direct problem in this setting corresponds to the restriction of $\funt$ to $\mathbb N$. This will be denoted as a function $\funti: \mathbb N \to \mathbb N$ for greater clarity, although obviously $\funti(\leni) = \funt(\leni)$ for all $\leni \in \mathbb N$. The sequence $\funti(\leni)$, $\leni \in \mathbb N$ takes values $3, 5, 7, 8, 9, \ldots$, and is depicted in Figure~\ref{fig: funti}. This is A346232 in the On-Line Encyclopedia of Integer Sequences \cite{OEIS_unitsq_int_dir}. For this sequence, the expression \eqref{eq: theo: funt, sq, form; funt, simpler} in Theorem~\ref{theo: funt, sq, form} simplifies in the obvious way, and the following properties hold.
\begin{figure}%
\centering%
\subfigure[Sequence $\funti$]{%
\label{fig: funti}%
\includegraphics[width=.49\textwidth]{funti}%
}\hfill%
\subfigure[Sequence $\funli$]{%
\label{fig: funli}%
\includegraphics[width=.49\textwidth]{funli}%
}%
\caption{Sequences $\funti$ and $\funli$
}%
\label{fig: funti funli}%
\end{figure}%
\begin{theorem}
\label{theo: funti}
For $\leni \in \mathbb N$,
% $\funti(\leni)$ can be computed as
\begin{equation}
\label{eq: funti leni}
\funti(\leni) = \left\lfloor \sqrt{2\leni^2-2} \right\rfloor + 3.
\end{equation}
In addition,
\begin{enumerate}
\item
This sequence is increasing, with $\funti(\leni+1)-\funti(\leni) \in \{1, 2\}$.
\item
There can be no more than $2$ consecutive increments equal to $1$.
\item
Increments equal to $2$ always appear isolated, except at the initial sequence terms $3, 5, 7$.
\end{enumerate}
\end{theorem}
\begin{proof}
The equality \eqref{eq: funti leni} stems from \eqref{eq: theo: funt, sq, form; funt, simpler} noting that $\len$ is an integer.
In order to prove that $\funti(\leni+1)-\funti(\leni) \in \{1,2\}$, consider the function $\genfun(\genvar) = \sqrt{2\genvar^2-2}$ for $\genvar \in \mathbb R$, $\genvar>1$. Its first derivative is
\begin{equation}
\genfun'(\genvar) = \frac {\sqrt{2} \, \genvar} {\sqrt{\genvar^2-1}},
\end{equation}
and its second derivative is easily seen to be negative. Therefore $\genfun'(\genvar)$ can be bounded for $\genvar > 3$ as
\begin{equation}
\label{eq: der bounds}
\lim_{\genvar \rightarrow \infty} \genfun'(\genvar) = \sqrt{2} < \genfun'(\genvar) < \genfun'(3) = 3/2.
\end{equation}
For $\leni \geq 3$,
% I think the above this could be done for $\leni > 2$, replacing $3/2$ in \eqref{eq: der bounds} by $s\sqrt{2/3}$, which is also less than $2$. Then, only the case $\funti(2)-\funti(1) = 2$ would have to be checked separately.
by the mean value theorem \cite[section~5.3]{Abbott15}, when $\leni$ is increased to $\leni+1$ the term $\sqrt{2\leni^2-2}$ in \eqref{eq: funti leni} has an increment that equals $\genfun'(\genvar)$ for some $\leni < \genvar < \leni+1$. Therefore
\begin{equation}
\label{eq: der bounds 2}
\sqrt{2} < \sqrt{2(\leni+1)^2-2} - \sqrt{2\leni^2-2} < 3/2.
\end{equation}
Since $1 < \sqrt{2}$ and $3/2 < 2$, \eqref{eq: der bounds 2} implies that $\funti(\leni+1)-\funti(\leni)$ can only take the values $1$ or $2$ for $\leni \geq 3$. In addition, $\funti(2)-\funti(1) = \funti(3)-\funti(2) = 2$, and thus the result holds for all $\leni \in \mathbb N$.
Using the first bound in \eqref{eq: der bounds 2} three times,
\begin{equation}
3\sqrt{2} < \sqrt{2(\leni+3)^2-2} - \sqrt{2\leni^2-2}.
\end{equation}
Considering that $4 < 3\sqrt{2}$, this implies that $\funti(\leni+3)-\funti(\leni) \geq 4$ for $\leni \geq 3$. Therefore at least one of the three increments from $\funti(\leni)$ to $\funti(\leni+3)$ is $2$. Since $\funti(2)-\funti(1) = \funti(3)-\funti(2) = 2$, this result holds for all $\leni \in \mathbb N$.
Similarly, using the second bound in \eqref{eq: der bounds 2} twice,
\begin{equation}
\sqrt{2(\leni+2)^2-2} - \sqrt{2\leni^2-2} < 3,
\end{equation}
which implies that $\funti(\leni+2)-\funti(\leni) \leq 3$ for $\leni \geq 3$. Therefore the two increments $\funti(\leni+1)-\funti(\leni)$ and $\funti(\leni+2)-\funti(\leni+1)$ cannot both be $2$ for $\leni \geq 3$.
\end{proof}
The inverse problem with integer-length segments can be formulated as follows: given $\tiles \in \mathbb N$, find the \emph{minimum} integer length that allows visiting at least $\tiles$ tiles. Observe that in this case, unlike with real-valued lengths, there is indeed a minimum length, as every subset of $\mathbb N$ has a minimum. This can be expressed as a function $\funli: \mathbb N \to \mathbb N$:
\begin{equation}
\label{eq: funli funti}
\funli(\tiles) = \min\{\leni \in \mathbb N \st \funti(\leni) \geq \tiles\},
\end{equation}
which is related to the function $\funl$ for real-valued lengths by
\begin{equation}
\label{eq: funli funl}
\funli(\tiles) = \lfloor\funl(\tiles)\rfloor + 1.
\end{equation}
The converse to \eqref{eq: funli funti} is (compare to \eqref{eq: funt funl}):
\begin{equation}
\label{eq: funti funli}
\funti(\leni) = \max \{\tiles \in \mathbb N \st \funli(\tiles) \leq \leni\}.
\end{equation}
In view of \eqref{eq: funli funti} and \eqref{eq: funti funli}, $\funti$ and $\funli$ can be considered as ``pseudo-inverse'' sequences of each other.
The sequence $\funli(\tiles)$, $\tiles \in \mathbb N$ can be computed using \eqref{eq: theo: funt, sq, form; funl} and \eqref{eq: funli funl}. It has initial values $1, 1, 1, 2, 2, 3, 3, 4, 5 \ldots$, as seen in Figure~\ref{fig: funli}. This is A346693 in the On-Line Encyclopedia of Integer Sequences \cite{OEIS_unitsq_int_inv}. Moreover, a slightly simpler expression can be obtained from \eqref{eq: funti leni} and \eqref{eq: funli funti}. This is established by the next theorem, which also states some properties of $\funli$, parallel to those of $\funti$.
\begin{theorem}
\label{theo: funli}
For $\tiles \in \mathbb N$,
% $\funli(\tiles)$ can be computed as
\begin{equation}
\label{eq: funli}
\funli(\tiles) = \begin{cases}
\displaystyle
1 & \text{for } \tiles \leq 3 \\[1.3mm] % *!*
\displaystyle
\left \lceil \sqrt{\frac{(\tiles-3)^2} 2 + 1} \ \right \rceil & \text{for } \tiles \geq 4.
\end{cases}
\end{equation}
In addition,
\begin{enumerate}
\item
This sequence is non-decreasing. Except for the initial run of $3$ equal values, it is formed by runs of $1$ or $2$ equal values, with an increment of $1$ between consecutive runs.
\item
There can be no more than $3$ consecutive terms that are different.
\item
A run of $2$ equal values always has $2$ different terms before and $2$ different terms after the run, except for the initial terms $1, 1, 1, 2, 2, 3, 3$.
\end{enumerate}
\end{theorem}
\begin{proof}
Using \eqref{eq: funti leni}, the inequality $\funti(\leni) \geq \tiles$ in \eqref{eq: funli funti} is written as
\begin{equation}
\left\lfloor \sqrt{2\leni^2-2} \right\rfloor + 3 \geq \tiles.
\end{equation}
Since the right-hand side is an integer, this is equivalent to
\begin{equation}
\label{eq: theo funli ineq}
\sqrt{2\leni^2-2} \geq \tiles-3.
\end{equation}
Assuming $\tiles \geq 4$, taking squares and rearranging gives
\begin{equation}
\leni \geq \sqrt{\frac{(\tiles-3)^2} 2 + 1},
\end{equation}
which combined with \eqref{eq: funli funti} yields the second part of \eqref{eq: funli}. The first part results from noting that for $\tiles \leq 3$ the value $\leni=1$ satisfies \eqref{eq: theo funli ineq}.
The stated properties for $\funli$ follow directly from those of $\funti$ established by Theorem~\ref{theo: funti}.
\end{proof}
\section{Random segment: probabilistic characterization}
\label{part: rand}
Given $\len \in \mathbb R^+$, consider a segment of length $\len$ with uniformly random position and orientation. Specifically, the coordinates $x_1, y_1$ of the first endpoint are independent random variables uniformly distributed on $[0,a)$ and $[0,b)$ respectively, where $a$, $b$ are the grid parameters. The orientation $\theta$ of the segment is uniformly distributed on $[0,2\pi)$. The variables $x_1$, $y_1$ and $\theta$ determine the coordinates $x_2, y_2$ of the second endpoint.
Each realization of the random segment gives rise to a discrete bounding rectangle, whose normalized dimensions $i$ and $j$ are thus random variables, as is the number $\tiles$ of visited tiles. Except for a set of realizations with probability $0$, $i$ and $j$ are at least $1$, and $\tiles = i+j-1$. Note that $i$ and $j$ are not statistically independent.
This section deals with the two problems stated in \S\ref{part: intro} for random segments, namely obtaining the average number of visited tiles and the probability of visiting the maximum possible number of tiles. Segment lengths will be assumed to be real-valued. The results to be obtained are directly applicable for integer lengths as a particular case.
Arbitrary grids are considered in \S\ref{part: probmax: arbitrary grid, real lengths}. The main result is the average number of visited tiles. The square grid is addressed in \S\ref{part: probmax: unit square grid, real lengths}. This more specialized setting allows computation of the probability of visiting the maximum number of tiles, which would be difficult in the general case.
\subsection{Arbitrary grid with real-valued lengths}
\label{part: probmax: arbitrary grid, real lengths}
Let $\funta: \mathbb R^+ \to \mathbb R^+$ be defined such that $\funta(\len)$ is the average number of tiles visited by a random segment of length $\len$, with the distributions specified in the preceding.
\begin{theorem}
\label{theo: funta, form}
Given $a, b, \len \in \mathbb R^+$, consider a grid with parameters $a, b$ and a uniformly random segment of length $\len$, as defined above. The average number of tiles visited by the segment is
\begin{equation}
\label{eq: funta, form}
\funta(\len) = \frac{2\len}{\pi}\left(\frac 1 a + \frac 1 b\right) + 1.
\end{equation}
\end{theorem}
\begin{proof}
Suppose first that the grid is simplified to only vertical lines with spacing $a$. This matches the set-up of Buffon's original needle problem \cite[section~1.1]{Mathai99}, except that here the length $\len$ of the needle may exceed the spacing $a$, allowing it to cross multiple lines. As shown in \cite{Ramaley69}, the expected number of lines crossed equals $2\len/(\pi a)$.
% When $\len<a$ this expectation matches the probability of the needle crossing a grid line, since it cannot cross more than one.
Consider again a grid with horizontal spacing $a$ and vertical spacing $b$. The grid crossings decompose into crossings of horizontal and vertical grid lines. By linearity of expectation, the expected number of crossings is the sum of the expectations for parallel lines with spacing $a$ and $b$ respectively, which gives
\[
\frac{2\len}{\pi}\left(\frac 1 a + \frac 1 b\right).
\]
As noted in Proposition~\ref{prop: i+j-1}, the number of tiles visited by a segment is the count of its grid line crossings plus $1$, unless it exactly passes through any grid points, but that occurs with probability $0$ and therefore does not affect the expected value. Thus $\funta(\len)$ is obtained by adding $1$ to the above expression, which gives \eqref{eq: funta, form}.
\end{proof}
In view of Theorem~\ref{theo: funta, form}, the average number of visited tiles as a function of the segment length has a very simple form, namely an affine function. Conversely, for any $\tiles>1$ it is immediate to compute the length of a random segment that visits $\tiles$ tiles on average, given as $\funta^{-1}(\tiles)$.
In spite of the dependence between the random variables $i$ and $j$, their marginal distributions have relatively simple analytic expressions, as established by the next proposition.
For $0 \leq \genvar \leq 1$, let
\begin{equation}
\label{eq: f}
f(\genvar) = \int_0^\genvar \arccos x \, \diff x = \genvar \arccos \genvar - \sqrt{1-\genvar^2} + 1.
\end{equation}
\begin{proposition}
\label{prop: Pr i}
Given $a, b, \len \in \mathbb R^+$, consider a grid with parameters $a, b$ and a uniformly random segment of length $\len$, as defined above. Let the random variables $i$, $j$ represent the normalized dimensions of the discrete bounding rectangle. For $n \in \mathbb N$,
\begin{equation}
\label{eq: Pr i geq n}
\Pr[i \geq n] = \begin{cases}
\displaystyle
1 &\text{if\ \ } \displaystyle n =1 \\[1 mm] % *!*
\displaystyle
\frac{2\len}{\pi a} \left(f\left(\frac{a(n-1)}{\len}\right)-f\left(\frac{a(n-2)}{\len} \right)\right) &\text{if\ \ } \displaystyle 2 \leq n < \frac \len a + 1 \\[4 mm] % *!*
\displaystyle
\frac{2\len}{\pi a}\,\, \left(1 - f\left(\frac{a(n-2)}{\len}\right)\right) &\text{if\ \ } \displaystyle \frac\len a + 1 \leq n < \frac\len a+2 \\[3 mm] % *!*
\displaystyle
0 &\text{if\ \ } \displaystyle \frac \len a + 2 \leq n;
\end{cases}
\end{equation}
and $\Pr[j \geq n]$ is given by the same expressions with $a$ replaced by $b$.
\end{proposition}
\begin{proof}
Clearly, $\Pr[i \geq 1]=1$. In the following it will be assumed that $n \geq 2$. The basic idea is to compute $\Pr[i \geq n]$ conditioned on $(x_1,y_1)$ (or, as will be seen, only on $x_1$), and then to average over $x_1$ and $y_1$ (actually only over $x_1$).
Given the coordinates $(x_1, y_1)$ of the first endpoint of the segment, with $0 \leq x_1 < a$, $0 \leq y_1 < b$, the second endpoint $(x_2, y_2)$ lies on a circle with radius $\len$ centered at $(x_1,y_1)$, as shown in Figure~\ref{fig: Pr_i}. The segment orientation is a random angle $\theta$ uniformly distributed on $[0,2\pi)$. It is clear from the figure that $i \geq n$ if and only if $x_2 > a(n-1)$ or $x_2 < -a(n-2)$; and for $n \geq 2$ these events are exclusive. Thus
\begin{equation}
\label{eq: Pr i cond}
\Pr[i \geq n \cond x_1, y_1] = \Pr[x_2 > a(n-1) \cond x_1, y_1] + \Pr[x_2 < -a(n-2) \cond x_1, y_1].
\end{equation}
The two conditional probabilities on the right-hand side of \eqref{eq: Pr i cond} are different in general. However, averaging over $x_1, y_1$ gives, by symmetry, $\Pr[x_2 > a(n-1)] = \Pr[x_2 < -a(n-2)]$. In addition, the coordinate $y_1$ does not have any influence on these events, and therefore conditioning on $x_1, y_1$ is the same as conditioning on $x_1$. This implies that, for $n \geq 2$,
\begin{equation}
\label{eq: Pr i = 2 Pr}
\Pr[i \geq n] = 2 \Pr[x_2 > a(n-1)].
\end{equation}
\begin{figure}
\centering%
\subfigure[$x_2$ can exceed $a(n-1)$ for all $x_1$, $0 \leq x_1 \leq a$]{%
\label{fig: Pr_i_full}%
\includegraphics[width=.58\textwidth]{Pr_i_full_1p35}%
}\\%\hfill%
\subfigure[$x_2$ can exceed $a(n-1)$ only if $a(n-1)-\len \leq x_1 \leq a$]{%
\label{fig: Pr_i_notfull}%
\includegraphics[width=.58\textwidth]{Pr_i_notfull_1p35}%
}%
\caption{Conditions for the normalized width of the discrete bounding rectangle, $i$, to be equal or greater than a given $n$. Example with $a=1.35$, $b=1$, $n=3$
}%
\label{fig: Pr_i}
\end{figure}%
Consider the event $x_2 > a(n-1)$ conditioned on $x_1$, with $n \geq 2$. There are three possibilities depending on $x_1$, $n$ and $\len$. If $a(n-1) < \len$, regardless of $x_1$ the length $\len$ is enough for $x_2$ to exceed $a(n-1)$ for some angles $\theta$. This is depicted in Figure~\ref{fig: Pr_i_full}, where the section of the arc with solid line represents, for a given $x_1$, those angles for which $x_2 > a(n-1)$. If $a(n-2) < \len \leq a(n-1)$, the length will be enough provided that $x_1 > a(n-1)-\len$, and then only for certain angles. This restriction on $x_1$ corresponds to the shaded region in Figure~\ref{fig: Pr_i_notfull}. Lastly, if $\len \leq a(n-2)$ it is not possible for $x_2$ to exceed $a(n-1)$, regardless of $x_1$ or $\theta$. The figure makes it clear that the coordinate $y_1$ is irrelevant to this.
In the first two cases above, the probability that $x_2 > a(n-1)$, conditioned on $x_1$, is the length of the arc to the right of the line $x=a(n-1)$ divided by $2\pi\len$, that is,
\begin{equation}
\label{eq: Pr x_2 > a(n-1) cond}
\Pr[x_2 > a(n-1) \cond x_1] = \frac 1 \pi \arccos \frac{a(n-1)-x_1}{\len}.
\end{equation}
In the first case $x_1$ has a uniform distribution on $[0,a)$, and $\Pr[x_2 > a(n-1)]$ is easily obtained from \eqref{eq: Pr x_2 > a(n-1) cond} as
\begin{equation}
\label{eq: Pr x_2 > a(n-1), first case}
\begin{split}
\Pr[x_2 > a(n-1)] &= \E[\Pr[x_2 > a(n-1) \cond x_1]] \\
&= \frac 1 {\pi a} \int_0^a\arccos \frac{a(n-1)-x_1}{\len} \, \diff x_1 \\
&= \frac{\len}{\pi a}\,\, \left( f\left( \frac{a(n-1)}{\len} \right) - f\left( \frac{a(n-2)}{\len} \right) \right),
\end{split}
\end{equation}
where the function $f$ is defined in \eqref{eq: f}. Substituting into \eqref{eq: Pr i = 2 Pr} yields the result in \eqref{eq: Pr i geq n}, second line.
The second case is similar, but the integration over $x_1$ is from $a(n-1)-\len$ to $a$. Noting that $f(1)=1$, this gives
\begin{equation}
\label{eq: Pr x_2 > a(n-1), second case}
\Pr[x_2 > a(n-1)] = \frac{\len}{\pi a}\,\, \left( 1 - f\left( \frac{a(n-2)}{\len} \right) \right),
\end{equation}
which combined with \eqref{eq: Pr i = 2 Pr} yields the expression in \eqref{eq: Pr i geq n}, third line.