【发布时间】:2011-02-08 00:25:41
【问题描述】:
代码高尔夫的常规规则。这里以python中的实现为例
from PIL import Image
im = Image.new("RGB", (300,300))
for i in xrange(300):
print "i = ",i
for j in xrange(300):
x0 = float( 4.0*float(i-150)/300.0 -1.0)
y0 = float( 4.0*float(j-150)/300.0 +0.0)
x=0.0
y=0.0
iteration = 0
max_iteration = 1000
while (x*x + y*y <= 4.0 and iteration < max_iteration):
xtemp = x*x - y*y + x0
y = 2.0*x*y+y0
x = xtemp
iteration += 1
if iteration == max_iteration:
value = 255
else:
value = iteration*10 % 255
print value
im.putpixel( (i,j), (value, value, value))
im.save("image.png", "PNG")
结果应该是这样的
允许使用图像库。或者,您可以使用 ASCII 艺术。这段代码也是这样
for i in xrange(40):
line = []
for j in xrange(80):
x0 = float( 4.0*float(i-20)/40.0 -1.0)
y0 = float( 4.0*float(j-40)/80.0 +0.0)
x=0.0
y=0.0
iteration = 0
max_iteration = 1000
while (x*x + y*y <= 4.0 and iteration < max_iteration):
xtemp = x*x - y*y + x0
y = 2.0*x*y+y0
x = xtemp
iteration += 1
if iteration == max_iteration:
line.append(" ")
else:
line.append("*")
print "".join(line)
结果
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
**************************************** ***************************************
**************************************** ***************************************
**************************************** ***************************************
**************************************** ***************************************
**************************************** ***************************************
**************************************** ***************************************
**************************************** ***************************************
*************************************** **************************************
************************************* ************************************
************************************ ***********************************
*********************************** **********************************
************************************ ***********************************
************************************* ************************************
*********************************** **********************************
******************************** *******************************
**************************** ***************************
***************************** ****************************
**************************** ***************************
************************ * * ***********************
*********************** * * **********************
******************** ******* ******* *******************
**************************** ***************************
****************************** *****************************
***************************** * * * ****************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
编辑:
ASCII 艺术规则:
- 行/列大小已参数化,代码必须使用任何有效值。
- 至少三个级别的密度差异取决于迭代次数(因此我的原型不符合要求)
- 水平方向(所以我上面的原型不兼容)
- 关键参数是固定的(最大迭代次数 = 1000,失控值 xx + yy
图形规则:
- 行/列大小已参数化,代码必须使用任何有效值。
- 至少三级颜色,灰度
- 水平方向(我的原型是兼容的)
【问题讨论】:
-
Alternatively, you can use ASCII art. -
ASCII 艺术?那是 Tele-Tubby 吗?
-
谢谢。现在我不能再不颤抖地看那张照片了。
标签: code-golf rosetta-stone fractals mandelbrot