Advertisement
Guest User

Julia set rendering code

a guest
May 20th, 2022
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.64 KB | None | 0 0
  1. /*
  2.  * Asks for a value of c, then prints the filled-in Julia set of f(z) = z^2 + c to the Windows terminal.
  3.  */
  4.  
  5. #define _CRT_SECURE_NO_WARNINGS
  6.  
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <math.h>
  10.  
  11. // The resolution of the grid. Make sure the terminal is wide enough to print this many characters.
  12. #define X_PIX 620
  13. #define Y_PIX 310
  14.  
  15. // The maximum number of iterations to run.
  16. // I find that increasing this value too greatly makes the output skinnier.
  17. #define N_MAX 100
  18.  
  19. // Struct for complex numbers, in rectangular form. z = re + im * I.
  20. struct Complex {
  21.     double re;
  22.     double im;
  23. };
  24.  
  25. // Squares a complex number.
  26. struct Complex square_complex(struct Complex z) {
  27.     struct Complex output = { z.re * z.re - z.im * z.im, 2.00 * z.re * z.im };
  28.     return output;
  29. }
  30.  
  31. // Calculates the modulus of a complex number.
  32. double modulus(struct Complex z) {
  33.     return sqrt(z.re * z.re + z.im * z.im);
  34. }
  35.  
  36. // Iterates a complex number through the function f(z) = z^2 + c.
  37. struct Complex iterate(struct Complex z, struct Complex c) {
  38.     struct Complex output = square_complex(z);
  39.     output.re += c.re;
  40.     output.im += c.im;
  41.     return output;
  42. }
  43.  
  44. int main(void) {
  45.     // Initialize c and prompt the user for its real and imaginary parts.
  46.     struct Complex c;
  47.  
  48.     printf("Enter the real part of c: ");
  49.     scanf("%lf", &c.re);
  50.  
  51.     printf("\nEnter the imaginary part of c: ");
  52.     scanf("%lf", &c.im);
  53.  
  54.     // The value rad is R, the threshold for determining whether the sequence |A_n| is bounded.
  55.     double rad = 0.5 + 0.5 * sqrt(1 + 4 * modulus(c));
  56.  
  57.     printf("\n");
  58.  
  59.     double re, im;
  60.  
  61.     // Set up a nested for loop.
  62.     for (int y = 1; y < Y_PIX; y++) {
  63.         for (int x = 1; x < X_PIX; x++) {
  64.  
  65.             // Calculate the real and imaginary parts of the complex plane at the point (x, y) according to the grid resolution.
  66.             // The grid displayed is from -rad to rad on the real and imaginary axes. Points outside of this area are guaranteed to not be in the filled-in Julia set.
  67.             re = rad * -1.0 + ((rad *  2.0) / (double)X_PIX * x);
  68.             im = rad *  1.0 + ((rad * -2.0) / (double)Y_PIX * y);
  69.  
  70.             struct Complex z = { re, im };
  71.  
  72.             // Iterate z through the function f(z) = z^2 + c up to n_MAX times, stopping as soon as we find a term whose distance from the origin is further than rad.
  73.             int iter = 0;
  74.  
  75.             while (iter < N_MAX && modulus(z) <= rad) {
  76.                 z = iterate(z, c);
  77.  
  78.                 iter++;
  79.             }
  80.  
  81.             if (iter == N_MAX) {
  82.                 // The point is likely in the filled-in Julia set. Print a $.
  83.                 printf("$");
  84.             }
  85.             else {
  86.                 // The point is not in the filled-in Julia set. Print a period.
  87.                 printf(".");
  88.             }
  89.  
  90.         }
  91.         printf("\n");
  92.     }
  93.  
  94.     system("PAUSE");
  95.     return 0;
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement