// ========================================================================== // USER DEFINED POLYGONS // based upon NEW TRIANGLES PROGRAM // modified by Jeff Shih // modified by Dave Niebuhr // modified by Nick Gessler 9/28/2005 //=========================================================================== #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 corners = 0; int world[500][500]; int maxHits; int maxScale; int minScale; // ========================================================================== // FUNCTIONS //=========================================================================== //-------------------------------------------------------------- PLOT CORNERS void plotCorners (void) { for (int i = 0; i < corners; i++) { Form1->PaintBox1->Canvas->Pixels[corner[i].x][corner[i].y] = clWhite; } } //---------------------------------------------------------------- INITIALIZE void initialize (void) { corners = 0; } //------------------------------------------------------------ WHITE FUNCTION void white (void) { Form1->PaintBox1->Invalidate(); Application->ProcessMessages(); for (int i = 0; i < 500; i++) { for (int j = 0; j < 500; j++) { if (world[i][j] > 0) { Form1->PaintBox1->Canvas->Pixels[i][j]= clWhite; } } } } //-------------------------------------------------------- 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) { Form1->PaintBox1->Invalidate(); Application->ProcessMessages(); for (int i = 0; i < 500; i++) { for (int j = 0; j < 500; j++) { Form1->PaintBox1->Canvas->Pixels[i][j]= static_cast (grayRamp((maxHits * maxScale) / 100 - (maxHits * minScale / 100), world[i][j])); } } } //------------------------------------------------------- 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) { Form1->PaintBox1->Invalidate(); Application->ProcessMessages(); for (int i = 0; i < 500; i++) { for (int j = 0; j < 500; j++) { Form1->PaintBox1->Canvas->Pixels[i][j]= static_cast (colorRamp((maxHits * maxScale) / 100 - (maxHits * minScale / 100), world[i][j])); } } } // ========================================================================== // EVENT HANDLERS //=========================================================================== //------------------------------------------------------------- ON MOUSE DOWN void __fastcall TForm1::PaintBox1MouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y) { corners++; int count; count = corners - 1; corner[count].x = X; corner[count].y = Y; plotCorners(); // there is a bug if less than 3 corners are entered } //------------------------------------------------------ CLEAR CORNERS BUTTON void __fastcall TForm1::ButtonClearCornersClick(TObject *Sender) { initialize(); } //---------------------------------------------------------- CLEAR ALL BUTTON void __fastcall TForm1::ButtonClearAllClick(TObject *Sender) { Form1->Invalidate(); Form1->PaintBox1->Invalidate(); initialize(); // clear array for (int i = 0; i < 500; i++) { for (int j = 0; j < 500; j++) { world[i][j] = 0; } } } //---------------------------------------------------------------- RUN BUTTON void __fastcall TForm1::ButtonRunClick(TObject *Sender) { Form1->PanelRender->Visible = false; midPoint.x = random(500); midPoint.y = random(500); chosenPoint.x = random(500); chosenPoint.y = random(500); Form1->PaintBox1->Canvas->Pixels[midPoint.x][midPoint.y]=clWhite; Form1->PaintBox1->Canvas->Pixels[chosenPoint.x][chosenPoint.y]=clWhite; maxHits = 0; for (int i = 1; i < 100000; i++) { chosenCorner = (random(corners)); chosenPoint.x = midPoint.x; chosenPoint.y = midPoint.y; 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]=clWhite; world[int(midPoint.x)][int(midPoint.y)] ++; maxHits = max(maxHits, world[int(midPoint.x)][int(midPoint.y)]); } maxScale = 100; Form1->TrackBarMax->Position = maxScale; minScale = 0; Form1->TrackBarMin->Position = minScale; Form1->PanelRender->Visible = true; } //-------------------------------------------------------------- WHITE BUTTON void __fastcall TForm1::ButtonWhiteClick(TObject *Sender) { white(); } //------------------------------------------------------------- MIN TRACK BAR void __fastcall TForm1::TrackBarMinChange(TObject *Sender) { Form1->EditMin->Text = IntToStr(Form1->TrackBarMin->Position); minScale = Form1->TrackBarMin->Position; } //------------------------------------------------------------- MAX TRACK BAR void __fastcall TForm1::TrackBarMaxChange(TObject *Sender) { Form1->EditMax->Text = IntToStr(Form1->TrackBarMax->Position); maxScale = Form1->TrackBarMax->Position; } //--------------------------------------------------------------- GRAY BUTTON void __fastcall TForm1::ButtonGrayClick(TObject *Sender) { gray(); } //-------------------------------------------------------------- COLOR BUTTON void __fastcall TForm1::ButtonColorClick(TObject *Sender) { color(); } //----------------------------------------------------------------------- END //---------------------------------------------------------------------------