code-golf - Code Golf : the Mandelbrot set

标签 code-golf rosetta-stone fractals mandelbrot

Code Golf 的常用规则。这里以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")

结果应该是这样的

Mandelbrot set

允许使用图像库。或者,您可以使用 ASCII art。此代码执行相同的操作

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 艺术规则:

  • 行/列的大小已参数化,并且代码必须适用于任何有效值。
  • 根据迭代次数,密度至少有三个级别的差异(因此我的原型(prototype)不符合要求)
  • 水平方向(所以我的原型(prototype)不符合要求)
  • 关键参数是固定的(最大迭代 = 1000,失控值 xx + yy <= 4.0)

图形规则:

  • 行/列的大小已参数化,并且代码必须适用于任何有效值。
  • 至少三级颜色、灰度
  • 水平方向(我的原型(prototype)符合要求)

最佳答案

几年前就已经有了 Perl 解决方案 发表于perlmonks ,内容如下:

#!/usr/bin/perl
 $r=25; $c=80;
                                              $xr=6;$yr=3;$xc=-0.5;$dw=$z=-4/
                                              100;local$";while($q=$dr=rand()
                                             /7){$w+=$dw;$_=join$/,map{$Y=$_*
                                             $yr/$r;
  join""                                    ,map{$                  x=$_*$
 xr/$c;($                                   x,$y)=                 ($xc+$x
  *cos($                                   w)-$Y*               sin$w,$yc+
                                           $x*sin              ($w)+$Y*cos
  $w);$                                   e=-1;$                    a=$b=0
;($a,$b)   =($u-$v+$x,2*$a*               $b+$y)                    while(
$ u=$a*$   a)+($v=$b*$b)<4.5  &&++$e     <15;if                     (($e>$
  q&&$e<   15)||($e==$q and   rand()     <$dr))  {$q=$e;($d0,$d1)   =($x,$
  y); }                        chr(+(   32,96,+  46,45,43,58,73,37  ,36,64
 ,32)[$                        e/1.5]   );}(-$   c/2)..($c/2)-1;}   (-$r/2
 )..($     r/2)-1;select$",     $",$", 0.015;                       system
$^O=~m     ~[wW]in~x?"cls":     "clear";print                       ;$xc=(
$d0+15     *$xc)/16;$yc=($       d1+15*$yc)/                        16;$_*=
1+$z for                         $xr,$yr;$dw                     *=-1 if rand
()<0.02;                          (++$i%110                      )||($z*=-1)}

这是一个“Mandelbrot 浏览器”。

(它会旋转、放大和缩小,并随机滚动以检查区域 曼德尔布罗特集的它认为“有趣”。 它是创造者。)

它并不完全遵循此处指定的规范,但是 做了一个有趣的条目(恕我直言)。也许是一个简单的 对于 Perl 诸神来说,Mandlebrot 并不是很有趣;.-)

问候

rboo

关于code-golf - Code Golf : the Mandelbrot set,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2639281/

相关文章:

language-agnostic - Code-Golf:模数除法

string - Code Golf : "Color highlighting" of repeated text

code-golf - Code Golf : Leibniz formula for Pi

language-agnostic - 以最少的代码字符创建、排序和打印包含 100 个随机整数的列表

java - 改进绘制毕达哥拉斯树

language-agnostic - Code Golf : Frobenius Number

language-agnostic - Code Golf : Shortest code to find a weighted median?

language-agnostic - 文件 Fix-it codegolf (GCJ 2010 1B-A)

python - 在Mandelbrot中实现平滑着色

java - 生成 Mandelbrot 分形时如何设置 c 的值?