mandelbrot.pp 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. { The Computer Language Shootout
  2. http://shootout.alioth.debian.org
  3. contributed by Ales Katona
  4. modified by Vincent Snijders
  5. additional compiler options:
  6. i386: -Cfsse2
  7. x86_64: none
  8. }
  9. program mandelbrot;
  10. var n: longint;
  11. TextBuf: array[0..$FFF] of byte;
  12. OutFile: PText;
  13. procedure run;
  14. var
  15. Cy, Step: double;
  16. x, y, bits,bit: Longint;
  17. function CalculatePoint(Cx, Cy: double): boolean; inline;
  18. const
  19. Limit = 4;
  20. var
  21. i: longint;
  22. Zr, Zi, Ti, Tr: Double;
  23. begin
  24. Zr := 0; Zi := 0; Tr := 0; Ti := 0;
  25. for i := 1 to 50 do begin
  26. Zi := 2*Zr*Zi + Cy;
  27. Zr := Tr - Ti + Cx;
  28. Ti := Zi * Zi;
  29. Tr := Zr * Zr;
  30. if (Tr + Ti>=limit) then exit(true);
  31. end;
  32. CalculatePoint := false;
  33. end;
  34. begin
  35. Step := 2/n;
  36. for y := 0 to n-1 do
  37. begin
  38. Cy := y * Step - 1;
  39. bits := 255; bit := 128;
  40. for x := 0 to n-1 do
  41. begin
  42. if CalculatePoint(x * Step - 1.5, Cy) then
  43. bits := bits xor bit;
  44. if bit > 1 then
  45. bit := bit shr 1
  46. else
  47. begin
  48. write(OutFile^, chr(bits));
  49. bits := 255; bit := 128;
  50. end;
  51. end;
  52. if bit < 128 then write(OutFile^, chr(bits xor((bit shl 1)-1)));
  53. end;
  54. end;
  55. begin
  56. OutFile := @Output;
  57. SetTextBuf(OutFile^, TextBuf);
  58. Val(ParamStr(1), n);
  59. writeln(OutFile^, 'P4');
  60. writeln(OutFile^, n,' ',n);
  61. run;
  62. end.