Created
February 25, 2012 18:09
-
-
Save martinlk/1909860 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Martin Klingensmith | |
// Flea mover | |
// 2012 | |
// | |
// | |
#include <stdio.h> | |
#include <math.h> | |
#define WIDTH 500 | |
#define HEIGHT 500 | |
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 rect(int x1, int y1, int x2, int y2, uint32_t col); | |
int permute(int n); | |
// | |
// 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 grid[30][30]; | |
int main() | |
{ | |
int x,y,z; | |
double acc=0; | |
// | |
// Initialize grid | |
// | |
for(x=0;x<30;x++){ | |
for(y=0;y<30;y++){ | |
grid[x][y] = 1; | |
} | |
} | |
srand(time(NULL)); | |
for(x=0;x<50;x++){ | |
z = permute(x); | |
acc += z; | |
printf("\nFrame %0d empty squares %d",x,z); | |
} | |
printf("\nNumber of unoccupied squares after 50 iterations: %3.6f",acc/50); | |
return 0; | |
} | |
int permute(int n) | |
{ | |
int x=0,y=0,z; | |
uint16_t rowsize; | |
char fname[128]; | |
FILE *outfile; | |
sprintf(fname,"grid%0d.bmp",n); | |
outfile = fopen(fname,"w"); | |
// | |
// Start BMP | |
// | |
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)); | |
for(x=0;x<30;x++){ | |
for(y=0;y<30;y++){ | |
//printf("\t %d ",z); | |
if(grid[x][y]==0) | |
continue; | |
while(grid[x][y] > 0){ | |
z = rand()%4; | |
switch(z){ | |
case 0: | |
if(x+1 < 30){ | |
grid[x+1][y]++; | |
grid[x][y]--; | |
} | |
break; | |
case 1: | |
if(x-1 >= 0){ | |
grid[x-1][y]++; | |
grid[x][y]--; | |
} | |
break; | |
case 2: | |
if(y+1 < 30){ | |
grid[x][y]--; | |
grid[x][y+1]++; | |
} | |
break; | |
case 3: | |
if(y-1 >= 0){ | |
grid[x][y-1]++; | |
grid[x][y]--; | |
} | |
break; | |
} | |
} | |
} | |
} | |
z = 0; | |
for(x=0;x<30;x++){ | |
for(y=0;y<30;y++){ | |
color = grid[x][y] * 30; | |
color = (color << 16) | (color << 8) | color; | |
rect((WIDTH/30)*x, | |
(HEIGHT/30)*y, | |
(WIDTH/30)*(x+1), | |
(HEIGHT/30)*(y+1), | |
color); | |
if (grid[x][y]==0) z++; | |
} | |
} | |
//rect(1,1,100,30,80); | |
// | |
// Write pixels to BMP | |
// | |
for(x=0;x<HEIGHT;x++) | |
{ | |
fwrite(&(pixels[x]),1,3*WIDTH+3*WIDTH%4,outfile); | |
} | |
fclose(outfile); | |
return z; | |
} | |
// | |
// 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; | |
} | |
// | |
// Rect | |
// | |
void rect(int x1, int y1, int x2, int y2, uint32_t col) | |
{ | |
int x,y; | |
if(x1>x2){ | |
x = x1; | |
x1 = x2; | |
x2 = x; | |
}; | |
if(y1>y2){ | |
y = y1; | |
y1 = y2; | |
y2 = y; | |
}; | |
for(x=x1;x<=x2;x++){ | |
for(y=y1;y<=y2;y++){ | |
plot(x,y,col); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment