Write a program or function that prints an input string around the discrete circle that has the minimum possible radius. For example, for input This is an example
, your program should output:
a si
n s
i
e h
x T
a
m
p
le
Circle generation
You shall use the Midpoint circle algorithm to compute the coordinates of each point of the discrete circle. You can find examples on how to implement this algorithm on this Wikipedia page.
Here is the pseudo code of the algorithm (based on Wikipedia's C example):
integer x = radius
integer y = 0
int decisionCriterion = 1 - x
while y <= x
point at coordinates (x,y) belongs to the circle // Octant 1
point at coordinates (y,x) belongs to the circle // Octant 2
point at coordinates (-x,y) belongs to the circle // Octant 4
point at coordinates (-y,x) belongs to the circle // Octant 3
point at coordinates (-x,-y) belongs to the circle // Octant 5
point at coordinates (-y,-x) belongs to the circle // Octant 6
point at coordinates (x,-y) belongs to the circle // Octant 7
point at coordinates (y,-x) belongs to the circle // Octant 8
y++
if decisionCriterion <= 0
decisionCriterion += 2 * y + 1
else
x--
decisionCriterion += 2 * (y - x) + 1
end while
You may use a different algorithm if and only if it produces the exact same circles that the Midpoint circle algorithm produces, for all radiuses.
The circle must have the smallest possible radius that still allows to write all letters of the input.
If the circle ends up with more points than the number of characters in the string, then the last filling characters will be spaces .
The first character of the input must be printed on the point with coordinates (Radius,0)
. The subsequent characters are printed in an anticlockwise fashion.
Inputs
The input is a string of any ASCII characters between the space (32) and the tilde
~
(126).
You may assume the input will always be valid, shorter than 256 characters and at least 5 characters long.
The input may be taken from STDIN, or as a function parameter, or anything similar.
Outputs
You may output the result either to STDOUT, or return it as string from a function.
You may have trailing spaces, provided that it does not cause the line to exceed the longest line (the middle one) (as such, the middle line cannot have trailing spaces).
A trailing new line is permitted.
Test cases
Input: Hello, World!
Output:
,ol
l
W e
o H
r
l
d!
Input: 4 8 15 16 23 42
Output:
51
8
1
6 4
2 2
3 4
Input: Programming Puzzles & Code golf
Output:
gnim
uP ma
z r
z g
l o
e r
s P
&
C
od f
e Gol
Input: Ash nazg durbatuluk, ash nazg gimbatul, ash nazg thrakatuluk agh burzum-ishi krimpatul.
Output:
zan hsa ,
g ku
ig lu
bm ta
a b
t r
u u
l d
,
g
a z
s a
h n
n h
a s
z A
g
t
h
r
a .
k l
a u
t t
u a
l p
u m
k ri
ag k
h hi
burzum-is
Scoring
This is code-golf, so the shortest answer in bytes wins.