Perl,235 211 225 211 207 196 179 177 175 168 160 156 146 个字符
<>=~/-\d+/;for$y(@a=-$'..$'){print+(map$_|$y?!($t=8*($y>0)+atan2(-$y,$_)/atan2 1,1)&-$&/45==8|$t>=$`/45&$t<=-$&/45?qw(- / | \\)[$t%4]:$":o,@a),$/}
Perl 使用说功能,161 149 139 个字符
$ echo -n '<>=~/-\d+/;for$y(@a=-$'"'"'..$'"'"'){say map$_|$y?!($t=8*($y>0)+atan2(-$y,$_)/atan2 1,1)&-$&/45==8|$t>=$`/45&$t<=-$&/45?qw(- / | \\)[$t%4]:$":o,@a}' | wc -c
139
$ perl -E '<>=~/-\d+/;for$y(@a=-$'"'"'..$'"'"'){say map$_|$y?!($t=8*($y>0)+atan2(-$y,$_)/atan2 1,1)&-$&/45==8|$t>=$`/45&$t<=-$&/45?qw(- / | \\)[$t%4]:$":o,@a}'
Perl 没有尾随换行符,153 143 个字符
<>=~/-\d+/;for$y(@a=-$'..$'){print$/,map$_|$y?!($t=8*($y>0)+atan2(-$y,$_)/atan2 1,1)&-$&/45==8|$t>=$`/45&$t<=-$&/45?qw(- / | \\)[$t%4]:$":o,@a}
原版评论:
$_=<>;m/(\d+)-(\d+) (\d+)/;$e=$1/45;$f=$2/45; # parse angles and radius, angles are 0-8
for$y(-$3..$3){ # loop for each row and col
for$x(-$3..$3){
$t=atan2(-$y,$x)/atan2 1,1; # angle of this point
$t+=8if($t<0); # normalize negative angles
@w=split//,"-/|\\"x2; # array of ASCII symbols for enclosing lines
$s.=!$x&&!$y?"o":$t==$e||$t==$f?$w[$t]:$t>$e&&$t<$f?"x":$";
# if it's origin -> "o", if it's enclosing line, get symbol from array
# if it's between enclosing angles "x", otherwise space
}
$s.=$/;
}
print$s;
编辑 1:内联子、关系和相等运算符返回 0 或 1。
编辑 2:添加了带有注释的版本。
编辑 3:将封闭线固定在 360º。字符数显着增加。
编辑 4:添加了一个较短的版本,改变了规则。
编辑 5:更智能地修复 360º 封闭线。此外,使用数字作为填充。这两件事都很明显。嗯,我应该多睡点:/
编辑 6:m
从匹配运算符中删除不需要的。删除了一些分号。
编辑 7:更智能的正则表达式。不到 200 个字符!
编辑 8:许多小的改进:
- 内部 for 循环 -> 映射(1 个字符)
- 来自字符串的符号数组
split
-> qw(3 个字符)
- 内联符号数组(6个字符,加上之前改进的9个字符!)
- 逻辑或 -> 按位或(1 个字符)
- 正则表达式改进(1 个字符)
- 受雅各布的回答启发,使用算术测试负角(5 个字符)
编辑 9:在条件运算符中进行一些重新排序可以节省 2 个字符。
编辑 10:对字符使用裸词。
编辑 11:受 Lowjacker 的回答启发,在循环内移动打印。
编辑 12:使用say
.
编辑 13:重用角度字符作为填充字符,就像 Gwell 的回答一样。输出不如 Gwell 的好,这需要 5 个额外的字符 :) 此外,.. 运算符不需要括号。
编辑 14:将正则表达式直接应用于 <>。根据 Adrian 对 bta 答案的建议,将范围运算符分配给变量。添加没有最终换行符的版本。更新say
版本。
编辑 15:更多内联。映射{块}@a -> 映射表达式,@a。