1. //usr/bin/clang "$0" && exec ./a.out
    
  2. // mandelbrot
    
  3. #include <stdio.h>
    
  4. 
    
  5. int RESOLUTION_X = 150;
    
  6. int RESOLUTION_Y = 60;
    
  7. double OUTPUT_RES_X = 3.5;
    
  8. double OUTPUT_RES_Y = 2;
    
  9. double OUTPUT_OFFSET_X = -2.5;
    
  10. int OUTPUT_OFFSET_Y = -1;
    
  11. int MAX_ITERATIONS=27;
    
  12. 
    
  13. int calc(double xi, double yi) {
    
  14.     xi = xi/ RESOLUTION_X * OUTPUT_RES_X + OUTPUT_OFFSET_X;
    
  15.     yi = yi / RESOLUTION_Y * OUTPUT_RES_Y + OUTPUT_OFFSET_Y;
    
  16.     double x = 0.0;
    
  17.     double y = 0.0;
    
  18.     int iteration = 0;
    
  19.     while( x*x + y*y < 4 && iteration < MAX_ITERATIONS) {
    
  20.         double xtemp = x*x - y*y + xi;
    
  21.         y = 2*x*y + yi;
    
  22.         x = xtemp;
    
  23.         iteration += 1;
    
  24.     }
    
  25.     return iteration;
    
  26. }
    
  27. 
    
  28. char show(int value) {
    
  29.     return  value <= 26 ? ('A' + value - 1) : ' ';
    
  30. }
    
  31. 
    
  32. void draw_window(int w, int h) {
    
  33.   for (int yi = 0; yi < h; yi++) {
    
  34.     for (int xi = 0; xi < w; xi++) {
    
  35.         char value = show(calc(xi, yi));
    
  36.         printf("%c", value);
    
  37.     }
    
  38.     puts("");
    
  39.   }
    
  40. }
    
  41. 
    
  42. int main() {
    
  43.   draw_window(RESOLUTION_X, RESOLUTION_Y);
    
  44. }
    
  45.