Brownian tree: Difference between revisions
Content added Content deleted
(Created page with '{{Wikipedia|Brownian_tree|en}} Category:Raster graphics operations {{task|Fractals}}Generate and draw a Brownian Tree. =={{header|C}}== This code uses …') |
No edit summary |
||
Line 29: | Line 29: | ||
py = rand() % SIZE; |
py = rand() % SIZE; |
||
while (1){ |
|||
while (!hit){ |
|||
// randomly choose a direction |
// randomly choose a direction |
||
dx = rand() % 3 - 1; |
dx = rand() % 3 - 1; |
||
Line 41: | Line 40: | ||
}else if (world[py + dy][px + dx] != 0){ |
}else if (world[py + dy][px + dx] != 0){ |
||
// bumped into something |
// bumped into something |
||
⚫ | |||
world[py][px] = 1; |
world[py][px] = 1; |
||
⚫ | |||
}else{ |
}else{ |
||
py += dy; |
py += dy; |
||
Line 57: | Line 56: | ||
int x, y; |
int x, y; |
||
memset(world, 0, sizeof |
memset(world, 0, sizeof world); |
||
srand((unsigned)time(NULL)); |
srand((unsigned)time(NULL)); |
||
Revision as of 17:37, 27 April 2010
This page uses content from Wikipedia. The original article was at Brownian_tree. The list of authors can be seen in the page history. As with Rosetta Code, the text of Wikipedia is available under the GNU FDL. (See links for details on variance) |
![Task](http://static.miraheze.org/rosettacodewiki/thumb/b/ba/Rcode-button-task-crushed.png/64px-Rcode-button-task-crushed.png)
You are encouraged to solve this task according to the task description, using any language you may know.
Generate and draw a Brownian Tree.
C
This code uses the FreeImage library. <lang c>#include <string.h>
- include <stdlib.h>
- include <time.h>
- include <math.h>
- include <FreeImage.h>
- define NUM_PARTICLES 1000
- define SIZE 800
void draw_brownian_tree(int world[SIZE][SIZE]){
int px, py; // particle values int dx, dy; // offsets int hit; int i; // set the seed world[rand() % SIZE][rand() % SIZE] = 1;
for (i = 0; i < NUM_PARTICLES; i++){ // set particle's initial position px = rand() % SIZE; py = rand() % SIZE;
while (1){ // randomly choose a direction dx = rand() % 3 - 1; dy = rand() % 3 - 1;
if (dx + px < 0 || dx + px >= SIZE || dy + py < 0 || dy + py >= SIZE){ // plop the particle into some other random location px = rand() % SIZE; py = rand() % SIZE; }else if (world[py + dy][px + dx] != 0){ // bumped into something world[py][px] = 1; break; }else{ py += dy; px += dx; } } }
}
int main(){
int world[SIZE][SIZE]; FIBITMAP * img; RGBQUAD rgb; int x, y; memset(world, 0, sizeof world); srand((unsigned)time(NULL));
draw_brownian_tree(world);
img = FreeImage_Allocate(SIZE, SIZE, 32, 0, 0, 0);
for (y = 0; y < SIZE; y++){ for (x = 0; x < SIZE; x++){ rgb.rgbRed = rgb.rgbGreen = rgb.rgbBlue = (world[y][x] ? 255 : 0); FreeImage_SetPixelColor(img, x, y, &rgb); } } FreeImage_Save(FIF_BMP, img, "brownian_tree.bmp", 0); FreeImage_Unload(img);
}</lang>