Advertisement
Chris_M_Thomasson

Complex Iteration Example in C

Nov 28th, 2015
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.16 KB | None | 0 0
  1. /* Crude, simple code for Sparky
  2.    Showing how to code complex numbers. This implments:
  3.    (Z^2 + ((RE(Z) + IM(Z)) / 2.1)) - 0.83
  4. _____________________________________________________________*/
  5.  
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <math.h>
  9. #include <bmpfile.h>
  10.  
  11.  
  12.  
  13. static bmpfile_t* g_bmp = NULL;
  14.  
  15.  
  16.  
  17.  
  18. /* A 2d point
  19. _____________________________________________________________*/
  20. struct ffe_point
  21. {
  22.     double x;
  23.     double y;
  24. };
  25.  
  26. struct ffe_point
  27. ffe_complex_add(
  28.     struct ffe_point n0,
  29.     struct ffe_point n1
  30. ){
  31.     struct ffe_point ret = { n0.x + n1.x, n0.y + n1.y };
  32.     return ret;
  33. }
  34.  
  35. struct ffe_point
  36. ffe_complex_add_real(
  37.     struct ffe_point n0,
  38.     double real
  39. ){
  40.     struct ffe_point ret = { n0.x + real, n0.y };
  41.     return ret;
  42. }
  43.  
  44. struct ffe_point
  45. ffe_complex_sub_real(
  46.     struct ffe_point n0,
  47.     double real
  48. ){
  49.     struct ffe_point ret = { n0.x - real, n0.y };
  50.     return ret;
  51. }
  52.  
  53. struct ffe_point
  54. ffe_complex_mul(
  55.     struct ffe_point n0,
  56.     struct ffe_point n1
  57. ){
  58.     struct ffe_point ret = {
  59.         n0.x * n1.x - n0.y * n1.y,
  60.         n0.x * n1.y + n0.y * n1.x
  61.     };
  62.  
  63.     return ret;
  64. }
  65.  
  66.  
  67.  
  68.  
  69. /* Axes describing a 2d plane
  70. _____________________________________________________________*/
  71. struct ffe_axes
  72. {
  73.     double xmin;
  74.     double xmax;
  75.     double ymin;
  76.     double ymax;
  77. };
  78.  
  79.  
  80.  
  81. /* Complex Iteration
  82. _____________________________________________________________*/
  83. void iterate_point(struct ffe_point const* c, unsigned int x, unsigned int y)
  84. {
  85.     struct ffe_point z = *c;
  86.  
  87.     /*(Z^2 + ((RE(Z) + IM(Z)) / 2.1)) - 0.83*/
  88.  
  89.     for (unsigned int i = 0; i < 256; ++i)
  90.     {
  91.         double temp = ((z.x + z.y) / 2.1);
  92.  
  93.         z = ffe_complex_mul(z, z);
  94.         z = ffe_complex_add_real(z, temp);
  95.         z = ffe_complex_sub_real(z, 0.83);
  96.  
  97.         double dis = sqrt(z.x * z.x + z.y * z.y);
  98.  
  99.         if (dis > 10.0)
  100.         {
  101.             rgb_pixel_t color = { 255, ((i + 1) * 5) % 255, 0, 0 };
  102.             bmp_set_pixel(g_bmp, x, y, color);
  103.             return;
  104.         }
  105.     }
  106.  
  107.     rgb_pixel_t color = { 255, 0, 0, 0 };
  108.     bmp_set_pixel(g_bmp, x, y, color);
  109. }
  110.  
  111.  
  112. void iterate_axes(struct ffe_axes const* axes, unsigned int width, unsigned int height)
  113. {
  114.     double xstep = (axes->xmax - axes->xmin) / (width - 1.0);
  115.     double ystep = (axes->ymax - axes->ymin) / (height - 1.0);
  116.  
  117.     for (unsigned int y = 0; y < height; ++y)
  118.     {
  119.         for (unsigned int x = 0; x < width; ++x)
  120.         {
  121.             struct ffe_point c = {
  122.                 axes->xmin + (x * xstep),
  123.                 axes->ymin + (y * ystep)
  124.             };
  125.  
  126.             iterate_point(&c, x, y);
  127.         }
  128.     }
  129. }
  130.  
  131.  
  132. int
  133. main(void)
  134. {  
  135.     #define OUTBMP "C:\\Users\\Chris\\Desktop\\hello_fractal.bmp"
  136.    
  137.     {
  138.         unsigned int width = 300;
  139.         unsigned int height = 300;
  140.  
  141.         g_bmp = bmp_create(width, height, 24);
  142.        
  143.         struct ffe_axes axes = { -2.0, 2.0, -2.0, 2.0 };
  144.  
  145.         iterate_axes(&axes, width, height);
  146.  
  147.         bmp_save(g_bmp, OUTBMP);
  148.  
  149.         bmp_destroy(g_bmp);
  150.     }
  151.  
  152.     puts("Done, hit <ENTER> to exit...");
  153.     getchar();
  154.  
  155.     return 0;
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement