Code Golf:绘制ascii艺术明星

nin*_*alj 25 language-agnostic code-golf rosetta-stone

由于本周没有人发布过代码高尔夫挑战,我会试一试.我这样做是为了在漫长的编译周期中除了玩剑之外你还能做些什么.

挑战:

绘制ASCII艺术星,在标准输入上给出三个数字(尖峰的数量,星的类型(通过连接n个顶点的顶点绘制星形)和星的直径).例子:

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
Run Code Online (Sandbox Code Playgroud)

由于正确地光栅化线条可以成为代码高尔夫挑战的PITA,我会留下一些余地,但不会太多.更多例子:

够好了:

    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
Run Code Online (Sandbox Code Playgroud)

不削减它:

            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
Run Code Online (Sandbox Code Playgroud)

玩得开心!

Nak*_*lon 6

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          
Run Code Online (Sandbox Code Playgroud)


Mes*_*esh 5

C#:555 428个字符

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?"#":" ");}}

星5 2 20

            #
           # #
           # #
           # #
          #   #
          #   #
  #####################
   ##    #     #    ##
     #   #     #   #
      #  #     #  #
       ##       ##
        ##     ##
        # #   # #
       #   ###   #
       #   # #   #
       # ##   ## #
      # #       # #
      ##         ##
      #           #
Run Code Online (Sandbox Code Playgroud)