Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- #include <bmpfile.h>
- #define PI 3.141592653589793
- #define PI2 (PI * 2.0)
- #define RAND_MOD(mp_mod) (rand() % (mp_mod))
- #define RAND_FLOAT() (rand() / (RAND_MAX - 0.0))
- #define RAND_RANGE(mp_base, mp_mod) ((mp_base) + (int)RAND_MOD(((mp_mod) + 1)))
- #define RGBA(mp_r, mp_g, mp_b, mp_a) { (mp_b), (mp_g), (mp_r), (mp_a) }
- #define RGB(mp_r, mp_g, mp_b) RGBA(mp_r, mp_g, mp_b, 255)
- struct point2d
- {
- double x;
- double y;
- };
- struct point2d
- point2d_get_scaled(
- struct point2d p0,
- struct point2d p1,
- double scale
- ){
- struct point2d spt = {
- p0.x + (p1.x - p0.x) * scale,
- p0.y + (p1.y - p0.y) * scale
- };
- return spt;
- }
- struct dla_point
- {
- int x;
- int y;
- };
- struct dla
- {
- bmpfile_t* bmp;
- unsigned int width;
- unsigned int height;
- };
- int
- dla_create(
- struct dla* const self,
- unsigned int width,
- unsigned int height
- ){
- if ((self->bmp = bmp_create(width, height, 24)))
- {
- // Set background to black
- for (unsigned int y = 0; y < height; ++y)
- {
- for (unsigned int x = 0; x < width; ++x)
- {
- bmp_set_pixel(self->bmp, x, y, (rgb_pixel_t)RGB(0, 0, 0));
- }
- }
- self->width = width;
- self->height = height;
- return 1;
- }
- return 0;
- }
- void
- dla_destroy(
- struct dla* const self
- ){
- bmp_destroy(self->bmp);
- }
- struct dla_point
- dla_walk(
- struct dla const* const self,
- struct dla_point pt
- ){
- int rx = RAND_RANGE(-1, 2);
- int ry = RAND_RANGE(-1, 2);
- pt.x += rx;
- pt.y += ry;
- if (pt.x < 0) self->width - (1 + RAND_MOD(2));
- if (pt.y < 0) self->height - (1 + RAND_MOD(2));
- if (pt.x >= self->width) pt.x = RAND_MOD(2);
- if (pt.y >= self->height) pt.y = RAND_MOD(2);
- return pt;
- }
- rgb_pixel_t*
- dla_check(
- struct dla const* const self,
- struct dla_point pt
- ){
- if (RAND_MOD(128) > 1)
- {
- int dx = 0;//(RAND_MOD(2) < 1) ? -1 : 1;
- int dy = 0;//(RAND_MOD(2) < 1) ? -1 : 1;
- rgb_pixel_t* p = bmp_get_pixel(self->bmp, pt.x + dx, pt.y + dy);
- if (p && p->alpha == 254)
- {
- //printf("FOUND!\n");
- return p;
- }
- return NULL;
- }
- int xw = 3;
- int yw = 3;
- struct dla_point z = {
- pt.x - 1,
- pt.y - 1
- };
- for (unsigned int i = 0; i < 2; ++i)
- {
- for (unsigned int x = 0; x < xw; ++x)
- {
- //bmp_set_pixel(self->bmp, z.x, z.y, (rgb_pixel_t)RGB(255, i, 0));
- rgb_pixel_t* p = bmp_get_pixel(self->bmp, z.x, z.y);
- if (p && p->alpha == 254) return p;
- z.x += 1;
- }
- for (unsigned int y = 0; y < yw; ++y)
- {
- //bmp_set_pixel(self->bmp, z.x, z.y, (rgb_pixel_t)RGB(255, i, 0));
- rgb_pixel_t* p = bmp_get_pixel(self->bmp, z.x, z.y);
- if (p && p->alpha == 254) return p;
- z.y += 1;
- }
- for (unsigned int x = 0; x < xw; ++x)
- {
- //bmp_set_pixel(self->bmp, z.x, z.y, (rgb_pixel_t)RGB(255, i, 0));
- rgb_pixel_t* p = bmp_get_pixel(self->bmp, z.x, z.y);
- if (p && p->alpha == 254) return p;
- z.x -= 1;
- }
- for (unsigned int y = 0; y < yw; ++y)
- {
- //bmp_set_pixel(self->bmp, z.x, z.y, (rgb_pixel_t)RGB(255, i, 0));
- rgb_pixel_t* p = bmp_get_pixel(self->bmp, z.x, z.y);
- if (p && p->alpha == 254) return p;
- z.y -= 1;
- }
- z.x -= 1;
- z.y -= 1;
- xw += 2;
- yw += 2;
- }
- return NULL;
- }
- void
- dla_iterate_point_trace(
- struct dla* const self,
- struct dla_point origin,
- unsigned int imax
- ){
- struct dla_point z = origin;
- for (unsigned int i = 0; i < imax; ++i)
- {
- unsigned char color = (unsigned char)((i / (imax - 1.0)) * 255);
- struct dla_point wz = dla_walk(self, z);
- rgb_pixel_t* p = dla_check(self, wz);
- if (p)
- {
- // plot point!
- bmp_set_pixel(self->bmp, z.x, z.y, (rgb_pixel_t)RGBA(p->red, (p->green + color) % 256, p->blue, 254));
- return;
- }
- z = wz;
- }
- }
- void
- dla_seed(
- struct dla* const self,
- unsigned int imax
- ){
- double astep = PI2 / imax;
- double rstep = (self->height / 10.0) / (imax - 1.0);
- for (unsigned int i = 0; i < imax + 4000; ++i)
- {
- unsigned char color = 150 + ((unsigned char)((i / (imax + 3999.0)) * 105));
- double angle = astep * i;
- double xradius = rstep * i * 1.85;
- double yradius = rstep * i;
- {
- struct point2d pbt0 = {
- (int)(cos(angle) * xradius + self->width / 2.0),
- (int)(sin(angle) * yradius + self->height / 2.0)
- };
- bmp_set_pixel(self->bmp, pbt0.x, pbt0.y, (rgb_pixel_t)RGBA(color, 0, color, 254));
- struct point2d pbt1 = {
- (int)(cos(angle) * fabs(cos(angle)) * xradius + self->width / 2.0),
- (int)(sin(angle) * fabs(sin(angle)) * yradius + self->height / 2.0)
- };
- bmp_set_pixel(self->bmp, pbt1.x, pbt1.y, (rgb_pixel_t)RGBA(0, 0, color, 254));
- }
- {
- struct point2d pbt0 = {
- (int)(cos(angle) * -xradius + self->width / 2.0),
- (int)(sin(angle) * -yradius + self->height / 2.0)
- };
- bmp_set_pixel(self->bmp, pbt0.x, pbt0.y, (rgb_pixel_t)RGBA(color, 0, color, 254));
- struct point2d pbt1 = {
- (int)(cos(angle) * fabs(cos(angle)) * -xradius + self->width / 2.0),
- (int)(sin(angle) * fabs(sin(angle)) * -yradius + self->height / 2.0)
- };
- bmp_set_pixel(self->bmp, pbt1.x, pbt1.y, (rgb_pixel_t)RGBA(0, 0, color, 254));
- }
- }
- }
- void
- dla_iterate(
- struct dla* const self,
- unsigned int imax,
- unsigned int iptsmax
- ){
- for (unsigned int i = 0; i < imax; ++i)
- {
- struct dla_point rpt = {
- RAND_MOD(self->width),
- RAND_MOD(self->height)
- };
- dla_iterate_point_trace(self, rpt, iptsmax);
- printf("DLA point iteration: %u or %u\r", i, imax);
- }
- }
- int dla_main_app()
- {
- struct dla self;
- if (dla_create(&self, 3840, 2160))
- {
- dla_seed(&self, 1200);
- dla_iterate(&self, 2500000, 1000);
- // Save the bitmap to a file
- bmp_save(self.bmp, "output.bmp");
- dla_destroy(&self);
- return 1;
- }
- return 0;
- }
- int main(void)
- {
- if (dla_main_app())
- {
- puts("\n\nThe DLA simulation Worked!");
- }
- else
- {
- puts("\n\nThe DLA simulation did NOT Work!! ;^o");
- }
- // Open the bitmap...
- //system("mspaint output.bmp");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment