//=========================================================================== // 3D HALF WAY THERE // N. Gessler 20-Jan-2010 //=========================================================================== //--------------------------------------------------------------------------- #include #pragma hdrstop #include "Unit1.h" #include "math.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" #define EMPTY 0 #define RED 1 #define CYAN 2 #define BLACK 3 #define WHITE 4 //--------------------------------------------------------------------------- TForm1 *Form1; //=========================================================================== // VARIABLES //=========================================================================== int numberOfCities; int cityX[10]; int cityY[10]; int cityZ[10]; int city; int campX, campY, campZ; int world[250][250][250]; int flatWorld[250][250]; int maxVal; int runToIterations = 1000; int totalIterations; bool plus = True; int depth = 250; //=========================================================================== // FUNCTIONS //=========================================================================== //------------------------------------------------------------------ DRAW BOX void drawBox (void) { Form1->PaintBox1->Canvas->Pen->Color = clBlack; Form1->PaintBox1->Canvas->MoveTo(0, 0); Form1->PaintBox1->Canvas->LineTo(0, 249); Form1->PaintBox1->Canvas->LineTo(249, 249); Form1->PaintBox1->Canvas->LineTo(249, 0); Form1->PaintBox1->Canvas->LineTo(0, 0); } //-------------------------------------------------------------- RENDER IN 3D void renderIn3D (void) { Form1->Refresh(); for (int row = 0; row < 250; row++) { for (int column = 0; column < 250; column++) { if (flatWorld[row][column] == CYAN) { Form1->PaintBox1->Canvas->Pixels[row][column] = clAqua; } if (flatWorld[row][column] == RED) { Form1->PaintBox1->Canvas->Pixels[row][column] = clRed; } if (flatWorld[row][column] == BLACK) { Form1->PaintBox1->Canvas->Pixels[row][column] = clBlack; } if (flatWorld[row][column] == EMPTY) { Form1->PaintBox1->Canvas->Pixels[row][column] = clWhite; } } } } //---------------------------------------------------------------- INITIALIZE void initialize (void) { numberOfCities = 0; Form1->PaintBox1->Refresh(); maxVal = 0; totalIterations = 0; Form1->EditTotalIterations->Text = 0; for (int row = 0; row < 250; row++) { for (int column = 0; column < 250; column++) { flatWorld[row][column] = 0; for (int depth = 0; depth < 250; depth++) { world[row][column][depth] = 0; } } } renderIn3D(); drawBox(); } //----------------------------------------------------------------------- RUN void run (void) { drawBox(); Form1->ButtonRun->Caption = "Wait"; for (int i = 0; i < runToIterations; i++) { totalIterations++; city = random(numberOfCities); if (plus) { // the normal version campX = (campX + cityX[city]) / 2; campY = (campY + cityY[city]) / 2; campZ = (campZ + cityZ[city]) / 2; } else { // the bent version campX = abs(cityX[city] - (campX - cityX[city]) / 2); campY = abs(cityY[city] - (campY - cityY[city]) / 2); campZ = abs(cityZ[city] - (campZ - cityZ[city]) / 2); } if (int(campX) < 250 && int(campY) < 250) { Form1->PaintBox1->Canvas->Pixels[int(campX)][int(campY)] = clRed; world[int(campX)][int(campY)][int(campZ)]++; // wants to paint RED if (flatWorld[int(campX)][int(campY)] == CYAN) flatWorld[int(campX)][int(campY)] = BLACK; else flatWorld[int(campX)][int(campY)] = RED; // wants to paint CYAN if (flatWorld[int(campX + .08 * (campZ - 125))][int(campY)] == RED) flatWorld[int(campX + .08 * (campZ - 125))][int(campY)] = BLACK; else flatWorld[int(campX + .08 * (campZ - 125))][int(campY)] = CYAN; } Application->ProcessMessages(); } renderIn3D(); drawBox(); Form1->EditTotalIterations->Text = totalIterations; Form1->ButtonRun->Caption = "Run"; } //=========================================================================== // EVENT HANDLERS //=========================================================================== //---------------------------------------------------------- FORM CONSTRUCTOR __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { initialize(); randomize(); } //------------------------------------------------------- PAINTBOX MOUSE DOWN void __fastcall TForm1::PaintBox1MouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y) { cityX[numberOfCities] = X; cityY[numberOfCities] = Y; cityZ[numberOfCities] = depth; numberOfCities++; Form1->PaintBox1->Canvas->Pixels[X][Y] = clRed; Form1->PaintBox1->Canvas->Pixels[X + .08 * (depth - 125)][Y] = clAqua; } //------------------------------------------ TRACKBAR RUNTO ITERATIONS CHANGE void __fastcall TForm1::TrackBarRunToIterationsChange(TObject *Sender) { runToIterations = pow(10, TrackBarRunToIterations->Position); Form1->EditRunToIterations->Text = runToIterations; } //---------------------------------------------------- RADIOGROUP TWEAK CLICK void __fastcall TForm1::RadioGroupTweakClick(TObject *Sender) { if (RadioGroupTweak->ItemIndex == 0) { plus = True; } else plus = False; } //---------------------------------------------------------- BUTTON RUN CLICK void __fastcall TForm1::ButtonRunClick(TObject *Sender) { run(); } //-------------------------------------------------------- BUTTON RESET CLICK void __fastcall TForm1::ButtonResetClick(TObject *Sender) { initialize(); } //----------------------------------------------------- TRACKBAR DEPTH CHANGE void __fastcall TForm1::TrackBarDepthChange(TObject *Sender) { Form1->EditDepth->Text = TrackBarDepth->Position; depth = abs(TrackBarDepth->Position - 250); } //---------------------------------------------------------------- FORM PAINT void __fastcall TForm1::FormPaint(TObject *Sender) { //drawBox(); } //------------------------------------------------------------------- THE END