8
\$\begingroup\$

Challenge

Given an integer radius \$r \geq 0\$, write a program that prints each set of cartesian coordinates in a counter-clockwise spiral originating at \$(0, 0)\$ as shown here (for \$r = 3\$), with indices:

Input

The program should take \$r\$, a non-negative integer, as an input. The input is assumed to be valid and in the range \$2^{32} \geq r \geq 0\$.

Output

The output of the program should be a comma-space-delimited list of integer coordinates in a counter-clockwise circle, starting with \$(0, 0)\$ out to a radius of \$r\$.

Test Cases

r = 0 -> No output

r = 1 ->

(0, 0)

r = 3 (shown in diagram) ->

(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, 0), (-2, -1), (-2, -2), (-1, -2), (0, -2), (1, -2), (2, -2)

This is code-golf, so the shortest answer in bytes, per language, wins.

\$\endgroup\$
5
  • 5
    \$\begingroup\$ Welcome to Code Golf! This is a very well written first challenge. However, I'm afraid it might be a dupe of this one. The only notable difference is that the other challenge asks for one specific element. \$\endgroup\$
    – Arnauld
    Commented Aug 24, 2020 at 16:38
  • 2
    \$\begingroup\$ @Arnauld ah, I didn't see that post when I searched for this one. Do you think this one is sufficiently different? \$\endgroup\$
    – Brenden
    Commented Aug 24, 2020 at 16:46
  • 2
    \$\begingroup\$ IMO, it's not different enough. But I won't VTC as dupe because my vote is a hammer. Let's see what others think. \$\endgroup\$
    – Arnauld
    Commented Aug 24, 2020 at 16:52
  • \$\begingroup\$ So.... we are keeping this question open because our votes are hammers? \$\endgroup\$
    – ZaMoC
    Commented Aug 24, 2020 at 19:03
  • 3
    \$\begingroup\$ Since many people seem to agree that this is too close, I will hammer it. Generally I think if a question is just a "loop" of another one it should be closed. For the future, consider using our sandbox so people can help catch this earlier. Still, a great first post! Hope you continue to post :) \$\endgroup\$ Commented Aug 24, 2020 at 19:19

0

Browse other questions tagged or ask your own question.