Extendify the ASCII Sun

C: 116 102 99 95 92 90

s(n){for(int c=-n,r=c;r<=n;c++)putchar(c>n?c=-c,r++,10:c?r?c-r?c+r?32:47:92:45:r?124:79);}

I think that I am getting fairly close to a minimal solution using this approach, but I can't stop feeling that there is a much better approach in C. Ungolfed:

void s(int n) {
  for(
    int c = -n, r = c;
    r <= n;
    c++
  )
    putchar(
      c > n
        ? c = -c, r++, '\n'
        : c
          ? r
            ? c - r
              ? c + r
                ? ' '
                : '/'
              : '\\'
            : '-'
          : r
            ? '|'
            : 'O'
    );
}

GNU sed, 252 + 1

Phew - I beat the php answer!

Score + 1 for using the -r parameter.

Because of sed limitations, we have to burn nearly 100 bytes just convert N to a string of N spaces. The rest is the fun stuff.

/^0/{y/0/O/;q}
s/./<&/g
s/9/8 /g
s/8/7 /g
s/7/6 /g
s/6/5 /g
s/5/4 /g
s/4/3 /g
s/3/2 /g
s/2/1 /g
s/1/ /g
s/0//g
:t
s/ </<          /g
tt
s/<//g
:
s/ //
s^.*^\\&|&/^;ta
:a
/\\$/q
p
s^\\\|/^-O-^;tn
s^(\\)? (/)?^\2 \1^g;ta
:n
y/ /-/
p
s^-O-^/|\\^
y/-/ /
ta

Explanation

  • The first line is an early exit for the N=0 case.
  • The next 15 lines (up to the :) convert N to a string of N spaces
  • s/ // removes one space
  • s^.*^\\&|&/^;ta converts N-1 spaces to: \ + N-1 spaces + | + N-1 spaces + /
  • Iterate, printing each iteration, and moving \ one space to the right and / one space to the left...
  • ...until we match \|/, which is replaced with -O- and jump to the n label
  • replace with - and print
  • replace -0- with /|\, and replace with - and jump back into the main loop
  • Iterate, printing each iteration, and moving \ one space to the right and / one space to the left...
  • ...until we match \$ which indicates were finished, and quit.

Output

 $ for i in {0..3}; do sed -rf asciisun.sed <<< $i ; done
 O
 \|/
 -O-
 /|\
 \ | /
  \|/ 
 --O--
  /|\ 
 / | \
 \  |  /
  \ | / 
   \|/  
 ---O---
   /|\  
  / | \ 
 /  |  \
 $

J, 37 34 40 bytes

1:echo('O\/-|'{.@#~0=+&|,-,+,[,])"*/~@i:

Usage:

   (1:echo('O\/-|'{.@#~0=+&|,-,+,[,])"*/~@i:) 2  NB. prints to stdout:
\ | /
 \|/ 
--O--
 /|\ 
/ | \

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. for n = 1 creates the following 3-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), equals 0 iff (if and only if) x=0 and y=0
    • - x-y, equals 0 iff x=y i.e. we are on the diagonal
    • + x+y, equals 0 iff x=-y i.e. we are on the anti-diagonal
    • [ x, equals 0 iff x=0 i.e. we are on the middle row
    • ] y, equals 0 iff y=0 i.e. we are on the middle column
  • 'O\/-|'#~0= compare these above property values to 0 and take the ith character from the string 'O\/-|' if the ith 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 need
  • we now have the exact matrix we need so we print it to stdout once with 1:echo

Try it online here.