Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!


2 Answers

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          
like image 50
7 revsNakilon Avatar answered Sep 14 '25 22:09

7 revsNakilon


C# : 555 428 characters

using System.Drawing;class P{static void Main(string[]a){int p=int.Parse(a[0]),i=int.Parse(a[1]),l=int.Parse(a[2]),n;var o=System.Console.Out;var b=new Bitmap(l*4/3,l*4/3);var g=Graphics.FromImage(b);g.TranslateTransform(l/8,l/3);for(n=0;n<p;n++){g.DrawLine(Pens.Red,0,0,l,0);g.TranslateTransform(l,0);g.RotateTransform(360*i/p);}for(i=0;i<b.Height;i++,o.WriteLine())for(p=0;p<b.Width;p++)o.Write(b.GetPixel(p,i).A>0?"#":" ");}}

Stars 5 2 20

            #
           # #
           # #
           # #
          #   #
          #   #
  #####################
   ##    #     #    ##
     #   #     #   #
      #  #     #  #
       ##       ##
        ##     ##
        # #   # #
       #   ###   #
       #   # #   #
       # ##   ## #
      # #       # #
      ##         ##
      #           #
like image 40
4 revs, 2 users 88%Adrian Avatar answered Sep 14 '25 21:09

4 revs, 2 users 88%Adrian