Brownian tree/C++ animated: Difference between revisions

m
Fixed syntax highlighting.
(Created page with "=={{header|C++}}== 300px <lang cpp> #include <windows.h> #include <windowsx.h> #include <string> //-----------------------------------------...")
 
m (Fixed syntax highlighting.)
 
(6 intermediate revisions by 4 users not shown)
Line 1:
=={{header|C++}}==
[[File:brownianTreeAnim_cpp.png|300px]]
 
<lang cpp>
The green dots you can see in this picture are what I call movers. They are particles that still do not belong to the tree.
<syntaxhighlight lang="cpp">
#include <windows.h>
#include <windowsx.h>
#include <string>
 
Line 12:
enum states { SEED, GROWING, MOVING, REST };
enum treeStates { NONE, MOVER, TREE };
const int MAX_SIDE = 600500, MAX_MOVERS = 511, MAX_CELLS = 15137;
 
//--------------------------------------------------------------------------------------------------
classstruct point
{
point() int: ax(0), inty(0) b ) { x = a; y = b; { }
public:
point() int a, int b ) : x(a), y(b) { x = y = 0; }
point( int a, int b ) { x = a; y = b; }
void set( int a, int b ) { x = a; y = b; }
int x, y;
};
 
//--------------------------------------------------------------------------------------------------
classstruct movers
{
public:
point pos;
bool moving;
movers() : moving( false ){}
};
//--------------------------------------------------------------------------------------------------
class myBitmap
{
Line 85 ⟶ 83:
BITMAPINFO infoheader;
BITMAP bitmap;
DWORD* dwpBits;
DWORD wb;
HANDLE file;
 
GetObject( bmp, sizeof( bitmap ), &bitmap );
 
DWORD* dwpBits = new DWORD[bitmap.bmWidth * bitmap.bmHeight];
ZeroMemory( dwpBits, bitmap.bmWidth * bitmap.bmHeight * sizeof( DWORD ) );
ZeroMemory( &infoheader, sizeof( BITMAPINFO ) );
Line 110 ⟶ 106:
GetDIBits( hdc, bmp, 0, height, ( LPVOID )dwpBits, &infoheader, DIB_RGB_COLORS );
 
HANDLE file = CreateFile( path.c_str(), GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL );
WriteFile( file, &fileheader, sizeof( BITMAPFILEHEADER ), &wb, NULL );
WriteFile( file, &infoheader.bmiHeader, sizeof( infoheader.bmiHeader ), &wb, NULL );
Line 119 ⟶ 115:
}
 
HDC getDC() const { return hdc; }
int getWidth() const { return width; }
int getHeight() const { return height; }
 
private:
Line 166 ⟶ 162:
void drawGrid()
{
DWORD clr;
BYTE g;
_bmp.clear();
 
Line 174 ⟶ 167:
for( int x = 0; x < MAX_SIDE; x++ )
{
BYTE g = _grid[x][y];
if( g != NONE )
{
DWORD clr = g > MOVER ? _color[g - 2] : RGB( 0, 255, 0 );
SetPixel( _bmp.getDC(), x, y, clr );
}
Line 199 ⟶ 192:
m->moving = true;
int x = MAX_SIDE - MAX_SIDE / 2, y = MAX_SIDE / 4, a, b;
do
while( true )
{
a = rand() % x + y; b = rand() % x + y;
} while if( _grid[a][b] =!= NONE ) break;
}
 
m->pos.set( a, b );
Line 211 ⟶ 203:
void startMovers()
{
movers* m;
for( int c = 0; c < MAX_MOVERS; c++ )
{
movers* m = &_movers[c];
addMover( m );
}
Line 262 ⟶ 253:
void moveMovers()
{
movers* mm;
bool found = false;
for( int m = 0; m < MAX_MOVERS; m++ )
{
movers* mm = &_movers[m];
if( !mm->moving ) continue;
found = true;
Line 352 ⟶ 342:
}
//--------------------------------------------------------------------------------------------------
</syntaxhighlight>
</lang>
9,476

edits