代码高尔夫:Mandelbrot 集

发布于 2024-08-29 13:16:58 字数 5077 浏览 5 评论 0原文

代码高尔夫的通常规则。以下是 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 艺术规则:

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

图形规则:

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

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")

The result should look like this

Mandelbrot set

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)

The result

********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
**************************************** ***************************************
**************************************** ***************************************
**************************************** ***************************************
**************************************** ***************************************
**************************************** ***************************************
**************************************** ***************************************
**************************************** ***************************************
***************************************   **************************************
*************************************       ************************************
************************************         ***********************************
***********************************           **********************************
************************************         ***********************************
*************************************       ************************************
***********************************           **********************************
********************************                 *******************************
****************************                         ***************************
*****************************                       ****************************
****************************                         ***************************
************************   *                         *   ***********************
***********************    *                         *    **********************
******************** *******                         ******* *******************
****************************                         ***************************
******************************                     *****************************
*****************************  *        *        *  ****************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************

Edit:

Rules for the ASCII art:

  • 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)

Rules for the graphic:

  • size in rows/columns is parametrized and the code must work with any valid value.
  • at least three level of colors, gray scale
  • oriented horizontally (my prototype is compliant)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(15

说好的呢 2024-09-05 13:16:58

几年前就已经有了 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

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)}

which is a "Mandelbrot explorer".

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

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 ;.-)

Regards

rboo

离鸿 2024-09-05 13:16:58

J,带级别的灰度图形输出,170 个字符。

load'viewmat'
c=:>(<:;|:;0$])((2
gt;:@+:)$-:%~i:)300
(2.5*|:3 99$i._99)viewmat {:99&<.@;@(({:+4&<@:({.+1&{)@:*:);~(0{c)&+@({.-1&{)@:*:,:(1{c)&+@:+:@({.*1&{))^:99 c

alt text http://i40.tinypic.com/2i7lm0.jpg

J,带级别的图形输出,151 个字符。

load'viewmat'
c=:>(<:;|:;0$])((2
gt;:@+:)$-:%~i:)99
viewmat {:99&<.@;@(({:+4&<@:({.+1&{)@:*:);~(0{c)&+@({.-1&{)@:*:,:(1{c)&+@:+:@({.*1&{))^:99 c

输出:

替代文本 http://i40.tinypic.com/6ynxap.jpg

J,图形输出,124 个字符。

load'viewmat'
c=:>(<:;|:)((2
gt;:@+:)$-:%~i:)200
viewmat 4>:({.+{:)@:*:(5<.>@(({.c)&+@({.-{:)@:*:;({:c)&+@:+:@({.*{:)))^:999 c

基本上只是在 ascii 输出上运行“viewmat”。

替代文本 http://i40.tinypic.com/etv5lf.jpg

J,ASCII,101 个字符。

c=:>(<:;|:)((2
gt;:@+:)$-:%~i:)20
4>:({.+{:)@:*:(5<.>@(({.c)&+@({.-{:)@:*:;({:c)&+@:+:@({.*{:)))^:999 c

浪费了 6 个字符来正确参数化大小。

J,ASCII,95 个字符。

c=:>(<:;|:)41 41$10%~i:20
4>:({.+{:)@:*:(5<.>@(({.c)&+@({.-{:)@:*:;({:c)&+@:+:@({.*{:)))^:999 c

它还不能实现多级灰度。

说明(目前已过时,稍后更新):

i:20

生成从-20到20的整数列表。

10%~i:20

将它们全部除以10(%是除法,~是倒数将此

41 41$10%~i:20 

列表填充到 41x41 数组中,并

(-&1;|:)41 41

J,带级别的灰度图形输出,170 个字符。

load'viewmat'
c=:>(<:;|:;0$])((2
gt;:@+:)$-:%~i:)300
(2.5*|:3 99$i._99)viewmat {:99&<.@;@(({:+4&<@:({.+1&{)@:*:);~(0{c)&+@({.-1&{)@:*:,:(1{c)&+@:+:@({.*1&{))^:99 c

alt text http://i40.tinypic.com/2i7lm0.jpg

J,带级别的图形输出,151 个字符。

load'viewmat'
c=:>(<:;|:;0$])((2
gt;:@+:)$-:%~i:)99
viewmat {:99&<.@;@(({:+4&<@:({.+1&{)@:*:);~(0{c)&+@({.-1&{)@:*:,:(1{c)&+@:+:@({.*1&{))^:99 c

输出:

替代文本 http://i40.tinypic.com/6ynxap.jpg

J,图形输出,124 个字符。

load'viewmat'
c=:>(<:;|:)((2
gt;:@+:)$-:%~i:)200
viewmat 4>:({.+{:)@:*:(5<.>@(({.c)&+@({.-{:)@:*:;({:c)&+@:+:@({.*{:)))^:999 c

基本上只是在 ascii 输出上运行“viewmat”。

替代文本 http://i40.tinypic.com/etv5lf.jpg

J,ASCII,101 个字符。

c=:>(<:;|:)((2
gt;:@+:)$-:%~i:)20
4>:({.+{:)@:*:(5<.>@(({.c)&+@({.-{:)@:*:;({:c)&+@:+:@({.*{:)))^:999 c

浪费了 6 个字符来正确参数化大小。

J,ASCII,95 个字符。

c=:>(<:;|:)41 41$10%~i:20
4>:({.+{:)@:*:(5<.>@(({.c)&+@({.-{:)@:*:;({:c)&+@:+:@({.*{:)))^:999 c

它还不能实现多级灰度。

说明(目前已过时,稍后更新):

i:20

生成从-20到20的整数列表。

10%~i:20

将它们全部除以10(%是除法,~是倒数将此

41 41$10%~i:20 

列表填充到 41x41 数组中,并

10%~i:20`

在该数组中附加相同大小的第二个数组,但 -&1 从每个元素中减去 1。 ; 是追加,|: 是转置。现在我们有一个 2x41x41 数组,其中第一个 41x41 数组包含每个坐标的“x0”值,第二个包含“y0”值。哇

4<:~({.+{:)@:*:(5<.>@(({.c)&+@:({.-{:)@:*:;({:c)&+@:+:@:({.*{:)))^:1000 ($c)$0

,让我们从右边分解它,

($c)$0

这将创建第二个 2x41x41 数组(与 c 大小相同),这将是我们要迭代的变量 - 基本上第一个数组包含“x”。 ” 值,第二个包含“y”值。

^:1000 表示“重复括号中的前一操作1000 次。这是我们的循环计数器。

(5<.>@(({.c)&+@:({.-{:)@:*:;({:c)&+@:+:@:({.*{:))

这是下一个大块。循环的迭代。首先我们用 (({.c)&+@:({.-{:)@:*: 生成 x,然后将 ; 连接到 y ({:c)&+@:+:@:({.*{:)

   (({.c)&+@:({.-{:)@:*:

这会生成 x (记住它是在代表 x 和 y 的 2x41x41 数组上进行操作)。 : 每个元素,然后 { - {: 或 x^2 - y^2. 选择第一个 41x41 数组,{: 第二个 @: 将函数连接在一起。

对于 x^2-y^2,我们需要添加 x0 - 这是 c 的第一个 41x41 数组,因此我们可以这样做。 with ({.c)&+ - &{.c (x0) 柯式化为 +

({:c)&+@:+:@:({.*{:)

我们以类似的方式生成 y。首先,我们将输入 x {. 和 y {: 数组成对相乘,用 +: 将结果加倍。 ,并添加 y0 {:c

该函数的最后一步就是 5<.> - 拆箱两个新的 x 和 y 数组的连接,并封顶。在 5 处 - 没关系,因为这个函数在 4 以上单调递增,如果我们不限制每次迭代的值,我们将得到越界整数。

4>:({.+{:)@:*:

最后一步,一旦我们完成迭代。我们对 x 和 y 数组 *: 求平方,将它们加在一起 ​​{.+{:,并创建一个对于 > 的每个元素都为 true 的布尔数组: 小于 4。

输出:

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 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0 0 0 0 0

J, greyscale graphical output with levels, 170 characters.

load'viewmat'
c=:>(<:;|:;0$])((2
gt;:@+:)$-:%~i:)300
(2.5*|:3 99$i._99)viewmat {:99&<.@;@(({:+4&<@:({.+1&{)@:*:);~(0{c)&+@({.-1&{)@:*:,:(1{c)&+@:+:@({.*1&{))^:99 c

alt text http://i40.tinypic.com/2i7lm0.jpg

J, graphical output with levels, 151 characters.

load'viewmat'
c=:>(<:;|:;0$])((2
gt;:@+:)$-:%~i:)99
viewmat {:99&<.@;@(({:+4&<@:({.+1&{)@:*:);~(0{c)&+@({.-1&{)@:*:,:(1{c)&+@:+:@({.*1&{))^:99 c

Output:

alt text http://i40.tinypic.com/6ynxap.jpg

J, graphical output, 124 characters.

load'viewmat'
c=:>(<:;|:)((2
gt;:@+:)$-:%~i:)200
viewmat 4>:({.+{:)@:*:(5<.>@(({.c)&+@({.-{:)@:*:;({:c)&+@:+:@({.*{:)))^:999 c

Basically just runs "viewmat" on the ascii output.

alt text http://i40.tinypic.com/etv5lf.jpg

J, ASCII, 101 characters.

c=:>(<:;|:)((2
gt;:@+:)$-:%~i:)20
4>:({.+{:)@:*:(5<.>@(({.c)&+@({.-{:)@:*:;({:c)&+@:+:@({.*{:)))^:999 c

Wasted 6 characters to properly parametrize the size.

J, ASCII, 95 characters.

c=:>(<:;|:)41 41$10%~i:20
4>:({.+{:)@:*:(5<.>@(({.c)&+@({.-{:)@:*:;({:c)&+@:+:@({.*{:)))^:999 c

It doesn't do multiple levels of greyscale yet.

Explanation (currently out of date, will update later):

i:20

Generate the list of integers from -20 to 20.

10%~i:20

Divide them all by 10 (% is divide, ~ is invert order of arguments.

41 41$10%~i:20 

Fill this list into a 41x41 array, with wrapping.

(-&1;|:)41 41

J, greyscale graphical output with levels, 170 characters.

load'viewmat'
c=:>(<:;|:;0$])((2
gt;:@+:)$-:%~i:)300
(2.5*|:3 99$i._99)viewmat {:99&<.@;@(({:+4&<@:({.+1&{)@:*:);~(0{c)&+@({.-1&{)@:*:,:(1{c)&+@:+:@({.*1&{))^:99 c

alt text http://i40.tinypic.com/2i7lm0.jpg

J, graphical output with levels, 151 characters.

load'viewmat'
c=:>(<:;|:;0$])((2
gt;:@+:)$-:%~i:)99
viewmat {:99&<.@;@(({:+4&<@:({.+1&{)@:*:);~(0{c)&+@({.-1&{)@:*:,:(1{c)&+@:+:@({.*1&{))^:99 c

Output:

alt text http://i40.tinypic.com/6ynxap.jpg

J, graphical output, 124 characters.

load'viewmat'
c=:>(<:;|:)((2
gt;:@+:)$-:%~i:)200
viewmat 4>:({.+{:)@:*:(5<.>@(({.c)&+@({.-{:)@:*:;({:c)&+@:+:@({.*{:)))^:999 c

Basically just runs "viewmat" on the ascii output.

alt text http://i40.tinypic.com/etv5lf.jpg

J, ASCII, 101 characters.

c=:>(<:;|:)((2
gt;:@+:)$-:%~i:)20
4>:({.+{:)@:*:(5<.>@(({.c)&+@({.-{:)@:*:;({:c)&+@:+:@({.*{:)))^:999 c

Wasted 6 characters to properly parametrize the size.

J, ASCII, 95 characters.

c=:>(<:;|:)41 41$10%~i:20
4>:({.+{:)@:*:(5<.>@(({.c)&+@({.-{:)@:*:;({:c)&+@:+:@({.*{:)))^:999 c

It doesn't do multiple levels of greyscale yet.

Explanation (currently out of date, will update later):

i:20

Generate the list of integers from -20 to 20.

10%~i:20

Divide them all by 10 (% is divide, ~ is invert order of arguments.

41 41$10%~i:20 

Fill this list into a 41x41 array, with wrapping.

10%~i:20`

Append to this array a second array of the same size, but transposed. -&1 subtracts one from each element, ; is append, |: is transpose. Now we have a 2x41x41 array, where the first 41x41 array contains the "x0" values of each coordinate and the second contains the "y0" values.

4<:~({.+{:)@:*:(5<.>@(({.c)&+@:({.-{:)@:*:;({:c)&+@:+:@:({.*{:)))^:1000 ($c)$0

Wow. Let's break this up from the right.

($c)$0

This creates a second 2x41x41 array (the same size as c), filled with 0s. This is going to be the variable we're iterating on - basically the first array contains the "x" values and the second contains the "y" values.

^:1000 means "Repeat the previous thing in parentheses 1000 times. This is our loop counter.

(5<.>@(({.c)&+@:({.-{:)@:*:;({:c)&+@:+:@:({.*{:))

This is the next big chunk. It's basically doing one iteration of the loop. First we generate x with (({.c)&+@:({.-{:)@:*:, then join ; it to y ({:c)&+@:+:@:({.*{:)

   (({.c)&+@:({.-{:)@:*:

This generates x (remember it's operating on that 2x41x41 array representing x and y). First it squares *: each element, then does {. - {:, or x^2 - y^2. {. selects the first 41x41 array and {: the second. @: joins functions together.

To x^2-y^2, we need to add x0 - this is the first 41x41 array of c so we can do this with ({.c)&+ - & curries {.c (x0) into +.

({:c)&+@:+:@:({.*{:)

We generate y in a similar fashion. First we pairwise multiply the input x {. and y {: arrays, double the result with +:, and add y0 {:c.

The last step of this function is simply 5<.> - unboxing the join of the two new x and y arrays, and capping at 5 - it's ok since this function is monotonically increasing above 4, and we'll get out-of-bounds integers if we don't cap the values on each iteration.

4>:({.+{:)@:*:

The last step, once we've finished iterating. We square the x and y arrays *:, add them together {.+{:, and make a boolean array that's true for every element that's >: less than 4.

Output:

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 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0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
烧了回忆取暖 2024-09-05 13:16:58

Python 108(原为 122):

for a in range(900):print"\n.x"[(a%30>0)+(abs(reduce(lambda z,c:z*z+c,[a%30*.1-2+1j*(a/30*.1-1.5)]*30))<2)],

最后一个“30”是迭代计数,而不是维度之一。
输出如下:

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . . . . . x . . . . . . . . . 
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . . . x x . . . . . . . . . . 
. . . . . . . . . . . . . . . . . x x . . . . . . . . . . 
. . . . . . . . . . . . . . x . x x x x x . . . . . . . . 
. . . . . . . . . . . . . . x x x x x x x x x . . . . . . 
. . . . . . . . . . . . . x x x x x x x x x x . . . . . . 
. . . . . . . . . x . . . x x x x x x x x x x . . . . . . 
. . . . . . . . x x x . x x x x x x x x x x x x . . . . . 
. . . . . . . x x x x x x x x x x x x x x x x . . . . . . 
x x x x x x x x x x x x x x x x x x x x x x . . . . . . . 
. . . . . . . x x x x x x x x x x x x x x x x . . . . . . 
. . . . . . . . x x x . x x x x x x x x x x x x . . . . . 
. . . . . . . . . x . . . x x x x x x x x x x . . . . . . 
. . . . . . . . . . . . . x x x x x x x x x x . . . . . . 
. . . . . . . . . . . . . . x x x x x x x x x . . . . . . 
. . . . . . . . . . . . . . x . x x x x x . . . . . . . . 
. . . . . . . . . . . . . . . . . x x . . . . . . . . . . 
. . . . . . . . . . . . . . . . . x x . . . . . . . . . . 
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . . . . . x . . . . . . . . . 
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . . . . . . . . . . . . . . .

现在将根据更新的规则进行更新......

Python 108 (was 122):

for a in range(900):print"\n.x"[(a%30>0)+(abs(reduce(lambda z,c:z*z+c,[a%30*.1-2+1j*(a/30*.1-1.5)]*30))<2)],

That last "30" is the iteration count, not one of the dimensions.

Output looks thus:

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . . . . . x . . . . . . . . . 
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . . . x x . . . . . . . . . . 
. . . . . . . . . . . . . . . . . x x . . . . . . . . . . 
. . . . . . . . . . . . . . x . x x x x x . . . . . . . . 
. . . . . . . . . . . . . . x x x x x x x x x . . . . . . 
. . . . . . . . . . . . . x x x x x x x x x x . . . . . . 
. . . . . . . . . x . . . x x x x x x x x x x . . . . . . 
. . . . . . . . x x x . x x x x x x x x x x x x . . . . . 
. . . . . . . x x x x x x x x x x x x x x x x . . . . . . 
x x x x x x x x x x x x x x x x x x x x x x . . . . . . . 
. . . . . . . x x x x x x x x x x x x x x x x . . . . . . 
. . . . . . . . x x x . x x x x x x x x x x x x . . . . . 
. . . . . . . . . x . . . x x x x x x x x x x . . . . . . 
. . . . . . . . . . . . . x x x x x x x x x x . . . . . . 
. . . . . . . . . . . . . . x x x x x x x x x . . . . . . 
. . . . . . . . . . . . . . x . x x x x x . . . . . . . . 
. . . . . . . . . . . . . . . . . x x . . . . . . . . . . 
. . . . . . . . . . . . . . . . . x x . . . . . . . . . . 
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . . . . . x . . . . . . . . . 
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Now going to update based on the updated rules....

坠似风落 2024-09-05 13:16:58

Haskell(185个字符,具有问题中所需的所有功能)

使用复数并实际计算所需的迭代次数(与我其他答案中的“简单”版本不同)。

import Complex
i v=show(length$takeWhile((<=4).magnitude)$take 1000$iterate(\c->c*c+v)v)!!0
r t=[-2,4/read t-2..2]
main=getLine>>=(\[s,t]->mapM_ putStrLn[[i$x:+y|x<-r s]|y<-r t]).words

生成图像的尺寸是从标准输入读取的。输出根据所需的迭代次数进行“着色”:

$ ./fract
60 30
1111111111111111111111111111111111111111111111111111111111111
1111111111111112222222222222222111111111111111111111111111111
1111111112222222222222222222222222222111111111111111111111111
1111112222222222222222222222222222222222111111111111111111111
1112222222222222222222222222222222222222222111111111111111111
1222222222222222222222333333333322222222222221111111111111111
2222222222222222223333333344443333332222222222211111111111111
2222222222222233333333344458755443333322222222222111111111111
2222222222233333333344445568117544433333222222222211111111111
2222222233333333344445556711111765544333322222222221111111111
2222233333333444445562391112161568786443332222222222111111111
2223333334444455556681111111111111157544333222222222211111111
2333334445617677777911111111111111111854333222222222211111111
3333444456681131231111111111111111111654333322222222221111111
3444555671111111111111111111111111114644333322222222221111111
2569888111111111111111111111111111296544333322222222221111111
3444555671111111111111111111111111114644333322222222221111111
3333444456681131231111111111111111111654333322222222221111111
2333334445617677777911111111111111111854333222222222211111111
2223333334444455556681111111111111157544333222222222211111111
2222233333333444445562391112161568786443332222222222111111111
2222222233333333344445556711111765544333322222222221111111111
2222222222233333333344445568117544433333222222222211111111111
2222222222222233333333344458755443333322222222222111111111111
2222222222222222223333333344443333332222222222211111111111111
1222222222222222222222333333333322222222222221111111111111111
1112222222222222222222222222222222222222222111111111111111111
1111112222222222222222222222222222222222111111111111111111111
1111111112222222222222222222222222222111111111111111111111111
1111111111111112222222222222222111111111111111111111111111111
1111111111111111111111111111111111111111111111111111111111111

Haskell (185 chars, with all the features required in the question)

Using complex numbers and actually counting the required number of iterations (unlike the "simple" version in my other answer).

import Complex
i v=show(length$takeWhile((<=4).magnitude)$take 1000$iterate(\c->c*c+v)v)!!0
r t=[-2,4/read t-2..2]
main=getLine>>=(\[s,t]->mapM_ putStrLn[[i$x:+y|x<-r s]|y<-r t]).words

The dimensions of the resulting image are read from stdin. The output is "colored" according to the number of required iterations:

$ ./fract
60 30
1111111111111111111111111111111111111111111111111111111111111
1111111111111112222222222222222111111111111111111111111111111
1111111112222222222222222222222222222111111111111111111111111
1111112222222222222222222222222222222222111111111111111111111
1112222222222222222222222222222222222222222111111111111111111
1222222222222222222222333333333322222222222221111111111111111
2222222222222222223333333344443333332222222222211111111111111
2222222222222233333333344458755443333322222222222111111111111
2222222222233333333344445568117544433333222222222211111111111
2222222233333333344445556711111765544333322222222221111111111
2222233333333444445562391112161568786443332222222222111111111
2223333334444455556681111111111111157544333222222222211111111
2333334445617677777911111111111111111854333222222222211111111
3333444456681131231111111111111111111654333322222222221111111
3444555671111111111111111111111111114644333322222222221111111
2569888111111111111111111111111111296544333322222222221111111
3444555671111111111111111111111111114644333322222222221111111
3333444456681131231111111111111111111654333322222222221111111
2333334445617677777911111111111111111854333222222222211111111
2223333334444455556681111111111111157544333222222222211111111
2222233333333444445562391112161568786443332222222222111111111
2222222233333333344445556711111765544333322222222221111111111
2222222222233333333344445568117544433333222222222211111111111
2222222222222233333333344458755443333322222222222111111111111
2222222222222222223333333344443333332222222222211111111111111
1222222222222222222222333333333322222222222221111111111111111
1112222222222222222222222222222222222222222111111111111111111
1111112222222222222222222222222222222222111111111111111111111
1111111112222222222222222222222222222111111111111111111111111
1111111111111112222222222222222111111111111111111111111111111
1111111111111111111111111111111111111111111111111111111111111
野生奥特曼 2024-09-05 13:16:58

Delphi - 310 249 239 224 个字符

ASCII 版本。使用 7 个级别进行渐变着色。

  • 根据(Uwe Raabe 建议)删除了 program P;{$APPTYPE CONSOLE}
  • 内联 1000 const(由 Uwe Raabe 建议);
  • 内联'8Oo;,。 ' 变量;
  • word 更改为 integer,以便循环变量可以为负数,然后将 I 的循环范围从 3..38 更改为 -5..30,以便 (I-8) 可以替换为 I(由 Eric Grange 建议);
  • 1000 更改为 1e3(由 Eric Grange 建议)
  • 将类型 Double 更改为 Real(由 Luc Neville 通过 e 建议) -mail)
  • 删除了括号 while((x*x+y*y<=4)and(t<1000))do --> while(x*x+y*y<=4)and(t<1000)do(Luc Neville 通过电子邮件建议)
  • 删除了后面跟有关键字的数字之间的空格(以便for j:=-5 to 30 do 变为 for j:=-5to 30do(Neville 通过电子邮件建议)
  • 将循环范围更改为 for j: =8to 40do,删除负号,并将整数更改回单词 通过更改 x * x - y * y + i / 16 - 2; 来补偿公式中的此偏移量。到 x * x - y * y + i / 16 - 3;

不可读版本:

var n,x,y:Real;t,i,j:Word;begin for j:=8to 40do begin for i:=8to 65do begin x:=0;y:=0;t:=0;while(x*x+y*y<=4)and(t<1e3)do begin n:=x*x-y*y+i/16-3;y:=2*x*y+j/12-2;x:=n;inc(t)end;Write('8Oo;,. '[t mod 7+1])end;WriteLn;end;end.

格式化代码:

var
  n, x, y: Real;
  t, i, j: Word;
begin
  for j := 8 to 40 do
  begin
    for i := 8 to 65 do
    begin
      x := 0;
      y := 0;
      t := 0;
      while (x * x + y * y <= 4) and (t < 1e3) do
      begin
        n := x * x - y * y + i / 16 - 3;
        y := 2 * x * y + j / 12 - 2;
        x := n;
        inc(t)
      end;
      Write('8Oo;,. '[t mod 7 + 1])
    end;
    WriteLn;
  end;
end.

输出:

OOOOOOOOOOOOOOOOOooooooooooooooooooooooooooooooooooooooooo
OOOOOOOOOOOOOOOOooooooooo;;;;;;;;;;;;;;;oooooooooooooooooo
OOOOOOOOOOOOOOOoooooo;;;;;;;;;;;;;;,,,,,;;;;oooooooooooooo
OOOOOOOOOOOOOOooooo;;;;;;;;;;;;;,,,, ; .,,,;;;oooooooooooo
OOOOOOOOOOOOOoooo;;;;;;;;;;;;;,,,,.. O   ,,,,;;;oooooooooo
OOOOOOOOOOOOooo;;;;;;;;;;;;;,,,,,...8oo,8..,,,;;;;oooooooo
OOOOOOOOOOOooo;;;;;;;;;;;;,,,,,,.. Oo. ; 8...,,;;;;ooooooo
OOOOOOOOOOOoo;;;;;;;;;;;;,,,,,.   8OO    O8 ..,,;;;;oooooo
OOOOOOOOOOoo;;;;;;;;;;;,,,,..  O,oO8oo ,OOoO88,.,;;;;ooooo
OOOOOOOOOOo;;;;;;;;;;,,,....  o. .         OOO  ,;;;;;oooo
OOOOOOOOOO;;;;;;;;;,,......  o.,              o .,;;;;;ooo
OOOOOOOOOo;;;;;;,,. o     88O.                ;O.,;;;;;ooo
OOOOOOOOO;;;,,,,,.. ,,o;,oOo;                  o ,,;;;;;oo
OOOOOOOOO;,,,,,... 8o;   8;,                    .,,;;;;;oo
OOOOOOOOO,,,,,... Oo       .                   8.,,;;;;;oo
OOOOOOOOO,.,.  88oo o       ,                 , .,,;;;;;oo
OOOOOOOO                                     ,8 .,,;;;;;;o
OOOOOOOOO,.,.  88oo o       ,                 , .,,;;;;;oo
OOOOOOOOO,,,,,... Oo       .                   8.,,;;;;;oo
OOOOOOOOO;,,,,,... 8o;   8;,                    .,,;;;;;oo
OOOOOOOOO;;;,,,,,.. ,,o;,oOo;                  o ,,;;;;;oo
OOOOOOOOOo;;;;;;,,. o     88O.                ;O.,;;;;;ooo
OOOOOOOOOO;;;;;;;;;,,......  o.,              o .,;;;;;ooo
OOOOOOOOOOo;;;;;;;;;;,,,....  o. .         OOO  ,;;;;;oooo
OOOOOOOOOOoo;;;;;;;;;;;,,,,..  O,oO8oo ,OOoO88,.,;;;;ooooo
OOOOOOOOOOOoo;;;;;;;;;;;;,,,,,.   8OO    O8 ..,,;;;;oooooo
OOOOOOOOOOOooo;;;;;;;;;;;;,,,,,,.. Oo. ; 8...,,;;;;ooooooo
OOOOOOOOOOOOooo;;;;;;;;;;;;;,,,,,...8oo,8..,,,;;;;oooooooo
OOOOOOOOOOOOOoooo;;;;;;;;;;;;;,,,,.. O   ,,,,;;;oooooooooo
OOOOOOOOOOOOOOooooo;;;;;;;;;;;;;,,,, ; .,,,;;;oooooooooooo
OOOOOOOOOOOOOOOoooooo;;;;;;;;;;;;;;,,,,,;;;;oooooooooooooo
OOOOOOOOOOOOOOOOooooooooo;;;;;;;;;;;;;;;oooooooooooooooooo
OOOOOOOOOOOOOOOOOooooooooooooooooooooooooooooooooooooooooo

Delphi - 310 249 239 224 chars

ASCII version. Uses 7 levels for gradient shading.

  • Removed program P; and {$APPTYPE CONSOLE} according to (suggested by Uwe Raabe);
  • Inlined 1000 const (suggested by Uwe Raabe);
  • Inlined '8Oo;,. ' variable;
  • Changed word to integer so that the loop variable can be negative, and then changed the loop range for I from 3..38 to -5..30 so that (I-8) could be replaced by I (suggested by Eric Grange);
  • Changed 1000 to 1e3 (suggested by Eric Grange)
  • Changed type Double to Real (suggested by Luc Neville via e-mail)
  • Removed brackets while((x*x+y*y<=4)and(t<1000))do --> while(x*x+y*y<=4)and(t<1000)do (suggested by Luc Neville via e-mail)
  • Removed spaces between numbers that were followed by a keyword (so that for j:=-5 to 30 do becomes for j:=-5to 30do(Suggested by Neville via e-mail)
  • Changed loop range to for j:=8to 40do, removing the negative sign, and changed integer back to word. Compensated this offset in the formula, by changing x * x - y * y + i / 16 - 2; to x * x - y * y + i / 16 - 3;

Unreadable version:

var n,x,y:Real;t,i,j:Word;begin for j:=8to 40do begin for i:=8to 65do begin x:=0;y:=0;t:=0;while(x*x+y*y<=4)and(t<1e3)do begin n:=x*x-y*y+i/16-3;y:=2*x*y+j/12-2;x:=n;inc(t)end;Write('8Oo;,. '[t mod 7+1])end;WriteLn;end;end.

Formatted code:

var
  n, x, y: Real;
  t, i, j: Word;
begin
  for j := 8 to 40 do
  begin
    for i := 8 to 65 do
    begin
      x := 0;
      y := 0;
      t := 0;
      while (x * x + y * y <= 4) and (t < 1e3) do
      begin
        n := x * x - y * y + i / 16 - 3;
        y := 2 * x * y + j / 12 - 2;
        x := n;
        inc(t)
      end;
      Write('8Oo;,. '[t mod 7 + 1])
    end;
    WriteLn;
  end;
end.

Output:

OOOOOOOOOOOOOOOOOooooooooooooooooooooooooooooooooooooooooo
OOOOOOOOOOOOOOOOooooooooo;;;;;;;;;;;;;;;oooooooooooooooooo
OOOOOOOOOOOOOOOoooooo;;;;;;;;;;;;;;,,,,,;;;;oooooooooooooo
OOOOOOOOOOOOOOooooo;;;;;;;;;;;;;,,,, ; .,,,;;;oooooooooooo
OOOOOOOOOOOOOoooo;;;;;;;;;;;;;,,,,.. O   ,,,,;;;oooooooooo
OOOOOOOOOOOOooo;;;;;;;;;;;;;,,,,,...8oo,8..,,,;;;;oooooooo
OOOOOOOOOOOooo;;;;;;;;;;;;,,,,,,.. Oo. ; 8...,,;;;;ooooooo
OOOOOOOOOOOoo;;;;;;;;;;;;,,,,,.   8OO    O8 ..,,;;;;oooooo
OOOOOOOOOOoo;;;;;;;;;;;,,,,..  O,oO8oo ,OOoO88,.,;;;;ooooo
OOOOOOOOOOo;;;;;;;;;;,,,....  o. .         OOO  ,;;;;;oooo
OOOOOOOOOO;;;;;;;;;,,......  o.,              o .,;;;;;ooo
OOOOOOOOOo;;;;;;,,. o     88O.                ;O.,;;;;;ooo
OOOOOOOOO;;;,,,,,.. ,,o;,oOo;                  o ,,;;;;;oo
OOOOOOOOO;,,,,,... 8o;   8;,                    .,,;;;;;oo
OOOOOOOOO,,,,,... Oo       .                   8.,,;;;;;oo
OOOOOOOOO,.,.  88oo o       ,                 , .,,;;;;;oo
OOOOOOOO                                     ,8 .,,;;;;;;o
OOOOOOOOO,.,.  88oo o       ,                 , .,,;;;;;oo
OOOOOOOOO,,,,,... Oo       .                   8.,,;;;;;oo
OOOOOOOOO;,,,,,... 8o;   8;,                    .,,;;;;;oo
OOOOOOOOO;;;,,,,,.. ,,o;,oOo;                  o ,,;;;;;oo
OOOOOOOOOo;;;;;;,,. o     88O.                ;O.,;;;;;ooo
OOOOOOOOOO;;;;;;;;;,,......  o.,              o .,;;;;;ooo
OOOOOOOOOOo;;;;;;;;;;,,,....  o. .         OOO  ,;;;;;oooo
OOOOOOOOOOoo;;;;;;;;;;;,,,,..  O,oO8oo ,OOoO88,.,;;;;ooooo
OOOOOOOOOOOoo;;;;;;;;;;;;,,,,,.   8OO    O8 ..,,;;;;oooooo
OOOOOOOOOOOooo;;;;;;;;;;;;,,,,,,.. Oo. ; 8...,,;;;;ooooooo
OOOOOOOOOOOOooo;;;;;;;;;;;;;,,,,,...8oo,8..,,,;;;;oooooooo
OOOOOOOOOOOOOoooo;;;;;;;;;;;;;,,,,.. O   ,,,,;;;oooooooooo
OOOOOOOOOOOOOOooooo;;;;;;;;;;;;;,,,, ; .,,,;;;oooooooooooo
OOOOOOOOOOOOOOOoooooo;;;;;;;;;;;;;;,,,,,;;;;oooooooooooooo
OOOOOOOOOOOOOOOOooooooooo;;;;;;;;;;;;;;;oooooooooooooooooo
OOOOOOOOOOOOOOOOOooooooooooooooooooooooooooooooooooooooooo
猫腻 2024-09-05 13:16:58

T-SQL(421 408 个字符)

declare @s varchar(max);
with
x(x)as(select CAST(-2.0 as real)union all select x+.01 from x where x<1),
y(y)as(select CAST(-1.1 as real)union all select y+.02 from y where y<1.1),
g(x,y,u,v,g)as(select x,y,x*0,y*0,0 from x,y union all select x,y,u*u-v*v+x,2*u*v+y,g+1 from g where g<24and u*u+v*v<4),
m(x,y,v)as(select x,y,case when x<-1.99 then char(10)end+substring(' .-''^"+oiexp**##$$§§%%%@',COUNT(*),1)from g group by x,y)
select @s=@s+v from m order by y,x option(maxrecursion 400)
select @s

输出在这里:

T-SQL (421 408 characters)

declare @s varchar(max);
with
x(x)as(select CAST(-2.0 as real)union all select x+.01 from x where x<1),
y(y)as(select CAST(-1.1 as real)union all select y+.02 from y where y<1.1),
g(x,y,u,v,g)as(select x,y,x*0,y*0,0 from x,y union all select x,y,u*u-v*v+x,2*u*v+y,g+1 from g where g<24and u*u+v*v<4),
m(x,y,v)as(select x,y,case when x<-1.99 then char(10)end+substring(' .-''^"+oiexp**##$$§§%%%@',COUNT(*),1)from g group by x,y)
select @s=@s+v from m order by y,x option(maxrecursion 400)
select @s

Output is here:

極樂鬼 2024-09-05 13:16:58

awk - 134 135 字符

ASCII 艺术,完全兼容。 python 参考实现的相当简单的翻译:

{for(j=-2;j<=2;j+=4/$2){for(i=-3;i<=1;i+=4/$1){for(x=y=k=0;x*x+y*y<=4&&++k<4^5;)
{t=x*x-y*y+i;y=2*x*y+j;x=t}printf"%d",log(k)}print""}}

测试运行

$ awk -f mandel.awk 
10 10
00000000000
00000000000
00000111000
00001116110
00011166610
00046666611
00011166610
00001116110
00000111000
00000000000
00000000000

,这是一个 VT-100 艺术。将 xterm 设置为“不可读”字符,然后尝试 400x200 设置:

{for(j=-2;j<=2;j+=4/$2){for(i=-3;i<=1;i+=4/$1){for(x=y=k=0;x*x+y*y<=4&&++k<1000;)
{t=x*x-y*y+i;y=2*x*y+j;x=t}printf"\x1b[4%dm ",log(k)}print"\x1b[0m"}}

awk - 134 135 chars

ASCII art, fully compliant. Fairly straightforward translation of the python reference implementation:

{for(j=-2;j<=2;j+=4/$2){for(i=-3;i<=1;i+=4/$1){for(x=y=k=0;x*x+y*y<=4&&++k<4^5;)
{t=x*x-y*y+i;y=2*x*y+j;x=t}printf"%d",log(k)}print""}}

test run

$ awk -f mandel.awk 
10 10
00000000000
00000000000
00000111000
00001116110
00011166610
00046666611
00011166610
00001116110
00000111000
00000000000
00000000000

and this is a VT-100 art. Set the xterm to "unreadable" characters, then try a 400x200 set:

{for(j=-2;j<=2;j+=4/$2){for(i=-3;i<=1;i+=4/$1){for(x=y=k=0;x*x+y*y<=4&&++k<1000;)
{t=x*x-y*y+i;y=2*x*y+j;x=t}printf"\x1b[4%dm ",log(k)}print"\x1b[0m"}}
情愿 2024-09-05 13:16:58

Haskell(162 个字符)

一个(相当)直接的 ASCII 艺术版本:

c(x,y)|x*x+y*y<4.0='x'
 |True='.'
i v w=c(iterate s(v,w)!!1000)where s(x,y)=(x*x-y*y+v,2*x*y+w)
main=mapM_ putStrLn[[i x y|x<-[-1.5,-1.45..0.5]]|y<-[1,0.9.. -1]]

输出:

$ ./a.out
.........................................
.........................................
..........................xxxx...........
..........................xxxx...........
....................x..xxxxxxxxxx........
....................xxxxxxxxxxxxxxxxx....
..................xxxxxxxxxxxxxxxxxxx....
.................xxxxxxxxxxxxxxxxxxxxx...
.......xxxxxxx..xxxxxxxxxxxxxxxxxxxxxx...
......xxxxxxxxx.xxxxxxxxxxxxxxxxxxxxxx...
..xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.....
......xxxxxxxxx.xxxxxxxxxxxxxxxxxxxxxx...
.......xxxxxxx..xxxxxxxxxxxxxxxxxxxxxx...
.................xxxxxxxxxxxxxxxxxxxxx...
..................xxxxxxxxxxxxxxxxxxx....
....................xxxxxxxxxxxxxxxxx....
....................x..xxxxxxxxxx........
..........................xxxx...........
..........................xxxx...........
.........................................
.........................................

更可读的版本:

outchar (x,y)
   | x*x + y*y < 4.0 = 'x'
   | otherwise       = '.'

calc v w = outchar (iterate step (v,w) !! 1000)
   where step (x,y) = (x*x - y*y + v, 2*x*y + w)

main = mapM_ putStrLn [[calc x y
                        | x <- [-1.5, -1.45 .. 0.5]]
                       | y <- [1, 0.9 .. -1]]

Haskell (162 characters)

A (fairly) straight forward ASCII art version:

c(x,y)|x*x+y*y<4.0='x'
 |True='.'
i v w=c(iterate s(v,w)!!1000)where s(x,y)=(x*x-y*y+v,2*x*y+w)
main=mapM_ putStrLn[[i x y|x<-[-1.5,-1.45..0.5]]|y<-[1,0.9.. -1]]

Output:

$ ./a.out
.........................................
.........................................
..........................xxxx...........
..........................xxxx...........
....................x..xxxxxxxxxx........
....................xxxxxxxxxxxxxxxxx....
..................xxxxxxxxxxxxxxxxxxx....
.................xxxxxxxxxxxxxxxxxxxxx...
.......xxxxxxx..xxxxxxxxxxxxxxxxxxxxxx...
......xxxxxxxxx.xxxxxxxxxxxxxxxxxxxxxx...
..xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.....
......xxxxxxxxx.xxxxxxxxxxxxxxxxxxxxxx...
.......xxxxxxx..xxxxxxxxxxxxxxxxxxxxxx...
.................xxxxxxxxxxxxxxxxxxxxx...
..................xxxxxxxxxxxxxxxxxxx....
....................xxxxxxxxxxxxxxxxx....
....................x..xxxxxxxxxx........
..........................xxxx...........
..........................xxxx...........
.........................................
.........................................

More readable version:

outchar (x,y)
   | x*x + y*y < 4.0 = 'x'
   | otherwise       = '.'

calc v w = outchar (iterate step (v,w) !! 1000)
   where step (x,y) = (x*x - y*y + v, 2*x*y + w)

main = mapM_ putStrLn [[calc x y
                        | x <- [-1.5, -1.45 .. 0.5]]
                       | y <- [1, 0.9 .. -1]]
青春有你 2024-09-05 13:16:58

Python 197 162

def m():
    for a in range(900):
        c=complex(a%30*.1-2,a/30*.1-1.5);z,i=c-c,0
        while i<28 and abs(z)<2:z,i=z*z+c,i+1
        if a%30==0:print
        print ".-+oxawOX "[i/3],

我对如何消除 python 中的空白感兴趣。

Python 197 162

def m():
    for a in range(900):
        c=complex(a%30*.1-2,a/30*.1-1.5);z,i=c-c,0
        while i<28 and abs(z)<2:z,i=z*z+c,i+1
        if a%30==0:print
        print ".-+oxawOX "[i/3],

I'm interested in how you get rid of the white space in python.

雪若未夕 2024-09-05 13:16:58

汇编器

.COM 可执行文件 = 140 字节

源 = 652 字节

mov ax,13h
int 10h
mov bx,200
xor di,di
mov ax,0a000h
mov es,ax
l1:mov bp,320
mov [si],bx
fild w[si]
fisub w[v100]
fidiv w[v50]
l2:mov cx,1000
mov [si],bp
fild w[106h]
fisub w[si]
fidiv w[v80]
fld1
fsubp
fldz
fldz
l3:fld st(1)
fmul st(0)
fld st(1)
fmul st(0)
fld st(0)
fadd st(2)
fcomp d[v4]
fstsw ax
test ah,45h
jz l4
fsubp
fadd st(3)
fxch st(2)
fild w[v2]
fmulp st(1)
fmulp st(1)
fadd st(3)
loop l3
mov al,255
l5:fcompp
fcomp
stosb
dec bp
jnz l2
fcomp
dec bx
jnz l1
mov ah,7
int 21h
mov ax,3
int 10h
ret
l4:mov ax,-10
mul cx
fcompp
jmp l5
v100:dw 100
v50:dw 80
v80:dw 90
v4:dd 4.0
v2:dw 2

Assembler

.COM Executable = 140 bytes

Source = 652 bytes

mov ax,13h
int 10h
mov bx,200
xor di,di
mov ax,0a000h
mov es,ax
l1:mov bp,320
mov [si],bx
fild w[si]
fisub w[v100]
fidiv w[v50]
l2:mov cx,1000
mov [si],bp
fild w[106h]
fisub w[si]
fidiv w[v80]
fld1
fsubp
fldz
fldz
l3:fld st(1)
fmul st(0)
fld st(1)
fmul st(0)
fld st(0)
fadd st(2)
fcomp d[v4]
fstsw ax
test ah,45h
jz l4
fsubp
fadd st(3)
fxch st(2)
fild w[v2]
fmulp st(1)
fmulp st(1)
fadd st(3)
loop l3
mov al,255
l5:fcompp
fcomp
stosb
dec bp
jnz l2
fcomp
dec bx
jnz l1
mov ah,7
int 21h
mov ax,3
int 10h
ret
l4:mov ax,-10
mul cx
fcompp
jmp l5
v100:dw 100
v50:dw 80
v80:dw 90
v4:dd 4.0
v2:dw 2
梨涡 2024-09-05 13:16:58

JavaScript(168/174 个字符)

Rhino 版本(168 个字符):

m=1000;for(i=0;i<40;i++){l=[];for(j=0;j<80;j++){v=j/80*3-2,w=2-i/40*3,x=y=c=0;while(x*x+y*y<5&&c<m)t=x*x-y*y+v,y=2*x*y+w,x=t,c++;l.push(c==m?' ':'-')}print(l.join(''))}

Firebug 版本(174 个字符):

m=1000;for(i=0;i<40;i++){l=[];for(j=0;j<80;j++){v=j/80*3-2,w=2-i/40*3,x=y=c=0;while(x*x+y*y<5&&c<m)t=x*x-y*y+v,y=2*x*y+w,x=t,c++;l.push(c==m?' ':'-')}console.log(l.join(''))}

JavaScript (168/174 characters)

Rhino version (168 characters):

m=1000;for(i=0;i<40;i++){l=[];for(j=0;j<80;j++){v=j/80*3-2,w=2-i/40*3,x=y=c=0;while(x*x+y*y<5&&c<m)t=x*x-y*y+v,y=2*x*y+w,x=t,c++;l.push(c==m?' ':'-')}print(l.join(''))}

Firebug version (174 characters):

m=1000;for(i=0;i<40;i++){l=[];for(j=0;j<80;j++){v=j/80*3-2,w=2-i/40*3,x=y=c=0;while(x*x+y*y<5&&c<m)t=x*x-y*y+v,y=2*x*y+w,x=t,c++;l.push(c==m?' ':'-')}console.log(l.join(''))}
御守 2024-09-05 13:16:58

还有一个 dc 条目 - 152 个字符

我在 dc

Ak?4r/sa4r/sbA00sm[0*]sG_2sj[_3si[0ddsxsysk[lxd*lyd*-li+2lxly**lj+sysx1lxd*lyd*+
4!>G1lk1+dsklm<G*1=L]dsLx0klk1-vvnAklila+dsi1!<I]dsIxAPljlb+dsj2!<J]dsJx

测试运行中通常的疯狂:

bash-3.2$ dc -f mandel.dc 
10 20
000000000000000000000
000000000011111111111
000000001111111111111
000000111111115211111
000000111111555551111
000000555555555551111
000000111111555551111
000000111111115211111
000000001111111111111
000000000011111111111
000000000000000000000

And a dc entry - 152 chars

My usual madness in dc

Ak?4r/sa4r/sbA00sm[0*]sG_2sj[_3si[0ddsxsysk[lxd*lyd*-li+2lxly**lj+sysx1lxd*lyd*+
4!>G1lk1+dsklm<G*1=L]dsLx0klk1-vvnAklila+dsi1!<I]dsIxAPljlb+dsj2!<J]dsJx

test run:

bash-3.2$ dc -f mandel.dc 
10 20
000000000000000000000
000000000011111111111
000000001111111111111
000000111111115211111
000000111111555551111
000000555555555551111
000000111111555551111
000000111111115211111
000000001111111111111
000000000011111111111
000000000000000000000
抽个烟儿 2024-09-05 13:16:58

Ruby - 139 个字符(Twitter 友好)

b,a=$*;a.to_i.times{|l|puts (0..b.to_i).map{|n|x=y=i=0;(x,y,i=x*x-y*y+n/38.0-1.5,2*x*y+l/14.0-1,i+1)until(x*x+y*y>4||i>78);(32+i).chr}*""}

$ ruby​​ mandelbrot.rb 80 32

或作为单个命令行(134 个字符,包括 红宝石-e)

ruby -e "32.times{|l|puts (0..78).map{|n|x=y=i=0;(x,y,i=x*x-y*y+n/38.0-1.5,2*x*y+l/14.0-1,i+1)until(x*x+y*y>4||i>78);(32+i).chr}*''}"
"###############################$$$$$$%%%%&&')*/))*.o&%$$$$########"""""
############################$$$$$$$%%%%%&''((*-G,*('&&%%$$$$$########""
#########################$$$$$$$%%%%%&&'929-03ooL.+0:'&%%%%%$$$$########
#####################$$$$$$$%%%&&&&&''()-1oooooooo/*('&&%%%%%%%$$$######
##################$$$$$$%%%&'''''''''())*,6oooooooo.*)((''&&&&&&&&%$$#####
###############$$$$$%%%%%%&&'(+3:0.,++ooE6ooooooooooo?8o1j?8)(((()4/'&%$$###
###########$$$%%%%%%%%%%&&&&''(*1>ooo8oooooooooooooooooooooooo.3oooD/,'%%$$##
#######$$%%%%%%%%%%%%%&&&&&&')8,,17ooooooooooooooooooooooooooooooooQ0)'&&%%$$#
$$$%&)(&&&&&&&&&&&&&&&'''((*18oooooooooooooooooooooooooooooooooooool*(('&%%$$
$%%%%&&*2*))((((25*(('''((()+F>=oooooooooooooooooooooooooooooooooooooooo1.(%%$$
%%%%%&&'()*,1o47//5o5/?,+***+3oooooooooooooooooooooooooooooooooooooooooo.)(&%%$$
%%%%&'''(*+-3ooooooooooom//-.oooooooooooooooooooooooooooooooooooooooooooo9+&%%$$
&&(*+)))+-?ooooooooooooooooL5ooooooooooooooooooooooooooooooooooooooooooo8)&%%%$$
'()*-X=U95ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo*'&&%%%$$
ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo:.+)('&&%%%$$
'()*-X=U95ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo*'&&%%%$$
&&(*+)))+-?ooooooooooooooooL5ooooooooooooooooooooooooooooooooooooooooooo8)&%%%$$
%%%%&'''(*+-3ooooooooooom//-.oooooooooooooooooooooooooooooooooooooooooooo9+&%%$$
%%%%%&&'()*,1o47//5o5/?,+***+3oooooooooooooooooooooooooooooooooooooooooo.)(&%%$$
$%%%%&&*2*))((((25*(('''((()+F>=oooooooooooooooooooooooooooooooooooooooo1.(%%$$
$$$%&)(&&&&&&&&&&&&&&&'''((*18oooooooooooooooooooooooooooooooooooool*(('&%%$$
#######$$%%%%%%%%%%%%%&&&&&&')8,,17ooooooooooooooooooooooooooooooooQ0)'&&%%$$#
###########$$$%%%%%%%%%%&&&&''(*1>ooo8oooooooooooooooooooooooo.3oooD/,'%%$$##
###############$$$$$%%%%%%&&'(+3:0.,++ooE6ooooooooooo?8o1j?8)(((()4/'&%$$###
##################$$$$$$%%%&'''''''''())*,6oooooooo.*)((''&&&&&&&&%$$#####
#####################$$$$$$$%%%&&&&&''()-1oooooooo/*('&&%%%%%%%$$$######
#########################$$$$$$$%%%%%&&'929-03ooL.+0:'&%%%%%$$$$########
############################$$$$$$$%%%%%&''((*-G,*('&&%%$$$$$########""
"###############################$$$$$$%%%%&&')*/))*.o&%$$$$########"""""
"""""###############################$$$$$%%%&'*)'&&%%%$$$$#######"""""""""
"""""""""#################################$$$$%%%%%$$$#########"""""""""""""
""""""""""""""""#############################################""""""""""""""""""""

Ruby - 139 chars (Twitter-friendly)

b,a=$*;a.to_i.times{|l|puts (0..b.to_i).map{|n|x=y=i=0;(x,y,i=x*x-y*y+n/38.0-1.5,2*x*y+l/14.0-1,i+1)until(x*x+y*y>4||i>78);(32+i).chr}*""}

$ ruby mandelbrot.rb 80 32

or as a single command line (134 chars including ruby -e)

ruby -e "32.times{|l|puts (0..78).map{|n|x=y=i=0;(x,y,i=x*x-y*y+n/38.0-1.5,2*x*y+l/14.0-1,i+1)until(x*x+y*y>4||i>78);(32+i).chr}*''}"
"###############################$$$$$$%%%%&&')*/))*.o&%$$$$########"""""
############################$$$$$$$%%%%%&''((*-G,*('&&%%$$$$$########""
#########################$$$$$$$%%%%%&&'929-03ooL.+0:'&%%%%%$$$$########
#####################$$$$$$$%%%&&&&&''()-1oooooooo/*('&&%%%%%%%$$$######
##################$$$$$$%%%&'''''''''())*,6oooooooo.*)((''&&&&&&&&%$$#####
###############$$$$$%%%%%%&&'(+3:0.,++ooE6ooooooooooo?8o1j?8)(((()4/'&%$$###
###########$$$%%%%%%%%%%&&&&''(*1>ooo8oooooooooooooooooooooooo.3oooD/,'%%$$##
#######$$%%%%%%%%%%%%%&&&&&&')8,,17ooooooooooooooooooooooooooooooooQ0)'&&%%$$#
$$$%&)(&&&&&&&&&&&&&&&'''((*18oooooooooooooooooooooooooooooooooooool*(('&%%$$
$%%%%&&*2*))((((25*(('''((()+F>=oooooooooooooooooooooooooooooooooooooooo1.(%%$$
%%%%%&&'()*,1o47//5o5/?,+***+3oooooooooooooooooooooooooooooooooooooooooo.)(&%%$$
%%%%&'''(*+-3ooooooooooom//-.oooooooooooooooooooooooooooooooooooooooooooo9+&%%$$
&&(*+)))+-?ooooooooooooooooL5ooooooooooooooooooooooooooooooooooooooooooo8)&%%%$$
'()*-X=U95ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo*'&&%%%$$
ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo:.+)('&&%%%$$
'()*-X=U95ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo*'&&%%%$$
&&(*+)))+-?ooooooooooooooooL5ooooooooooooooooooooooooooooooooooooooooooo8)&%%%$$
%%%%&'''(*+-3ooooooooooom//-.oooooooooooooooooooooooooooooooooooooooooooo9+&%%$$
%%%%%&&'()*,1o47//5o5/?,+***+3oooooooooooooooooooooooooooooooooooooooooo.)(&%%$$
$%%%%&&*2*))((((25*(('''((()+F>=oooooooooooooooooooooooooooooooooooooooo1.(%%$$
$$$%&)(&&&&&&&&&&&&&&&'''((*18oooooooooooooooooooooooooooooooooooool*(('&%%$$
#######$$%%%%%%%%%%%%%&&&&&&')8,,17ooooooooooooooooooooooooooooooooQ0)'&&%%$$#
###########$$$%%%%%%%%%%&&&&''(*1>ooo8oooooooooooooooooooooooo.3oooD/,'%%$$##
###############$$$$$%%%%%%&&'(+3:0.,++ooE6ooooooooooo?8o1j?8)(((()4/'&%$$###
##################$$$$$$%%%&'''''''''())*,6oooooooo.*)((''&&&&&&&&%$$#####
#####################$$$$$$$%%%&&&&&''()-1oooooooo/*('&&%%%%%%%$$$######
#########################$$$$$$$%%%%%&&'929-03ooL.+0:'&%%%%%$$$$########
############################$$$$$$$%%%%%&''((*-G,*('&&%%$$$$$########""
"###############################$$$$$$%%%%&&')*/))*.o&%$$$$########"""""
"""""###############################$$$$$%%%&'*)'&&%%%$$$$#######"""""""""
"""""""""#################################$$$$%%%%%$$$#########"""""""""""""
""""""""""""""""#############################################""""""""""""""""""""
や莫失莫忘 2024-09-05 13:16:58

Python。 98 个字符。

for _ in range(526):z=0;exec"z=z*z+_%25*.1-2+_/25*.1j-1j;"*50;print[".#"[z.real<4],"\n"][_%25<1],

打印如下内容:

. . . . . . . . . . . . . . . . . . . # . . . . 
. . . . . . . . . . . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . . . # # . . . . . 
. . . . . . . . . . . . . . . . . # # . . . . . 
. . . . . . . . . . . . . . # . # # # # # . . . 
. . . . . . . . . . . . . . # # # # # # # # # . 
. . . . . . . . . . . . . # # # # # # # # # # . 
. . . . . . . . . . . . . # # # # # # # # # # . 
. . . . . . . . # # # . # # # # # # # # # # # . 
. . . . . . . # # # # # # # # # # # # # # # # . 
# # # # # # # # # # # # # # # # # # # # # # . . 
. . . . . . . # # # # # # # # # # # # # # # # . 
. . . . . . . . # # # . # # # # # # # # # # # . 
. . . . . . . . . . . . . # # # # # # # # # # . 
. . . . . . . . . . . . . # # # # # # # # # # . 
. . . . . . . . . . . . . . # # # # # # # # # . 
. . . . . . . . . . . . . . # . # # # # # . . . 
. . . . . . . . . . . . . . . . . # # . . . . . 
. . . . . . . . . . . . . . . . . # # . . . . . 
. . . . . . . . . . . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . . . . . # . . . . 

http://codepad.org/rNPrgQ3H

Python. 98 chars.

for _ in range(526):z=0;exec"z=z*z+_%25*.1-2+_/25*.1j-1j;"*50;print[".#"[z.real<4],"\n"][_%25<1],

Prints something like this:

. . . . . . . . . . . . . . . . . . . # . . . . 
. . . . . . . . . . . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . . . # # . . . . . 
. . . . . . . . . . . . . . . . . # # . . . . . 
. . . . . . . . . . . . . . # . # # # # # . . . 
. . . . . . . . . . . . . . # # # # # # # # # . 
. . . . . . . . . . . . . # # # # # # # # # # . 
. . . . . . . . . . . . . # # # # # # # # # # . 
. . . . . . . . # # # . # # # # # # # # # # # . 
. . . . . . . # # # # # # # # # # # # # # # # . 
# # # # # # # # # # # # # # # # # # # # # # . . 
. . . . . . . # # # # # # # # # # # # # # # # . 
. . . . . . . . # # # . # # # # # # # # # # # . 
. . . . . . . . . . . . . # # # # # # # # # # . 
. . . . . . . . . . . . . # # # # # # # # # # . 
. . . . . . . . . . . . . . # # # # # # # # # . 
. . . . . . . . . . . . . . # . # # # # # . . . 
. . . . . . . . . . . . . . . . . # # . . . . . 
. . . . . . . . . . . . . . . . . # # . . . . . 
. . . . . . . . . . . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . . . . . # . . . . 

http://codepad.org/rNPrgQ3H

狠疯拽 2024-09-05 13:16:58

C# - 330

277 如果您删除 using 语句并删除前导空格和行结尾

int c,i,j,w=300,h=300,m=50;
using(var b=new Bitmap(w,h)){
 for(i=0;i<w;i++){
  for(j=0;j<h;j++){
   double x0=4.0*(i-w/2)/w-1,y0=4.0*(j-h/2)/h,x=0.0,y=0.0;
   for(c=0;x*x+y*y<=4.0&&c<m;c++){
    var t=x*x-y*y+x0;
    y=2.0*x*y+y0;x=t;
   }
   int v=c==m?255:c*10%255;
   b.SetPixel(i,j,Color.FromArgb(v,v,v));
  }
 }
}

长远来看不会记录设置 - 但从 Python 示例源转换并压缩到超出可读范围很有趣。

C# - 330

277 if you drop the using statement and remove the leading whitespace and line endings

int c,i,j,w=300,h=300,m=50;
using(var b=new Bitmap(w,h)){
 for(i=0;i<w;i++){
  for(j=0;j<h;j++){
   double x0=4.0*(i-w/2)/w-1,y0=4.0*(j-h/2)/h,x=0.0,y=0.0;
   for(c=0;x*x+y*y<=4.0&&c<m;c++){
    var t=x*x-y*y+x0;
    y=2.0*x*y+y0;x=t;
   }
   int v=c==m?255:c*10%255;
   b.SetPixel(i,j,Color.FromArgb(v,v,v));
  }
 }
}

Not record setting by a long shot - but fun to convert from the Python sample source and compress down beyond readable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文