在控制台中找到一种使用字符制作给定半径的圆的方法。请指定字体名称和大小。另外,请至少提供一个输出示例。
例如:
输入:
3
输出:
******
** **
** **
* *
** **
** **
******
...嗯,比半径为3的“手绘”“圆”看起来更好的东西。
奖励问题:椭圆。:)
在控制台中找到一种使用字符制作给定半径的圆的方法。请指定字体名称和大小。另外,请至少提供一个输出示例。
例如:
输入:
3
输出:
******
** **
** **
* *
** **
** **
******
...嗯,比半径为3的“手绘”“圆”看起来更好的东西。
奖励问题:椭圆。:)
Answers:
function c(r){var f=1.83;var e=2*Math.PI/(r*r*r*r*r);var s=r*2+1;var g=Array(s);for(var i=0;i<s;i++){g[i]=Array(Math.round(s*f))};for(var i=0;i<=2*Math.PI;i+=e) {var x=Math.round(f*r*Math.cos(i)+f*r);var y=Math.round(r*Math.sin(i))+r;g[y][x]=1;}for(var j=0;j<g.length;j++){for(var i=0;i<g[j].length;i++)document.write((g[j][i]==1)?'*':' ');document.writeln()}}
http://jsfiddle.net/YssSb/3/(f
是行高/字体宽比的校正系数。如果使用方形字体设置,即设置line-height = font-size,则可以设置f = 1并获得“正方形”圆圈。或f
任意设置椭圆。)
输出3(有趣的是,偶然与OP形状完全相同),5、15:
******
** **
** **
* *
** **
** **
******
*********
*** ****
*** **
** **
* *
* *
* *
** **
*** **
*** ****
*********
***************
****** ******
**** *****
*** ***
*** ***
*** ***
** **
** **
** **
** **
** **
* *
** **
* *
* *
* *
* *
* *
** **
* *
** **
** **
** **
** **
** **
*** ***
*** ***
*** ***
**** *****
****** ******
***************
Bresenham-算法有2个要点:
怎么做:
2 1 DCBABCD GFE | EFG IJ y | ---- JI GJ | / JG F | / | F DE | r / | ED C | / | C B 4 | / | B 3 A + ------- A B 4'x B 3' 抄送 DE ED FF GJ JG 伊吉吉 GFE EFG DCBABCD 2'1'
这不是打高尔夫球的代码,但是现有解决方案顶部的所有这些数字让我认为是这样,因此我在打高尔夫球的解决方案上花了很多时间。因此,我也在顶部添加了一个无用的数字。是Pi舍入的11倍。
object BresenhamCircle extends App {
var count = 0
val r = args(0).toInt
// ratio > 1 means expansion in horizontal direction
val ratio = args(1).toInt
val field = ((0 to 2 * r).map (i=> (0 to 2 * r * ratio).map (j=> ' ').toArray)).toArray
def square (x: Int, y: Int): Int = x * x + y * y
def setPoint (x: Int, y: Int) {
field (x)(y*ratio) = "Bresenham"(count)
field (y)(x*ratio) = "Bresenham"(count)
}
def points (x: Int, y: Int)
{
setPoint (r + x, r + y)
setPoint (r - x, r + y)
setPoint (r + x, r - y)
setPoint (r - x, r - y)
}
def bresenwalk () {
var x = 0;
var y = r;
val rxr = r * r
points (x, y);
do
{
val (dx, dy) = { if (math.abs (rxr - square ((x+1), y)) < math.abs (rxr - square (x, (y-1))))
(1, 0)
else
(0, -1)
}
count = (count + 1) % "Bresenham".length
x += dx
y += dy
points (x, y)
}while ((x <= y))
}
bresenwalk ()
println (field.map (_.mkString ("")).mkString ("\n"))
}
字体问题由站点的网络服务器和您的浏览器设置决定。现在,我正在寻找的是
'Droid Sans Mono',Consolas,Menlo,Monaco,Lucida Console,Liberation Mono,DejaVu Sans Mono,Bitstream Vera Sans Mono,Courier New,monospace,serif
字体大小为12px。如果您问我,这是非常无用的信息,但是谁呢?
奖励:椭圆和示例输出:
调用是
scala BresenhamCircle SIZE RATIO
例如
scala BresenhamCircle 10 2
s e r B r e s
h n e e n h
e m a a m e
e r r e
m m
h a a h
n n
s e e s
e e
r r
B B
r r
e e
s e e s
n n
h a a h
m m
e r r e
e m a a m e
h n e e n h
s e r B r e s
A ratio of 2 will print a circular shape for most fonts which happen to be about twice as tall than wide. To compensate for that, we widen by 2.
# As smaller value than 2 only 1 is available:
scala BresenhamCircle 6 1
erBre
aes sea
ah ha
e e
es se
r r
B B
r r
es se
e e
ah ha
aes sea
erBre
# widening it has more freedom:
scala BresenhamCircle 12 5
s e r B r e s
a h n e e n h a
B m m B
e r r e
e s s e
B r r B
a m m a
h h
n n
s e e s
e e
r r
B B
r r
e e
s e e s
n n
h h
a m m a
B r r B
e s s e
e r r e
B m m B
a h n e e n h a
s e r B r e s
我限制了Int的ratio参数以使其简单,但是可以很容易地扩展它以允许浮点数。
172个字符,其中包括两个强制换行符。对圆锥曲线使用Bresenham算法(无除法或乘法);它仅输出方形字体的圆圈,但应不受阶梯效果的影响(即始终具有相同的宽度)。
y=input();t=[y*[' ']for x in range(y)];x=0;y-=1;p=3-2*y
while x<=y:t[x][y]=t[y][x]='*';n,y=((x-y+1,y-1),(x,y))[p<0];p+=4*n+6;x+=1
for s in t[::-1]+t:print"".join(s[::-1]+s)
不是很漂亮,但是很好,我想我会试一试。
****
* *
* *
* *
* *
* *
* *
****
********
*** ***
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
*** ***
********
编辑:错别字,用除法代替加法。
我去了“奖金问题”,让它利用字符长宽比绘制椭圆:)
($w)=@ARGV;for$x(-$w..$w){$p.=abs($x*$x+$_*$_-$w*$w)<$w?'*':$"for(-$w..$w);$p.=$/;}print $p;
输出示例:
>perl circle.pl 3
***
* *
* *
* *
* *
* *
***
>perl circle.pl 5
*****
* *
* *
* *
* *
* *
* *
* *
* *
* *
*****
>perl circle.pl 8
*****
** **
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
** **
*****
g n=map(zipWith(?)f.repeat)f where x?y|abs(x^2+y^2-n^2)<n='*'|0<1=' ';f=[-n..n]
main=interact$unlines.g.read
通过检查所有点是否x²+y²-r²<n起作用。所有符合条件的点都是星号,所有其他点都是空白。
$ echo 3 | 朗哈斯克尔圈 *** * * * * * * * * * * *** $ echo 10 | 朗哈斯克尔圈 ******* ** ** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ** ** *******
参见此处的大型示例:http : //www.ideone.com/t042u
如果字体是方形的,则此代码会产生圆圈。如果您知道字体的高/宽比,则很容易修改以生成标称椭圆。
import math
r=input()
d=2*r+1
c=[' '*d]*d
for a in xrange(9*d):f=math.pi*a/r/9; x=int(r+r*math.sin(f)+.5);y=int(r+r*math.cos(f)+.5);c[y]=c[y][:x]+'*'+c[y][x+1:]
for s in c:print s
例子:
4:
*****
** **
** **
* *
* *
* *
** **
** **
*****
7:
*****
** **
** **
** **
** **
* *
* *
* *
* *
* *
** **
** **
** **
** **
*****
#include<math.h>
main(){int r=10,c=r*2+1,q=c*c,d;for(;q--;)d=hypot(r-q%c,r-q/c),printf("%c%s",d>r-4&&d<=r?42:32,q%c?"":"\n");}
结果:
*********
*************
***************
*****************
****** ******
***** *****
***** *****
**** ****
**** ****
**** ****
**** ****
**** ****
**** ****
**** ****
***** *****
***** *****
****** ******
*****************
***************
*************
*********