mandel.c 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. void *malloc();
  2. void *SDL_CreateWindow();
  3. void *SDL_CreateRenderer();
  4. int SDL_SetRenderDrawColor();
  5. int SDL_RenderDrawPoint();
  6. int SDL_RenderClear();
  7. int SDL_RenderPresent();
  8. int SDL_PollEvent();
  9. int SDL_DestroyRenderer();
  10. int SDL_DestroyWindow();
  11. int SDL_Quit();
  12. int SDL_Init();
  13. void *win;
  14. void *rnd;
  15. int W;
  16. int H;
  17. int *col;
  18. plot(int x, int y)
  19. {
  20. int n;
  21. int fx;
  22. int fy;
  23. int zx;
  24. int zy;
  25. int nx;
  26. int ny;
  27. fx = (x - W/2)*4000 / W;
  28. fy = (y - H/2)*4000 / H;
  29. zx = fx;
  30. zy = fy;
  31. for (n=0; n<200; n++) {
  32. if (zx*zx + zy*zy > 4000000)
  33. break;
  34. nx = (zx*zx)/1000 - (zy*zy)/1000 + fx;
  35. ny = zx*zy/500 + fy;
  36. zx = nx;
  37. zy = ny;
  38. }
  39. n = col[n];
  40. SDL_SetRenderDrawColor(rnd, 100, n, n, 255);
  41. SDL_RenderDrawPoint(rnd, x, y);
  42. return 0;
  43. }
  44. main() {
  45. int c;
  46. int n;
  47. int x;
  48. int y;
  49. void *e;
  50. int *ie;
  51. W = 800;
  52. H = 800;
  53. SDL_Init(32);
  54. win = SDL_CreateWindow("Mandelbrot MiniC", 0, 0, W, H, 0);
  55. rnd = SDL_CreateRenderer(win, -1, 0);
  56. e = malloc(56);
  57. ie = e;
  58. col = malloc(201 * sizeof (int));
  59. c = 20;
  60. for (n=0; n<200; n++) {
  61. col[n] = c;
  62. c = c + (255-c)/8;
  63. }
  64. col[n] = 30;
  65. SDL_RenderClear(rnd);
  66. for (x=0; x<W; x++)
  67. for (y=0; y<H; y++)
  68. plot(x, y);
  69. SDL_RenderPresent(rnd);
  70. for (;;) {
  71. if (SDL_PollEvent(e)) {
  72. if (ie[0] == 769)
  73. break;
  74. }
  75. }
  76. SDL_DestroyRenderer(rnd);
  77. SDL_DestroyWindow(win);
  78. SDL_Quit();
  79. }