//=========================================================================== // THE REGULAR POLYGON ALGORITHM (BENT) // Halfway There: The Regular Polygon Algorithm (Bent and modified 1/18/2009) // Here we arbitrarily bend the rules by changing a "+" to a "-" // which then requires limiting the size of the plot. // We also have the option of adding a point to the center // and preselecting the number of iterations. //=========================================================================== #include #include //================================================== include the math library #include #include #pragma hdrstop #include "Unit1.h" #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; //------------------------------------------------------------------- Borland __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { } //=========================================================================== // VARIABLES //=========================================================================== struct coor { double x; double y; } ; coor corner[100]; coor chosenPoint; coor midPoint; coor center; const double pi=3.141592; int chosenCorner; int side = 3; int world[600][600]; int maxHits; int percent; int times = 10000000; //=========================================================================== // FUNCTIONS //=========================================================================== //-------------------------------------------------------------- PLOT CORNERS void plotCorners (void) { if (Form1->RadioGroup1->ItemIndex == 0) { Form1->PaintBox1->Canvas->Pixels[300][300] = clSilver; } //Form1->PaintBox1->Canvas->Pen->Color = clSilver; for (int i = 0; i < side; i++) { Form1->PaintBox1->Canvas->Pixels[corner[i].x][corner[i].y] = clSilver; //Ellipse(corner[i].x-2,corner[i].y-2,corner[i].x,corner[i].y); } } //---------------------------------------------------------------- INITIALIZE void initialize (void) { double angle = (2*pi)/side; double turn = 0; // we make the PaintBox larger center.x = 300; center.y = 300; for (int count = 0; count < side; count++) { // we've got to reduce the size of the original figure to +- 100 corner[count].x = center.x + 100 * sin (turn); corner[count].y = center.y - 100 * cos (turn); turn = turn + angle; } if (Form1->RadioGroup1->ItemIndex == 0) { corner[side].x = center.x; corner[side].y = center.y; } plotCorners(); } //------------------------------------------------------- COLOR RAMP FUNCTION int colorRamp(int range, int value) { if (range == 0) range = 1; int pixelDistanceAlongPath = (value * 1792) / range; int red, green, blue; if (pixelDistanceAlongPath < 256) { red=0; green=0; blue=pixelDistanceAlongPath; } else if (pixelDistanceAlongPath < 512) { red =0; green=pixelDistanceAlongPath-256; blue=255; } else if (pixelDistanceAlongPath < 768) { red =0; green =255; blue= 255-(pixelDistanceAlongPath-512); } else if (pixelDistanceAlongPath < 1024) { red= (pixelDistanceAlongPath-768); green =255; blue =0; } else if (pixelDistanceAlongPath <1280) { red =255; green=255-(pixelDistanceAlongPath-1024); blue =0; } else if (pixelDistanceAlongPath < 1536) { red =255; green=0; blue=pixelDistanceAlongPath -1280; } else { red =255; green=pixelDistanceAlongPath-1536; blue =255; } return (RGB(red, green, blue)); } //------------------------------------------------------------ COLOR FUNCTION void color (void) { for (int i = 0; i < 600; i++) { for (int j = 0; j < 600; j++) { Form1->PaintBox1->Canvas->Pixels[i][j]= static_cast (colorRamp((maxHits*percent)/100, world[i][j])); } } } //-------------------------------------------------------- GRAY RAMP FUNCTION int grayRamp(int range, int value) { if (range == 0) range = 1; int distance = ( value * 255 ) / range; return (RGB(distance, distance, distance)); } //------------------------------------------------------------- GRAY FUNCTION void gray (void) { for (int i = 0; i < 600; i++) { for (int j = 0; j < 600; j++) { Form1->PaintBox1->Canvas->Pixels[i][j]= static_cast (grayRamp((maxHits*percent)/100, world[i][j])); } } } //=========================================================================== // EVENT HANDLERS //=========================================================================== //---------------------------------------------------------------- RUN BUTTON void __fastcall TForm1::ButtonRunClick(TObject *Sender) { midPoint.x = random(600); midPoint.y = random(600); chosenPoint.x = random(600); chosenPoint.y = random(600); Form1->PaintBox1->Canvas->Pixels[midPoint.x][midPoint.y]=clSilver; Form1->PaintBox1->Canvas->Pixels[chosenPoint.x][chosenPoint.y]=clSilver; maxHits = 0; for (int i = 1; i < times; i++) { chosenCorner = random(side); if (Form1->RadioGroup1->ItemIndex == 0) { chosenCorner = random(side + 1); } chosenPoint.x = midPoint.x; chosenPoint.y = midPoint.y; // The algorithm bent by replacing the "+" with a "-" midPoint.x = corner[chosenCorner].x - (chosenPoint.x-corner[chosenCorner].x)/2; midPoint.y = corner[chosenCorner].y - (chosenPoint.y-corner[chosenCorner].y)/2; Form1->PaintBox1->Canvas->Pixels[midPoint.x][midPoint.y]=clSilver; world[int(midPoint.x)][int(midPoint.y)] ++; // for some reason "max(a, b)" doesn't work... if (maxHits < world[int(midPoint.x)][int(midPoint.y)]) { maxHits = world[int(midPoint.x)][int(midPoint.y)]; } } percent = 100; Form1->TrackBarRange->Position = percent; Form1->ButtonRun->Visible = false; Form1->PanelRender->Visible = true; } //-------------------------------------------------------- ENTER SIDES BUTTON void __fastcall TForm1::ButtonEnterSidesClick(TObject *Sender) { Form1->Invalidate(); initialize(); ButtonRun->Visible = true; Application->ProcessMessages(); for (int i = 0; i < 600; i++) { for (int j = 0; j < 600; j++) { world[i][j] = 0; } } plotCorners(); Form1->PanelRender->Visible = false; } //----------------------------------------------------------- SIDES TRACK BAR void __fastcall TForm1::TrackBarSidesChange(TObject *Sender) { Form1->EditSides->Text = " " + IntToStr(TrackBarSides->Position); side = TrackBarSides->Position; } //-------------------------------------------------------------- COLOR BUTTON void __fastcall TForm1::ButtonColorClick(TObject *Sender) { Form1->PaintBox1->Invalidate(); Application->ProcessMessages(); color(); } //--------------------------------------------------------------- GRAY BUTTON void __fastcall TForm1::ButtonGrayClick(TObject *Sender) { Form1->PaintBox1->Invalidate(); Application->ProcessMessages(); gray(); } //----------------------------------------------------------- RANGE TRACK BAR void __fastcall TForm1::TrackBarRangeChange(TObject *Sender) { Form1->EditRange->Text = " " + IntToStr(Form1->TrackBarRange->Position); percent = Form1->TrackBarRange->Position; } //---------------------------------------------------- RADIO GROUP ITERATIONS void __fastcall TForm1::RadioGroupIterationsClick(TObject *Sender) { if (RadioGroupIterations->ItemIndex == 0) times = 1000000; if (RadioGroupIterations->ItemIndex == 1) times = 10000000; if (RadioGroupIterations->ItemIndex == 2) times = 100000000; } //---------------------------------------------------------------------------