//--------------------------------------------------------------------------- #include #pragma hdrstop #include "Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; int zoomLevel=1; double xstart=-3; double ystart=-2; int maxiteration = 200 ; int grayRamp(int value, int range) { if (range == 0) range = 1; int distance = ( value * 255 ) / range; return (RGB(distance, distance, distance)); } int colorRamp(int part, int whole) { int pixelDistanceAlongPath = (part * 1792) / whole; int red, green, blue; // Which edge of the color cube are we on? if (pixelDistanceAlongPath < 256) { // Edge 1 from BLACK to BLUE red=0; green=0; blue=pixelDistanceAlongPath; } else if (pixelDistanceAlongPath < 512) { // Edge 2 from BLUE to CYAN red =0; green=pixelDistanceAlongPath-256; blue=255; } else if (pixelDistanceAlongPath < 768) { // Edge 3 from CYAN to GREEN red =0; green =255; blue= 255-(pixelDistanceAlongPath-512); } else if (pixelDistanceAlongPath < 1024) { // Edge 4 from GREEN to YELLOW red= (pixelDistanceAlongPath-768); green =255; blue =0; } else if (pixelDistanceAlongPath <1280) { // Edge 5 from YELLOW to RED red =255; green=255-(pixelDistanceAlongPath-1024); blue =0; } else if (pixelDistanceAlongPath < 1536) { // Edge 6 from RED to MAGENTA red =255; green=0; blue=pixelDistanceAlongPath -1280; } else { // Edge 7 from MAGENTA to WHITE red =255; green=pixelDistanceAlongPath-1536; blue =255; } return (RGB(red, green, blue)); } void renderPix(int iX, int iY, double X, double Y, double zoom) { double xC=(xstart)+double((double(iX)+.5)/100/zoom); double yC=(ystart)+double((double(iY)-.5)/100/zoom); double x0=(xstart)+double((double(iX)+.5)/100/zoom); double y0=(ystart)+double((double(iY)-.5)/100/zoom); double x2 = xC*xC; double y2 = yC*yC; int iteration = 0; while ( (x2 + y2) < (2*2) && iteration < maxiteration ) { yC = 2*xC*yC + y0; xC = x2 - y2 + x0; x2 = xC*xC; y2 = yC*yC; iteration++; } if ( iteration == maxiteration ) Form1->PaintBox1->Canvas->Pixels[iX][iY]=clBlack; else Form1->PaintBox1->Canvas->Pixels[iX][iY]= static_cast (colorRamp(iteration, maxiteration)); } void Render(void) { for (int i=0; i < 500; i++) { for (int j=0; j<500; j++) { renderPix(i,j,xstart,ystart,zoomLevel); } } } //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { } //--------------------------------------------------------------------------- void __fastcall TForm1::Button1Click(TObject *Sender) { Render(); } //--------------------------------------------------------------------------- void __fastcall TForm1::PaintBox1MouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y) { if(Button==0) { xstart=(xstart)+(double(X)/100/double(zoomLevel))-1.25/double(zoomLevel); ystart=(ystart)+(double(Y)/100/double(zoomLevel))-1.25/double(zoomLevel); zoomLevel*=2; } else { xstart=(xstart)+(double(X)/100/double(zoomLevel))-5/double(zoomLevel); ystart=(ystart)+(double(Y)/100/double(zoomLevel))-5/double(zoomLevel); zoomLevel/=2; } Render(); } //--------------------------------------------------------------------------- void __fastcall TForm1::TrackBar1Change(TObject *Sender) { maxiteration=Form1->TrackBar1->Position; } //--------------------------------------------------------------------------- void __fastcall TForm1::Button2Click(TObject *Sender) { zoomLevel=1; xstart=-2; ystart=-2; Render(); } //---------------------------------------------------------------------------