J, 37 34 40 bytes
1:echo('O\/-|'{.@#~0=+&|,-,+,[,])"*/~@i:
Usage:
(1:echo('O\/-|'{.@#~0=+&|,-,+,[,])"*/~@i:) 2 NB. prints to stdout:
\ | /
\|/
--O--
/|\
/ | \
I will add explanation tomorrow.Explanation (from left to right):
i:
generates list-n, -(n-1), ..., n-1, n
( )"*/~@i:
creates the Descartes product of i: with itself in a matrix arrangement, e.g. forn = 1
creates the following3-by-3
matrix┌─────┬────┬────┐ │-1 -1│-1 0│-1 1│ ├─────┼────┼────┤ │0 -1 │0 0 │0 1 │ ├─────┼────┼────┤ │1 -1 │1 0 │1 1 │ └─────┴────┴────┘
for every matrix-element with integers
x y
we do the following+&|,-,+,[,]
calculate a list of properties+&|
abs(x)+abs(y)
, equals0
iff (if and only if)x=0
andy=0
-
x-y, equals0
iffx=y
i.e. we are on the diagonal+
x+y, equals0
iffx=-y
i.e. we are on the anti-diagonal[
x, equals0
iffx=0
i.e. we are on the middle row]
y, equals0
iffy=0
i.e. we are on the middle column
'O\/-|'#~0=
compare these above property values to0
and take thei
th character from the string'O\/-|'
if thei
th property is true.the first character in the resulting string will always be the one we need, if there string is empty we need a space
{.
takes the first character of a string and if there is no one it returns a space character as padding just as we needwe now have the exact matrix we need so we print it to stdout once with
1:echo