In this code golf challenge, you'll be computing the placement of (open) circles of areas \$\pi, 2\pi, 3\pi, \dots\$ when greedily placed along integer points in a square spiral in such a way that no two overlap.
A square spiral is a sequence in \$\mathbb Z^2\$ that starts at \$(0,0)\$ and successively wraps around the origin. (Like Arnauld, you might find inspiration from this CGSE question.)
(0, 0) -> (1, 0) -> (1, 1) -> (0, 1) -> (-1, 1) -> (-1, 0) -> (-1, -1) -> (0, -1) -> (1, -1) -> (2, -1) -> (2, 0) -> (2, 1) -> (2, 2) -> (1, 2) -> (0, 2) -> (-1, 2) -> (-2, 2) -> (-2, 1) -> (-2, 0) -> (-2, -1) -> (-2, -2) -> (-1, -2) -> (0, -2) -> (1, -2) -> (2, -2) -> (3, -2) -> (3, -1) -> (3, 0) -> (3, 1) -> (3, 2) -> ...
In this challenge, you will start at the origin, walk around the spiral, and place circles with areas \$\pi, 2\pi, 3\pi\$, at integer points in a greedy fashion, as shown in the GIF below.
The first seven circles are:
- a circle of area \$\pi\$ placed at \$(0,0)\$,
- a circle of area \$2\pi\$ placed at \$(2,2)\$,
- a circle of area \$3\pi\$ placed at \$(-2,2)\$,
- a circle of area \$4\pi\$ placed at \$(3,-2)\$,
- a circle of area \$5\pi\$ placed at \$(-3,-3)\$,
- a circle of area \$6\pi\$ placed at \$(5,5)\$, and
- a circle of area \$7\pi\$ placed at \$(0,6)\$.
Again, the circles are placed as soon as they fit. The second circle could not be placed at \$(1,0)\$, \$(1,1)\$, \$(0,1)\$, \$(-1,1)\$, \$(-1,0)\$, \$(-1, -1)\$, \$(0,-1)\$, \$(1, -1)\$, \$(2, -1)\$, \$(2, 0)\$, or \$(2, 1)\$ without overlapping with the first circle, but it could be—and is—placed at \$(2,2)\$.
Once we've placed "all" of the blue circles, we start over. In the second generation, we place "red" circles with areas \$\pi, 2\pi, 3\pi\$, in a greedy fashion:
Once we've placed all of the red circles from the second generation, we move onto yellow circles, cyan circles, magenta, dark green, purple, light green, orange, brown circles, and so on.
Challenge
Okay! That's a lot of setup! Here's your code-golf challenge:
Write a program that takes in two positive (alternatively, \$0\$-indexed) integers, g
and k
, and outputs the position of the circle of area \$k\pi\$ in the \$g\$-th generation.
When \$g=1\$, this corresponds to the blue circles; when \$g=2\$, this corresponds to the red circles; when \$g=3\$, the yellow circles, and so on.
Your program should be able to compute all of the values in the following table in practice, and it should be able to compute bigger values (without running into floating point errors) in principle.
Data table
g | k | position | (color in GIFs; for reference only)
---+----+------------+------------------------------------
1 | 1 | (0, 0) | blue
1 | 2 | (2, 2) | blue
1 | 3 | (-2, 2) | blue
1 | 50 | (36, -18) | blue
2 | 1 | (0, -4) | red
2 | 2 | (-3, 9) | red
2 | 14 | (-2, -59) | red
2 | 15 | (-66, 28) | red
3 | 1 | (-5, 2) | yellow
3 | 2 | (4, -10) | yellow
3 | 10 | (-58, 3) | yellow
8 | 1 | (-6, 10) | orange
8 | 2 | (16, 21) | orange
8 | 3 | (-27, -19) | orange
9 | 1 | (-10, 6) | crimson
9 | 2 | (-25, 4) | crimson
9 | 3 | (17, -27) | crimson
Input: RegionIntersection[Circle[{6,-6}, 3],Circle[{3,-2},2] Output: Point[{21/5,-(18/5)}]
So the question arose: should tangent circles be considered an overlap? Looks like all answers here suppose "not" and include (6,-6). \$\endgroup\$