mandelbrot.c 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /* $Source$
  2. * $State$
  3. * $Revision$
  4. */
  5. /* Adapted from code by Chris Losinger. (This is the non-obfuscated
  6. * version...
  7. *
  8. * http://www.codeproject.com/cpp/mandelbrot_obfuscation.asp
  9. */
  10. #include <stdlib.h>
  11. #include <unistd.h>
  12. enum
  13. {
  14. ROWS = 40,
  15. COLUMNS = 60,
  16. MAX_ITERATIONS = 255
  17. };
  18. void nl(void)
  19. {
  20. write(1, "\n", 1);
  21. }
  22. void out(int n)
  23. {
  24. const char* chars = "****++++++---- ";
  25. write(1, chars + (n/16), 1);
  26. }
  27. int main(int argc, const char* argv[])
  28. {
  29. /* Size and position of the visible area. */
  30. double view_r = -2.3, view_i = -1.0;
  31. double zoom = 0.05;
  32. int x, y, n;
  33. for (y=0; y < ROWS; y++)
  34. {
  35. double c_i = view_i + y * zoom;
  36. for (x=0; x < COLUMNS; x++)
  37. {
  38. double c_r = view_r + x*zoom;
  39. double z_r = c_r;
  40. double z_i = c_i;
  41. for (n=0; n < MAX_ITERATIONS; n++)
  42. {
  43. double z_r2 = z_r * z_r;
  44. double z_i2 = z_i * z_i;
  45. /* Have we escaped? */
  46. if (z_r2 + z_i2 > 4)
  47. break;
  48. /* z = z^2 + c */
  49. z_i = 2 * z_r * z_i + c_i;
  50. z_r = z_r2 - z_i2 + c_r;
  51. }
  52. out(n);
  53. }
  54. nl();
  55. }
  56. return 0;
  57. }