Ecco un generatore di frattali di Sierpinski programmato in C ed una versione del generatore del frattale di Sierpinski in 3D fatto in javascript programmato da Luca Prezzavento..
Visualizza Frattale programmato in Javascript in 3D
Come compilare e utilizzare il software
Per compilare il generatore di frattali bisogna usare make e quindi eseguire il seguente
comando:
make
Per generare un frattale dobbiamo eseguire il file eseguibile “generatoredifrattali” da
terminale assegnando dei parametri, ovvero -l, -r e -o che sono rispettivamente
il livello, le iterazioni e il nome del file bmp da generare.
esempio:
./generatoredifrattali -l 2 -r 1000 -o out.bmp
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include "bitmap.h" #include "grid.h" int main(int argc, char **argv) { struct grid bitmap, triangle; int cols, rows = 256, levels = 2, x, y, upx; uint64_t zero = 0, one = 1, sum, out; FILE *file = stdout; FILE *numout = fopen("numout", "w"); struct bitmap_color oddcol = { 255, 0, 0 }, evncol = { 0, 255, 0 }; int opt; while ((opt = getopt(argc, argv, "b:f:l:o:r:")) != -1) { switch (opt) { case 'b': sscanf(optarg, "%hhx%hhx%hhx", &evncol.red, &evncol.green, &evncol.blue); break; case 'f': sscanf(optarg, "%hhx%hhx%hhx", &oddcol.red, &oddcol.green, &oddcol.blue); break; case 'l': levels = atoi(optarg); break; case 'o': file = fopen(optarg, "wb"); break; case 'r': rows = atoi(optarg); break; } } cols = (1 + ((rows - 1) * (levels - (levels % 2)))) * 2; triangle = grid_alloc(cols, rows, sizeof(uint64_t)); bitmap = bitmap_create(cols - (cols % 4), rows); for (x = 0; x < cols; x++) { grid_set(triangle, x, 0, &zero); bitmap_set(bitmap, x, 0, evncol); } grid_set(triangle, cols / 2 + 1, 0, &one); bitmap_set(bitmap, cols / 2 + 1, 0, oddcol); for (y = 1; y < rows; y++ ) { for (x = 0; x < cols; x++) { sum = 0; for (upx = 0; upx < levels; upx++) { out = 0; if (levels % 2 == 1) { grid_get(triangle, x - levels / 2 + upx, y - 1, &out); } else { grid_get(triangle, x - (levels - 1) + upx * 2, y - 1, &out); } sum += out; } fprintf(numout, "%3d", (int) sum); grid_set(triangle, x, y, &sum); bitmap_set(bitmap, x, y, (sum % 2 == 0) ? evncol : oddcol); } fprintf(numout, "rn"); } bitmap_save(bitmap, file); grid_free(&triangle); bitmap_destroy(&bitmap); fclose(file); fclose(numout); return 0; }
#ifndef _GRID #define _GRID #include <stdint.h> struct grid { size_t width; size_t height; size_t usize; void *data; }; extern struct grid grid_alloc(size_t width, size_t height, size_t usize); extern void grid_set(struct grid grid, int x, int y, void *val); extern int grid_get(struct grid grid, int x, int y, void *data); extern void grid_free(struct grid *grid); #endif
#include <stdlib.h> #include <string.h> #include "grid.h" struct grid grid_alloc(size_t width, size_t height, size_t usize) { struct grid grid = { 0, 0, 0, 0 }; grid.data = malloc(width * height * usize); if (grid.data) { grid.width = width; grid.height = height; grid.usize = usize; } return grid; } void grid_set(struct grid grid, int x, int y, void *val) { uint8_t *start; size_t i; if (grid.data != (void *) 0 && x >= 0 && x < grid.width && y >= 0 && y < grid.height && val != (void *) 0) { start = ((uint8_t *) grid.data) + (y * grid.width * grid.usize) + (x * grid.usize); for (i = 0; i < grid.usize; i++) { start[i] = ((uint8_t *) val)[i]; } } } int grid_get(struct grid grid, int x, int y, void *data) { uint8_t *start; if (grid.data != (void *) 0 && x >= 0 && x < grid.width && y >= 0 && y < grid.height && data != (void *) 0) { start = ((uint8_t *) grid.data) + (y * grid.width * grid.usize) + (x * grid.usize); memmove(data, (void *) start, grid.usize); return 1; } else { return 0; } } void grid_free(struct grid *grid) { if (grid->data) { free((void *) grid->data); grid->data = (void *) 0; } }
#ifndef _BITMAP #define _BITMAP #include <stdio.h> #include "grid.h" struct bitmap_color { uint8_t blue; uint8_t green; uint8_t red; }; extern struct grid bitmap_create(int width, int height); extern void bitmap_set(struct grid bitmap, int x, int y, struct bitmap_color color); extern struct bitmap_color bitmap_get(struct grid bitmap, int x, int y); extern int bitmap_save(struct grid bitmap, FILE *file); extern void bitmap_destroy(struct grid *bitmap); #endif
#include <stdio.h> #include "bitmap.h" struct grid bitmap_create(int width, int height) { return grid_alloc(width, height, 3); } void bitmap_set(struct grid bitmap, int x, int y, struct bitmap_color color) { uint8_t ccolor[3]; ccolor[0] = color.blue; ccolor[1] = color.green; ccolor[2] = color.red; grid_set(bitmap, x, bitmap.height - y - 1, ccolor); } struct bitmap_color bitmap_get(struct grid bitmap, int x, int y) { struct bitmap_color color = { 0, 0, 0 }; uint8_t ccolor[3] = { 0, 0, 0 }; if (grid_get(bitmap, x, y, (void *) ccolor) != 0) { color.blue = ccolor[0]; color.green = ccolor[1]; color.red = ccolor[2]; } return color; } int bitmap_save(struct grid bitmap, FILE *file) { int written = 0; const uint16_t planes = 1, identifier = 0x4d42, depth = 24; const uint32_t reserved = 0, header_size = 40, offset = header_size + 14, compression = 0, hresolution = 2835, vresolution = 2835, colors = 0, icolors = 0; uint32_t width = (uint32_t) bitmap.width, height = (uint32_t) bitmap.height, imgsize, filesize; imgsize = width * height * 3; filesize = imgsize + offset; if (!bitmap.data || !file) { return 0; } written += fwrite(&identifier, 1, 2, file); written += fwrite(&filesize, 1, 4, file); written += fwrite(&reserved, 1, 4, file); written += fwrite(&offset, 1, 4, file); written += fwrite(&header_size, 1, 4, file); written += fwrite(&width, 1, 4, file); written += fwrite(&height, 1, 4, file); written += fwrite(&planes, 1, 2, file); written += fwrite(&depth, 1, 2, file); written += fwrite(&compression, 1, 4, file); written += fwrite(&imgsize, 1, 4, file); written += fwrite(&hresolution, 1, 4, file); written += fwrite(&vresolution, 1, 4, file); written += fwrite(&colors, 1, 4, file); written += fwrite(&icolors, 1, 4, file); written += fwrite(((uint8_t *) bitmap.data), 1, imgsize, file); return written; } void bitmap_destroy(struct grid *bitmap) { grid_free(bitmap); }
Download Generatore di frattali
Link alternativo 1
Link alternativo 2
Link alternativo 3
Link alternativo 4