mandelbrot.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /* Copyright 2019 Eric Bavier. Licensed under the GPLv3+ */
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <curses.h>
  5. #include <math.h>
  6. int max_iter = 1000;
  7. const char palette[] =
  8. {'.', ',', ':', ';', 'i', '+', '=', 'o', 'e', '*', 'l', 't', 'm', 'O', '&', '0', '8', '@', '#'};
  9. int
  10. main (int argc, char ** argv)
  11. {
  12. int WIDTH, HEIGHT;
  13. double cx = -0.8, cy = 0.0;
  14. double zoom = 1.0;
  15. /* Initialize curses terminal handling */
  16. WINDOW * win = initscr();
  17. cbreak(); noecho();
  18. intrflush (stdscr, FALSE);
  19. keypad (stdscr, TRUE);
  20. getmaxyx(win, HEIGHT, WIDTH);
  21. while (1)
  22. {
  23. clear();
  24. for (int py = 0; py < HEIGHT; ++py)
  25. {
  26. #pragma omp parallel for ordered schedule (dynamic)
  27. for (int px = 0; px < WIDTH; ++px)
  28. {
  29. double x0 = (3.5/zoom) * ((double)(px - WIDTH/2) / WIDTH) + cx;
  30. double y0 = (2.0/zoom) * ((double)(py - HEIGHT/2) / HEIGHT) +cy;
  31. double x = 0, y=0;
  32. int i = 0;
  33. for (; x*x + y*y <= 4.0 && i < max_iter; ++i)
  34. {
  35. double tmp = x*x - y*y + x0;
  36. y = 2*x*y + y0;
  37. x = tmp;
  38. }
  39. #pragma omp ordered
  40. {
  41. if (i == max_iter) addch (' ');
  42. else addch (palette[i % (sizeof(palette)-1)]);
  43. }
  44. }
  45. }
  46. move (HEIGHT-1,0);
  47. printw ("X: %.16f, Y: %.16f, zoom: %.10f, max: %d", cx, cy, zoom, max_iter);
  48. move (HEIGHT/2,WIDTH/2);
  49. refresh();
  50. /* See if the users wants to do anything else */
  51. int c = getch();
  52. switch (c){
  53. case KEY_DOWN: cy += 0.1 / zoom; break;
  54. case KEY_UP: cy -= 0.1 / zoom; break;
  55. case KEY_RIGHT: cx += 0.1 / zoom; break;
  56. case KEY_LEFT: cx -= 0.1 / zoom; break;
  57. case '+': zoom *= 1.1; max_iter *= 1.01; break;
  58. case '-': zoom /= 1.1; max_iter /= 1.01; break;
  59. case 'q': endwin(); exit(EXIT_SUCCESS);
  60. }
  61. }
  62. }