Skip to content

Instantly share code, notes, and snippets.

@martinlk
Created February 16, 2012 18:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save martinlk/1846916 to your computer and use it in GitHub Desktop.
Save martinlk/1846916 to your computer and use it in GitHub Desktop.
Fractal fern generator [2]
//
// Martin Klingensmith
// Fractal Fern generator.
// 2012
//
//
#include <stdio.h>
#include <math.h>
#define WIDTH 1200
#define HEIGHT 1200
typedef __int32 int32_t;
typedef unsigned __int32 uint32_t;
typedef unsigned __int16 uint16_t;
void line(int x1, int y1, int x2, int y2); // Draw line
void plot(int x, int y, uint32_t color); // Plot pixel
void fern(int points);
//
// BMP Magic value
//
struct {
unsigned char magic[2];
} bmpmagic;
//
// BMP header
//
struct {
uint32_t filesz;
uint16_t creator1;
uint16_t creator2;
uint32_t bmp_offset;
} bmpheader;
//
// DIB header
//
struct {
uint32_t header_sz;
int32_t width;
int32_t height;
uint16_t nplanes;
uint16_t bitspp;
uint32_t compress_type;
uint32_t bmp_bytesz;
int32_t hres;
int32_t vres;
uint32_t ncolors;
uint32_t nimpcolors;
} dib;
//
// Pixel
//
typedef struct px {
char b;
char g;
char r;
} px;
px pixels[WIDTH+1][HEIGHT];
uint32_t color;
int main()
{
int x=0;
uint16_t rowsize;
//
// Start BMP
//
FILE *outfile = fopen("fern.bmp","w");
if (outfile == NULL){
fprintf(stderr,"Can't open file\n");
}
bmpmagic.magic[0] = 0x42;
bmpmagic.magic[1] = 0x4D;
bmpheader.creator1 = 0;
bmpheader.creator2 = 0;
bmpheader.bmp_offset = sizeof(bmpmagic) + sizeof(bmpheader) + sizeof(dib);
dib.header_sz = sizeof(dib);
dib.width = WIDTH;
dib.height = HEIGHT;
dib.nplanes = 1;
dib.bitspp = 24;
dib.compress_type = 0;
rowsize = dib.bitspp * WIDTH * 4 / 32; // row size, bytes
rowsize += rowsize%4; // make multiple of 4 bytes
dib.bmp_bytesz = rowsize * HEIGHT;
dib.hres = 2835;
dib.vres = 2835;
dib.ncolors = 0;
dib.nimpcolors = 0;
bmpheader.filesz = dib.bmp_bytesz + 54;
//
// Write BMP header
//
fwrite(&bmpmagic,1,sizeof(bmpmagic),outfile);
fwrite(&bmpheader,1,sizeof(bmpheader),outfile);
fwrite(&dib,1,sizeof(dib),outfile);
//
// Initialize pixel buffer
//
memset(&pixels,0xFF,sizeof(pixels));
//
// Fern
//
srand(time(NULL));
fern(500000);
//
// Write pixels to BMP
//
for(x=0;x<HEIGHT;x++)
{
fwrite(&(pixels[x]),1,3*WIDTH+3*WIDTH%4,outfile);
}
fclose(outfile);
return 0;
}
//
// Draw fern
//
void fern(int points)
{
double x,y;
double xn,yn;
unsigned int px,py;
int p;
for(;points>0;points--){
p = rand()%100;
if(p < 2){
color = 0x10FF50;
xn = 0;
yn = .16*y;
x = xn;
y = yn;
} else if (p < 8) {
color = 0x10BB00;
xn = .2*x-.26*y;
yn = .2*x+.23*y+1.65;
x = xn;
y = yn;
} else if (p < 15) {
color = 0x10FF10;
xn = -.15*x+.28*y;
yn = .26*x+.24*y+.44;
x = xn;
y = yn;
} else {
color = 0x10FF00;
xn = .85*x+.04*y;
yn = -.04*x+.98*y+1.6;
x = xn;
y = yn;
};
px = (unsigned int)(x*WIDTH/10+WIDTH/2);
py = (unsigned int)(y*HEIGHT/10+HEIGHT/8);
plot(px,py,color);
plot(px+1,py-1,color*0x808080);
plot(px,py-1,color*0xA0A0A0);
plot(px+1,py,color*0x60A0A0);
plot(px-1,py+1,color*0x808080);
plot(px,py+1,color*0xA0A0A0);
plot(px-1,py,color*0x101010);
//printf("\n(%d,%d)",px,py);
}
}
//
// Pixel poke
//
void plot(int x, int y, uint32_t col)
{
if(x>HEIGHT) return;
if(y>WIDTH) return;
pixels[y][x].r = (col >> 16) & 0xFF;
pixels[y][x].g = (col >> 8) & 0xFF;
pixels[y][x].b = col & 0xFF;
return;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment