🙌

マンデルブロ集合

2020/12/21に公開

これは何ですか?

マンデルブロ集合を表示します。

ソースコード

program Mandelbrot;
const
  width  = 80;
  height = 20;
  fromX  = -2.5;
  toX    =  1.5;
  fromY  = -1.0;
  toY    =  1.0;
type
  complex = record r, i: real end;
var
  i, j: integer;
  x, y: real;

function cAdd(c1, c2: complex): complex;
begin
  cAdd.r := c1.r + c2.r;
  cAdd.i := c1.i + c2.i;
end;

function cMultiply(c1, c2: complex): complex;
begin
  cMultiply.r := c1.r * c2.r - c1.i * c2.i;
  cMultiply.i := c1.r * c2.i + c1.i * c2.r;
end;

function Mandel(x, y: real; count: integer): integer;
var
  z, d: complex;
  i: integer;
begin
  z.r := 0.0;
  z.i := 0.0;
  d.r := x;
  d.i := y;
  i := 0;
  while (i < count) and (sqr(z.r) + sqr(z.i) < 4.0) do begin
    z := cAdd(cMultiply(z, z), d);
    i := i + 1;
  end;
  Mandel := i;
end;

function Char64(i: integer): char;
const
  str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
begin
  if i < 64 then
    Char64 := str[i+1]
  else
    Char64 := ' ';
end;

begin
  for i := 0 to height do begin
    y := fromY + i * (toY - fromY) / height;
    for j := 0 to width do begin
      x := fromX + j * (toX - fromX) / width;
      write(Char64(Mandel(x, y, 64)));
    end;
    writeln;
  end;
end.

出力

BBBBBBBBBBBBBBBBCCCCCDDDDDDDDDDDDDDDDEEEEEEFFGHLJL FEEEEDDDDCCCCCCCCCCCCCCCCCCCCC
BBBBBBBBBBBBBBBCCCDDDDDDDDDDDDDDDDEEEEEEEFFFGIJkYLIGFFEEEEDDDDDCCCCCCCCCCCCCCCCCC
BBBBBBBBBBBBBBCCDDDDDDDDDDDDDDDDEEEEEEEFFGGHIO    SIGFFFFEEDDDDDDCCCCCCCCCCCCCCCC
BBBBBBBBBBBBBCCDDDDDDDDDDDDDDEEEEEEFFGHIIIIJKO    NKJIGGGSFEEDDDDDCCCCCCCCCCCCCCC
BBBBBBBBBBBBCCDDDDDDDDDDDDDEEEEFFFFGGHMq Qa          UMOPQPFEDDDDDDCCCCCCCCCCCCCC
BBBBBBBBBBBBCDDDDDDDDDDDEEFFFFFFFGGGIOM+                 UHGFEDDDDDDCCCCCCCCCCCCC
BBBBBBBBBBBCDDDDDDDEEEFFHMHHHHHHHHHILU                   RJTFEEDDDDDDCCCCCCCCCCCC
BBBBBBBBBBBDDDEEEEEEFFFGHKQUMNjNLJJLY                     PIGEEDDDDDDDCCCCCCCCCCC
BBBBBBBBBBBDEEEEEEFFFFGHILS       PQ                      fHFEEEDDDDDDCCCCCCCCCCC
BBBBBBBBBBBEEEEEGGGGHIMNOW         h                      IGFEEEDDDDDDCCCCCCCCCCC
BBBBBBBBBBB                                             MIHGFEEEDDDDDDCCCCCCCCCCC
BBBBBBBBBBBEEEEEGGGGHIMNOW         h                      IGFEEEDDDDDDCCCCCCCCCCC
BBBBBBBBBBBDEEEEEEFFFFGHILS       PQ                      fHFEEEDDDDDDCCCCCCCCCCC
BBBBBBBBBBBDDDEEEEEEFFFGHKQUMNjNLJJLY                     PIGEEDDDDDDDCCCCCCCCCCC
BBBBBBBBBBBCDDDDDDDEEEFFHMHHHHHHHHHILU                   RJTFEEDDDDDDCCCCCCCCCCCC
BBBBBBBBBBBBCDDDDDDDDDDDEEFFFFFFFGGGIOM+                 UHGFEDDDDDDCCCCCCCCCCCCC
BBBBBBBBBBBBCCDDDDDDDDDDDDDEEEEFFFFGGHMq Qa          UMOPQPFEDDDDDDCCCCCCCCCCCCCC
BBBBBBBBBBBBBCCDDDDDDDDDDDDDDEEEEEEFFGHIIIIJKO    NKJIGGGSFEEDDDDDCCCCCCCCCCCCCCC
BBBBBBBBBBBBBBCCDDDDDDDDDDDDDDDDEEEEEEEFFGGHIO    SIGFFFFEEDDDDDDCCCCCCCCCCCCCCCC
BBBBBBBBBBBBBBBCCCDDDDDDDDDDDDDDDDEEEEEEEFFFGIJkYLIGFFEEEEDDDDDCCCCCCCCCCCCCCCCCC
BBBBBBBBBBBBBBBBCCCCCDDDDDDDDDDDDDDDDEEEEEEFFGHLJL FEEEEDDDDCCCCCCCCCCCCCCCCCCCCC

Discussion