Nadesłany przez Tomasz Lubiński, 18 sierpnia 2008 01:00
Kod przedstawiony poniżej przedstawia główną część rozwiązania problemu.Pobierz pełne rozwiązanie.
Jeżeli nie odpowiada Ci sposób formatowania kodu przez autora skorzystaj z pretty printer'a i dostosuj go automatycznie do siebie.
Fraktale - zbior Julii - C++/Julia.cpp:
// Fraktale - zbior Julii // www.algorytm.org // Tomasz Lubinski (c) 2008 //--------------------------------------------------------------------------- #include <vcl.h> #pragma hdrstop #include "Julia.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { } #define MIN(a,b) ((a<b)?a:b) #define MAX(a,b) ((a>b)?a:b) //--------------------------------------------------------------------------- //Describes places to render double ratioX, ratioY; double minX, minY, maxX, maxY; int downX, downY; //colors TColor colors[121]; //--------------------------------------------------------------------------- //for HSV colors void HSV2RGB(float hue, float sat, float val, float &red, float &grn, float &blu) { int i; float f, p, q, t; if(val==0) {red=0; grn=0; blu=0;} else{ hue/=60; i = (int)(hue); f = hue-i; p = val*(1-sat); q = val*(1-(sat*f)); t = val*(1-(sat*(1-f))); if (i==0) {red=val; grn=t; blu=p;} else if (i==1) {red=q; grn=val; blu=p;} else if (i==2) {red=p; grn=val; blu=t;} else if (i==3) {red=p; grn=q; blu=val;} else if (i==4) {red=t; grn=p; blu=val;} else if (i==5) {red=val; grn=p; blu=q;} } } //initialize array with colors used to color different levels void initializeColors() { int level; float r, g, b; for (level=0; level<120; level++) { HSV2RGB(3.1*level, 0.85, 0.6, r, g, b); colors[level] = (TColor)(int)(r*255) + ((int)(g*255) << 8) + ((int)(b*255) << 16); } colors[120] = (TColor)0; } //type for complex numbers typedef struct complex_type { double real; double imaginary; } complex_t; //calculate squared modus of given complex c double complexModSq(complex_t c) { return c.real*c.real + c.imaginary*c.imaginary; } //function z[0]=p complex_t f(complex_t p) { return p; } //function z[n+1] = z[n]^2 + c complex_t g(complex_t z, complex_t c) { complex_t result; result.real = z.real*z.real - z.imaginary*z.imaginary + c.real; result.imaginary = 2*z.real*z.imaginary + c.imaginary; return result; } //value is inside set in the returned level int levelSet(complex_t p, complex_t c) { complex_t z; int iteration; iteration = 0; z = f(p); do { z = g(z, c); iteration++; } while (complexModSq(z) < 4 && iteration < 120); return iteration; } //generate fractal void __fastcall TForm1::Button1Click(TObject *Sender) { int i, j, level; complex_t p, c; minX = StrToFloat(minx->Text); minY = StrToFloat(miny->Text); maxX = StrToFloat(maxx->Text); maxY = StrToFloat(maxy->Text); c.real = StrToFloat(cRe->Text); c.imaginary = StrToFloat(cIm->Text); ratioX = (maxX - minX) / Fractal->Width; ratioY = (maxY - minY) / Fractal->Height; for (i=0; i<Fractal->Height; i++) { p.imaginary = i*ratioY + minY; for (j=0; j<Fractal->Width; j++) { p.real = j*ratioX + minX; level = levelSet(p, c); Fractal->Canvas->Pixels[j][i] = colors[level]; } } Fractal->Refresh(); } //--------------------------------------------------------------------------- void __fastcall TForm1::FormCreate(TObject *Sender) { minx->Text = FloatToStr(-1.5); maxx->Text = FloatToStr(1.5); miny->Text = FloatToStr(-1.25); maxy->Text = FloatToStr(1.25); Predefined->ItemIndex = 0; cRe->Text = FloatToStr(-0.123); cIm->Text = FloatToStr(0.745); minX = StrToFloat(minx->Text); minY = StrToFloat(miny->Text); maxX = StrToFloat(maxx->Text); maxY = StrToFloat(maxy->Text); ratioX = (maxX - minX) / Fractal->Width; ratioY = (maxY - minY) / Fractal->Height; //initialize array with colors initializeColors(); //render new fractal Button1Click(Sender); } //--------------------------------------------------------------------------- void __fastcall TForm1::FractalMouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y) { downX = X; downY = Y; Selection->Width = 0; Selection->Height = 0; Selection->Visible = true; } //--------------------------------------------------------------------------- void __fastcall TForm1::FractalMouseUp(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y) { //remove selection Selection->Visible = false; //get new range to render minx->Text = FloatToStr(MIN(downX, X)*ratioX + minX); maxx->Text = FloatToStr(MAX(downX, X)*ratioX + minX); miny->Text = FloatToStr(MIN(downY, Y)*ratioY + minY); maxy->Text = FloatToStr(MAX(downY, Y)*ratioY + minY); minX = StrToFloat(minx->Text); minY = StrToFloat(miny->Text); maxX = StrToFloat(maxx->Text); maxY = StrToFloat(maxy->Text); //render new fractal Button1Click(Sender); } //--------------------------------------------------------------------------- void __fastcall TForm1::FractalMouseMove(TObject *Sender, TShiftState Shift, int X, int Y) { //if left mouse button is held then draw selection if (Shift.Contains(ssLeft)) { Selection->Width = abs(downX - X); Selection->Height = abs(downY - Y); Selection->Left = Fractal->Left + MIN(downX, X); Selection->Top = Fractal->Top + MIN(downY, Y); } } //--------------------------------------------------------------------------- void __fastcall TForm1::PredefinedChange(TObject *Sender) { switch (Predefined->ItemIndex) { case 0: cRe->Text = FloatToStr(-0.123); cIm->Text = FloatToStr(0.745); break; case 1: cRe->Text = FloatToStr(-0.75); cIm->Text = FloatToStr(0.0); break; case 2: cRe->Text = FloatToStr(-0.390541); cIm->Text = FloatToStr(-0.586788); break; case 3: cRe->Text = FloatToStr(0); cIm->Text = FloatToStr(1); break; } //redraw fractal Button1Click(Sender); } //---------------------------------------------------------------------------