Chris_M_Thomasson

Experimental DLA Simulation

Mar 31st, 2016
396
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.01 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #include <bmpfile.h>
  5.  
  6.  
  7.  
  8.  
  9. #define PI 3.141592653589793
  10. #define PI2 (PI * 2.0)
  11. #define RAND_MOD(mp_mod) (rand() % (mp_mod))
  12. #define RAND_FLOAT() (rand() / (RAND_MAX - 0.0))
  13. #define RAND_RANGE(mp_base, mp_mod) ((mp_base) + (int)RAND_MOD(((mp_mod) + 1)))
  14.  
  15.  
  16. #define RGBA(mp_r, mp_g, mp_b, mp_a) { (mp_b), (mp_g), (mp_r), (mp_a) }
  17. #define RGB(mp_r, mp_g, mp_b) RGBA(mp_r, mp_g, mp_b, 255)
  18.  
  19.  
  20.  
  21.  
  22. struct point2d
  23. {
  24.     double x;
  25.     double y;
  26. };
  27.  
  28.  
  29. struct point2d
  30. point2d_get_scaled(
  31.     struct point2d p0,
  32.     struct point2d p1,
  33.     double scale
  34. ){
  35.     struct point2d spt = {
  36.         p0.x + (p1.x - p0.x) * scale,
  37.         p0.y + (p1.y - p0.y) * scale
  38.     };
  39.  
  40.     return spt;
  41. }
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49. struct dla_point
  50. {
  51.     int x;
  52.     int y;
  53. };
  54.  
  55.  
  56.  
  57.  
  58. struct dla
  59. {
  60.     bmpfile_t* bmp;
  61.     unsigned int width;
  62.     unsigned int height;
  63. };
  64.  
  65.  
  66. int
  67. dla_create(
  68.     struct dla* const self,
  69.     unsigned int width,
  70.     unsigned int height
  71. ){
  72.     if ((self->bmp = bmp_create(width, height, 24)))
  73.     {
  74.         // Set background to black
  75.         for (unsigned int y = 0; y < height; ++y)
  76.         {
  77.             for (unsigned int x = 0; x < width; ++x)
  78.             {
  79.                 bmp_set_pixel(self->bmp, x, y, (rgb_pixel_t)RGB(0, 0, 0));
  80.             }
  81.         }
  82.  
  83.         self->width = width;
  84.         self->height = height;
  85.  
  86.         return 1;
  87.     }
  88.  
  89.     return 0;
  90. }
  91.  
  92.  
  93. void
  94. dla_destroy(
  95.     struct dla* const self
  96. ){
  97.     bmp_destroy(self->bmp);
  98. }
  99.  
  100.  
  101. struct dla_point
  102. dla_walk(
  103.     struct dla const* const self,
  104.     struct dla_point pt
  105. ){
  106.     int rx = RAND_RANGE(-1, 2);
  107.     int ry = RAND_RANGE(-1, 2);
  108.  
  109.     pt.x += rx;
  110.     pt.y += ry;
  111.  
  112.     if (pt.x < 0) self->width - (1 + RAND_MOD(2));
  113.     if (pt.y < 0) self->height - (1 + RAND_MOD(2));
  114.  
  115.     if (pt.x >= self->width) pt.x = RAND_MOD(2);
  116.     if (pt.y >= self->height) pt.y = RAND_MOD(2);
  117.  
  118.     return pt;
  119. }
  120.  
  121.  
  122. rgb_pixel_t*
  123. dla_check(
  124.     struct dla const* const self,
  125.     struct dla_point pt
  126. ){
  127.  
  128.     if (RAND_MOD(128) > 1)
  129.     {
  130.         int dx = 0;//(RAND_MOD(2) < 1) ? -1 : 1;
  131.         int dy = 0;//(RAND_MOD(2) < 1) ? -1 : 1;
  132.  
  133.         rgb_pixel_t* p = bmp_get_pixel(self->bmp, pt.x + dx, pt.y + dy);
  134.         if (p && p->alpha == 254)
  135.         {
  136.             //printf("FOUND!\n");
  137.             return p;
  138.         }
  139.         return NULL;
  140.     }
  141.  
  142.     int xw = 3;
  143.     int yw = 3;
  144.  
  145.     struct dla_point z = {
  146.         pt.x - 1,
  147.         pt.y - 1
  148.     };
  149.  
  150.     for (unsigned int i = 0; i < 2; ++i)
  151.     {
  152.         for (unsigned int x = 0; x < xw; ++x)
  153.         {
  154.             //bmp_set_pixel(self->bmp, z.x, z.y, (rgb_pixel_t)RGB(255, i, 0));
  155.             rgb_pixel_t* p = bmp_get_pixel(self->bmp, z.x, z.y);
  156.             if (p && p->alpha == 254) return p;
  157.             z.x += 1;
  158.         }
  159.  
  160.         for (unsigned int y = 0; y < yw; ++y)
  161.         {
  162.             //bmp_set_pixel(self->bmp, z.x, z.y, (rgb_pixel_t)RGB(255, i, 0));
  163.             rgb_pixel_t* p = bmp_get_pixel(self->bmp, z.x, z.y);
  164.             if (p && p->alpha == 254) return p;
  165.             z.y += 1;
  166.         }
  167.  
  168.         for (unsigned int x = 0; x < xw; ++x)
  169.         {
  170.             //bmp_set_pixel(self->bmp, z.x, z.y, (rgb_pixel_t)RGB(255, i, 0));
  171.             rgb_pixel_t* p = bmp_get_pixel(self->bmp, z.x, z.y);
  172.             if (p && p->alpha == 254) return p;
  173.             z.x -= 1;
  174.         }
  175.  
  176.         for (unsigned int y = 0; y < yw; ++y)
  177.         {  
  178.             //bmp_set_pixel(self->bmp, z.x, z.y, (rgb_pixel_t)RGB(255, i, 0));
  179.             rgb_pixel_t* p = bmp_get_pixel(self->bmp, z.x, z.y);
  180.             if (p && p->alpha == 254) return p;
  181.             z.y -= 1;
  182.         }
  183.  
  184.         z.x -= 1;
  185.         z.y -= 1;
  186.  
  187.         xw += 2;
  188.         yw += 2;
  189.     }
  190.    
  191.     return NULL;
  192. }
  193.  
  194.  
  195. void
  196. dla_iterate_point_trace(
  197.     struct dla* const self,
  198.     struct dla_point origin,
  199.     unsigned int imax
  200. ){
  201.     struct dla_point z = origin;
  202.  
  203.     for (unsigned int i = 0; i < imax; ++i)
  204.     {
  205.         unsigned char color = (unsigned char)((i / (imax - 1.0)) * 255);
  206.         struct dla_point wz = dla_walk(self, z);
  207.         rgb_pixel_t* p = dla_check(self, wz);
  208.  
  209.         if (p)
  210.         {
  211.             // plot point!
  212.             bmp_set_pixel(self->bmp, z.x, z.y, (rgb_pixel_t)RGBA(p->red, (p->green + color) % 256, p->blue, 254));
  213.             return;
  214.         }
  215.  
  216.         z = wz;
  217.     }
  218. }
  219.  
  220.  
  221. void
  222. dla_seed(
  223.     struct dla* const self,
  224.     unsigned int imax
  225. ){
  226.     double astep = PI2 / imax;
  227.     double rstep = (self->height / 10.0) / (imax - 1.0);
  228.  
  229.     for (unsigned int i = 0; i < imax + 4000; ++i)
  230.     {
  231.         unsigned char color = 150 + ((unsigned char)((i / (imax + 3999.0)) * 105));
  232.  
  233.         double angle = astep * i;
  234.         double xradius = rstep * i * 1.85;
  235.         double yradius = rstep * i;
  236.  
  237.         {
  238.         struct point2d pbt0 = {
  239.             (int)(cos(angle) * xradius + self->width / 2.0),
  240.             (int)(sin(angle) * yradius + self->height / 2.0)
  241.         };
  242.  
  243.         bmp_set_pixel(self->bmp, pbt0.x, pbt0.y, (rgb_pixel_t)RGBA(color, 0, color, 254));
  244.  
  245.         struct point2d pbt1 = {
  246.             (int)(cos(angle) * fabs(cos(angle)) * xradius + self->width / 2.0),
  247.             (int)(sin(angle) * fabs(sin(angle)) * yradius + self->height / 2.0)
  248.         };
  249.  
  250.         bmp_set_pixel(self->bmp, pbt1.x, pbt1.y, (rgb_pixel_t)RGBA(0, 0, color, 254));
  251.         }
  252.  
  253.         {
  254.             struct point2d pbt0 = {
  255.                 (int)(cos(angle) * -xradius + self->width / 2.0),
  256.                 (int)(sin(angle) * -yradius + self->height / 2.0)
  257.             };
  258.  
  259.             bmp_set_pixel(self->bmp, pbt0.x, pbt0.y, (rgb_pixel_t)RGBA(color, 0, color, 254));
  260.  
  261.             struct point2d pbt1 = {
  262.                 (int)(cos(angle) * fabs(cos(angle)) * -xradius + self->width / 2.0),
  263.                 (int)(sin(angle) * fabs(sin(angle)) * -yradius + self->height / 2.0)
  264.             };
  265.  
  266.             bmp_set_pixel(self->bmp, pbt1.x, pbt1.y, (rgb_pixel_t)RGBA(0, 0, color, 254));
  267.         }
  268.     }
  269. }
  270.  
  271.  
  272. void
  273. dla_iterate(
  274.     struct dla* const self,
  275.     unsigned int imax,
  276.     unsigned int iptsmax
  277. ){
  278.     for (unsigned int i = 0; i < imax; ++i)
  279.     {
  280.         struct dla_point rpt = {
  281.             RAND_MOD(self->width),
  282.             RAND_MOD(self->height)
  283.         };
  284.  
  285.         dla_iterate_point_trace(self, rpt, iptsmax);
  286.  
  287.         printf("DLA point iteration: %u or %u\r", i, imax);
  288.     }
  289. }
  290.  
  291.  
  292. int dla_main_app()
  293. {
  294.     struct dla self;
  295.  
  296.     if (dla_create(&self, 3840, 2160))
  297.     {
  298.         dla_seed(&self, 1200);
  299.         dla_iterate(&self, 2500000, 1000);
  300.        
  301.         // Save the bitmap to a file
  302.         bmp_save(self.bmp, "output.bmp");
  303.  
  304.         dla_destroy(&self);
  305.  
  306.         return 1;
  307.     }
  308.  
  309.     return 0;
  310. }
  311.  
  312.  
  313.  
  314.  
  315. int main(void)
  316. {
  317.     if (dla_main_app())
  318.     {
  319.         puts("\n\nThe DLA simulation Worked!");
  320.     }
  321.  
  322.     else
  323.     {
  324.         puts("\n\nThe DLA simulation did NOT Work!! ;^o");
  325.     }
  326.  
  327.     // Open the bitmap...
  328.     //system("mspaint output.bmp");
  329.  
  330.     return 0;
  331. }
Advertisement
Add Comment
Please, Sign In to add comment