
MATHEMATICA

(* This gives a polar function of a ksides regular polygon with side length "side" *)
PolarPolygonSide[sidelength_, theta_,
k_] := ((sidelength/2)/Tan[Pi/k])/
Cos[Mod[theta  Pi/k, 2 Pi/k]  Pi/k];
(* {x, y}: coordinate of the test point,
phase: angle offset,
sides: number of sides *)
TruePointInsidePhase[x_, y_, sidelength_, phase_, sides_] :=
Module[{theta}, theta = ArcTan[x, y] + phase;
If[x^2 + y^2 == 0, 1,
If[x^2 + y^2  (PolarPolygonSide[sidelength, theta, sides]^2) < 0, 1, 0]] // Return];
sides = 4; dstep = 0.025; phasestep = 2 Pi/300;
epsilon = 2Pi*10^6; (* small initial angle to avoid a perfectly aligned square *)
seq = {};
Do[npoints = {}; k = 0;
Do[Do[Do[
Do[Do[k =
k + TruePointInsidePhase[i + di, j + dj, sidelength, phase, sides],
{i, sidelength  1, sidelength + 1, 1}],
{j, sidelength  1, sidelength + 1, 1}];
AppendTo[npoints, k]; k = 0; ,
{dj, 0, 1/2, dstep}],
{di, 0, 1/2, dstep}],
{phase, epsilon, 2 Pi/sides, phasestep}] // Quiet;
temp = npoints // Min;
AppendTo[seq, temp]; Print[seq // Last], {sidelength, 0, 10, 1}]
Print[seq]
(*
(*This gives the number of points strictly inside a polygone given by
function "PolarPolygonSide" of "sides" sides, side length: "sidelength", centered in (di, dj) and rotated by "phasestep" radians wrt to initial orientation: *)
FaeDensityPlot[sides_, sidelength_, di_, dj_, phasestep_] :=
Module[{npoints = {}, kamin = {}, k = 0},
Quiet[Do[Do[
Do[k = k +
TruePointInsidePhase[i + di, j + dj, sidelength, phase,
sides], {i, sidelength  1, sidelength + 1,
1}], {j, sidelength  1, sidelength + 1, 1}];
AppendTo[npoints, k];
k = 0; , {phase, 0, (2 \[Pi])/sides, phasestep}]];
Return[Min[npoints]]]
(*This plots the regions for the centers of squares of side "sidelength" with constant minimum numbers of lattice points inside*)
sidelength=6;
DensityPlot[FaeDensityPlot[4, sidelength, x, y, (2 \[Pi])/300] , {x, 0, 1/2}, {y, 0, 1/2}, PlotPoints > 100, PlotRange > All, ColorFunction > "DeepSeaColors", MaxRecursion > 3, PerformanceGoal > "Quality", PlotTheme > "Detailed", PlotLegends > Automatic]
*)
