algorytm.org

Implementacja w C/C++



Baza Wiedzy
wersja offline serwisu przeznaczona na urządzenia z systemem Android
Darowizny
darowiznaWspomóż rozwój serwisu
Nagłówki RSS
Artykuły
Implementacje
Komentarze
Forum
Bookmarki






Sonda
Implementacji w jakim języku programowania poszukujesz?

Zbiór Phoenix Julia - Implementacja w C/C++
Ocena użytkownikóww: *****  / 1
SłabyŚwietny
Nadesłany przez Tomasz Lubiński, 15 lipca 2009 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 Phoenix typu Julia - C++/PhoenixJulia.cpp:
// Fraktale - zbior Phoenix typu Julia
// www.algorytm.org
// Tomasz Lubinski (c) 2009
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop
#include "PhoenixJulia.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;
//---------------------------------------------------------------------------

//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;}
}
}

//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 + Re(c) + Im(c)*Zn-1
complex_t g(complex_t zp, complex_t z, complex_t c)
{
        complex_t result;
        result.real = z.real*z.real - z.imaginary*z.imaginary + c.real + c.imaginary*zp.real;
        result.imaginary = 2*z.real*z.imaginary + c.imaginary*zp.imaginary;
        return result;
}

//value is inside set in the returned level
TColor levelSet(complex_t p, complex_t c)
{
        complex_t z_next, z, z_prev;
        int iteration;
        float rv, gv, bv, vv;

        iteration = 0;
        z_prev.real = 0;
        z_prev.imaginary = 0;
        z.real = 0;
        z.imaginary = 0;
        z_next = f(p);
        do
        {
                z_prev = z;
                z = z_next;
                z_next = g(z_prev, z, c);
                iteration++;
        } while (complexModSq(z) < 4 && iteration < 120);

        if (iteration == 120)
        {
                return (TColor)0;
        }
        vv = (complexModSq(z)/100);
        if (vv>0.5) vv = 0.5;
        HSV2RGB(3.1*iteration, 0.75, 0.5+vv, rv, gv, bv);
        return (TColor)(int)(rv*255) + ((int)(gv*255) << 8) + ((int)(bv*255) << 16);
}

//generate fractal
void __fastcall TForm1::Button1Click(TObject *Sender)
{
        int i, j;
        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;
                        Fractal->Canvas->Pixels[j][i] = levelSet(p, c);
                }
        }
        Fractal->Refresh();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
        minx->Text = FloatToStr(-1.5);
        maxx->Text = FloatToStr(1.5);
        miny->Text = FloatToStr(-1.5);
        maxy->Text = FloatToStr(1.5);

        cRe->Text = FloatToStr(0.56667);
        cIm->Text = FloatToStr(-0.5);

        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;

        //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);
        }
}
//---------------------------------------------------------------------------


Dodaj komentarz