Advertisement
Guest User

mandelbrot

a guest
Feb 25th, 2020
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 1.49 KB | None | 0 0
  1. type Complex = Object
  2.   re: real;
  3.   im: real;
  4.   constructor init(r: real; i: real);
  5.   function add_complex(other: Complex): Complex;
  6.   function mul_complex(other: Complex): Complex;
  7.   function abs: real;
  8. end;
  9.  
  10. constructor Complex.init(r: real; i: real);
  11.  begin
  12.   re := r;
  13.   im := i;
  14.  end;
  15.  
  16. function Complex.add_complex(other: Complex): Complex;
  17.  var
  18.    t: Complex;
  19.  begin
  20.   t.init(re + other.re, im + other.im);
  21.   add_complex := t;
  22.  end;
  23.  
  24. function Complex.mul_complex(other: Complex): Complex;
  25.  var
  26.   t: Complex;
  27.  begin
  28.   t.init(re * other.re - im * other.im, re * other.im + im * other.re);
  29.   mul_complex := t;
  30.  end;
  31.  
  32. function Complex.abs: real;
  33.  begin
  34.   abs := sqrt(re * re + im * im);
  35.  end;
  36.  
  37. function mandelbrot_test(c: Complex): byte;
  38.  var
  39.   f: boolean;
  40.   i: byte;
  41.   n: Complex;
  42.  begin
  43.   i := 0;
  44.   f := true;
  45.   n.init(0, 0);
  46.   while (i < 255) and f do
  47.    begin
  48.     if n.abs() > 2 then
  49.       f := false
  50.     else
  51.       n := n.mul_complex(n).add_complex(c);
  52.     i := i + 1;
  53.    end;
  54.   mandelbrot_test := i;
  55.  end;
  56.  
  57. procedure mandelbrot(x1, y1, x2, y2: real; rx, ry, g: byte);
  58.  var
  59.   i, j: integer;
  60.   t: Complex;
  61.  begin
  62.   for i := 0 to ry do
  63.    begin
  64.     for j := 0 to rx do
  65.      begin
  66.       t.init(x1 + (x2 - x1) * j / rx, y1 + (y2 - y1) * i / ry);
  67.       if  mandelbrot_test(t) >= g then
  68.         write('*')
  69.       else
  70.         write(' ');
  71.      end;
  72.     writeln;
  73.    end;
  74.  end;
  75.  
  76. var
  77.  t: Complex;
  78. begin
  79.   mandelbrot(-2, -1, 1, 1, 80, 24, 255);
  80. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement