language-agnosticcode-golfrosetta-stone

Code Golf: draw ascii art stars


Since this week noone has posted a code-golf challenge, I'll give it a try. I do it so you can do something other than playing with swords during those long compile cycles.

The challenge:

Draw ASCII art stars, given three numbers on standard input (number of spikes, type of star (star is drawn by joining vertices that are n vertices apart), and diameter of star). Examples:

Input:                        Input:                       Input:

5 2 20                        7 2 20                       7 3 20

Output:                       Output:                      Output:

             x                        x                        x
            xx                        xx                       x
            xx                       x  xx   xx                xx      x
           x x                       x   xxxx x                xx     xx
  xx      x  x                      xxxxx  x  x                x x  xxx
   xxxx   x  x                   xxxx       x x                x x x  x
   x   xxx   x                 xx  x         xx         xxx    x  x  x
    x   x xxxx                 x   x          xx         x xxxxx xx  x
     x  x    xxx                x x           x x         xx   xxxxxx
     x x     x  xxx             x x           x  x          x xx   xxxxxx
      x      x     xx            x            x   x          x x   x     xxx
     x x     x  xxx              x            x  x          x xx   xxxxxx
     x x     xxx                x x           x x         xx   xxxxxx
    x   x xxxx                  x x           xx         x xxxxx xx  x
   x   xxx   x                 xx  x         xx         xxx    x  x  x
   xxxx   x  x                   xxxx       x x                x x x  x
  xx      x  x                      xxxxx  x  x                x x  xxx
           x x                      x    xxxx x                xx     xx
            xx                       x  xx   xx                xx      x
            xx                       x x                       x
             x                        x                        x

Since rasterizing correctly the lines can be a PITA for a code-golf challenge, I'll leave some leeway, but not too much. More examples:

Good enough:

    x           x                   x       x
     xx       xx                    x       x
       x     x                       x     x
     x  xx xx  x                      x   x
          x                            x x
         x x                            x
      xxx   xxx                      x     x
      x       x               xxxxxxxxxxxxxxxxxxxxx
     x         x                xx    x   x    xx
   xx  x     x  xx                xx x     x xx
  x               x                 xxx   xxx
xxxxxxxxxxxxxxxxxxxxx                 xxxxx
        x   x                      x  xx xx  x
                                  x xx     xx x
                                 xxx         xxx
         x x                    xx     x x     xx


          x
                                        x

Doesn't cut it:

            x                  xx               xx
            xx                   x             x
            xx                    x           x
           x x                     xx       xx
 xx       x  x                       x     x
  xxxx   x   x                     x  xx xx  x
  x   xxxx   x                          x
   x    xxxx x                         x x
    x  x    xxx                     xxx   xxx
     xx      x xxxxxx               x       x
     xx      x xxxxxx              x         x
     xx     xxx                  xx  x     x  xx
    x  x xxx x                  x               x
    x xxx    x                xxxxxxxxxxxxxxxxxxxxx
   xxx  x    x                        x   x
 xx      x   x
          x  x
          x  x                         x x
           x x
            xx
                                        x

 Lack of precission              Lack of clipping

Have fun!


Solution

  • Ruby — 323 276 304 297

    a,b,c=gets.split.map &:to_i
    o=(0..c).map{' '*c}
    m=(1..a).map{|i|r=2*h=Math::PI*i/a
    [(1+Math.sin(r))*c/2+0.5,(1+Math.cos(r))*c/2]}
    a.times{|n|j,i,x,y=*m[n],*m[n-b]
    j,x,i,y=i,y,j,x if k=((i-y)/(j-x))**2>1
    s,z=[x,j].sort
    s.to_i.upto(z){|m|t=i+(m-j)*(i-y)/(j-x)
    k ?(o[t][m]='x'):(o[m][t]='x')}}
    puts o
    
                 x              xx                   x                      xx          
                 x              x x                  x                      x x         
                xx              x  x    xx           xx       x            x   x        
               x x             x    xxxx x           xx      xx      xxxxxxxxxxxxxxxx   
     xxx      x  x             xxxxx xx  x           x x    xx       x   x       x  x   
      x xxx   x  x         xxxxx       x x    x      x x   x x       x  x         x x   
      x    xxx   x       xx   x         xx     xxxx  x  xxx x        x x           xx   
       x    x xxxx        x  x           x       x xxxxxx   x        xx             x   
        x  x     xxx      x  x           xxx      x  x xxxxx         x              xx  
         x x     x  xxx    xx            x  x      x xx  x xxxxx    xx              x x 
         xx      x     xx  xx            x   x      xx    x     xxxx x              x  x
         xx      x  xxx    xx            x  x      x xx   x xxxx    xx              x x 
         x x     xxx       xx            xxx      x  x xxxxx         x              xx  
        x  x  xxxx        x  x           x       x xxxxxxx x         xx             x   
       x   xxx   x       xx  x          xx     xxxx  x  xxx x        x x           xx   
      x xxx  x   x         xxxxx       x x    x      x  x  xx        x  x         x x   
     xxx      x  x            x xxxx xx  x           x x    xx       x   x       x  x   
              x  x             x    xxxx x           x x     x       xxxxxxxxxxxxxxxx   
               x x             x   x    xx           xx       x            x   x        
                xx              x x                  xx                     x x         
                 x              xx                   x                       x