Code Golf:绘制 ASCII 艺术明星
由于本周没有人发布代码高尔夫挑战,我将尝试一下。我这样做是为了让你可以在漫长的编译周期中做一些除了玩剑之外的事情。
挑战:
绘制 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
由于正确光栅化这些线条可能是代码高尔夫挑战的 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
没问题:
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
玩得开心!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
红宝石 —
323 276 304297Ruby —
323 276 304297C# :
555428 个字符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=新位图(l*4/3, l*4/3);var g=Graphics.FromImage(b);g.TranslateTransform(l/8,l/3);for(n=0;n
0?"#":" ");}}
星星 5 2 20
C# :
555428 charactersusing 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
OCaml,659 个字符 (wc -c)。
使用经典的 Bresenham 算法来绘制线条,为了好玩而递归。
另存为
stars.ml
并使用echo 5 2 20 | 运行ocaml star.ml
。输出
OCaml, 659 characters (wc -c).
Uses the classical Bresenham algorithm to draw the lines, made recursive for fun.
Save as
stars.ml
and run withecho 5 2 20 | ocaml stars.ml
.Outputs
我第一次尝试编码高尔夫...
Python -
550533507484451> 437 个字符另存为
k.py
。以python k.py 5 2 20
运行(通过用制表符替换第二级缩进,可以节省 2 个字符...)
此处提供扩展版本:http://gist.github.com/591485
示例输出:
My first attempt at code golf...
Python -
550533507484451437 charsSave as
k.py
. Run aspython k.py 5 2 20
(Can potentially save 2 more chars by replacing 2nd-level indent with tabs...)
Expanded version available here: http://gist.github.com/591485
Example output:
VBScript,
584 511482 字节我还对线条使用了 Bresenham 算法。
输出:
扩展代码:
VBScript,
584 511482 bytesI also used the Bresenham algorithm for lines.
Output:
Expanded code:
Javascript(基于Isc的python解决方案),
598591586559字节(与rhino shell一起使用:另存为'stars.js',运行与“rhinostars.js 7 2 20”):输出:
扩展代码:
Javascript (based on Isc's python solution),
598591586559 bytes (used with rhino shell: save as 'stars.js', run with 'rhino stars.js 7 2 20'):Output:
Expanded code: