高尔夫代码:曼德布洛特(Mandelbrot)集 [英] Code golf: the Mandelbrot set

查看:150
本文介绍了高尔夫代码:曼德布洛特(Mandelbrot)集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

高尔夫通用规则.这是python中的一个实现示例

Usual rules for the code golf. Here is an implementation in python as an example

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艺术.这段代码是一样的

Use of an image library is allowed. Alternatively, you can use ASCII art. This code does the same

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

Rules for the ASCII art:

  • 行/列的大小已参数化,并且代码必须使用任何有效值.
  • 根据迭代次数,密度至少要区分三个级别(因此我的原型不符合要求)
  • 水平定向(因此我的原型不兼容)
  • 关键参数是固定的(最大迭代= 1000,失控值x x + y y <= 4.0)
  • size in rows/columns is parametrized and the code must work with any valid value.
  • at least three level of differentiation in density depending on the iteration count (so my prototype up there is not compliant)
  • oriented horizontally (so my prototype up there is not compliant)
  • critical parameters are fixed (max iteration = 1000, runaway value xx + yy <= 4.0)

图形规则:

  • 行/列的大小已参数化,并且代码必须使用任何有效值.
  • 至少三种颜色的灰度级
  • 水平方向(我的原型兼容)

推荐答案

几年前已经有一个perl解决方案 发布在 perlmonks 中,内容为:

There was a perl solution already some years ago posted in perlmonks, it reads:

#!/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资源管理器".

which is a "Mandelbrot explorer".

(它旋转,放大和缩小,并随机滚动以检查区域 据曼德尔布罗特(Mandelbrot)报道,它认为有趣". 是创建者.)

(It rotates, zooms in & out, and scrolls randomly to examine regions of the Mandelbrot set it deems "interesting.", according to it's creator.)

它不完全遵循此处命名的规范,但 进行一个有趣的输入(恕我直言).也许很简单 Mandlebrot对于Perl众神而言不是很有趣; .-)

It doesn't exactly follow the specs named here but makes an interesting entry (imho). Maybe a simple Mandlebrot is not very interesting for the perl gods ;.-)

致谢

rboo

这篇关于高尔夫代码:曼德布洛特(Mandelbrot)集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆