Vistas de página en total

sábado, 15 de octubre de 2011

Fractal de ecena

esta aplicación es un paisaje en movimiento, dibujo un sol y dos lineas que simulan la playa y el relieve, aplico color para simular el paisaje.
este es el código del fractal

#define MAXLEVEL 6
#define MAXSIZE 1000
#define WATER 1
#define SUN 2
#define SKY 3
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
 int x1;
double frctl[MAXSIZE];
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------
 void TForm1::fractal(int y1,int y2,int maxlevel,double h,double scale)
{
        int first, last;
        double ratio,std;

        first=0;
        last=(int)pow(2.0,(double)maxlevel);
        frctl[first]=y1;
        frctl[last]=y2;
        ratio=1.0/pow(2.0,h);
        std=scale*ratio;
        subdivide(first,last,std,ratio);
}
void TForm1::subdivide(int p1,int p2,double std,double ratio)
{
        int midpnt;
        double stdmid;
        midpnt = (p1 + p2) / 2;
        if(midpnt != p1 && midpnt != p2)
        {
                frctl[midpnt] = (frctl[p1] + frctl[p2]) / 2 + (double)((random(16)-8)) /8.0*std;
                stdmid = std * ratio;
                subdivide(p1,midpnt,stdmid,ratio);
                subdivide(midpnt,p2,stdmid,ratio);
        }
}
void TForm1::draw_fractal()
{
        int i,x,xinc,l;

        l=(int)pow(2.0,(double)MAXLEVEL);
        xinc = cw / l * 3 / 2;
        Form1->PaintBox1->Canvas->MoveTo(.5,100);
        for(i=0,x=0;i<l;i++,x+=xinc)
                Form1->PaintBox1->Canvas->LineTo(x,(int)frctl[i]);

}
void __fastcall TForm1::FormCreate(TObject *Sender)
{
        cw=Form1->PaintBox1->ClientWidth;
        ch=Form1->PaintBox1->ClientHeight;
        x1=50;    
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
     x1++;
        randomize();

       Form1->PaintBox1->Canvas->Brush->Color=clOlive;
        Form1->PaintBox1->Canvas->Rectangle(0,0,cw,ch);
        Form1->PaintBox1->Canvas->Brush->Color=clBlue;//Color del cielo
        Form1->PaintBox1->Canvas->Pen->Color=clMaroon;//contorno de la montaña
        fractal(100,100,MAXLEVEL,.75,50.0);
        draw_fractal();

        Form1->PaintBox1->Canvas->FloodFill(1,2,clMaroon,fsBorder); //fsSurface ó fsBorder
        Form1->PaintBox1->Canvas->Pen->Color=clGreen;


        fractal(170,170,MAXLEVEL,0.9,30.0);
        draw_fractal();           // (TColor)WATER,fsSurface
        Form1->PaintBox1->Canvas->Brush->Color=clSkyBlue;
        Form1->PaintBox1->Canvas->FloodFill(1,ch-2,clGreen,fsBorder);

        Form1->PaintBox1->Canvas->Brush->Color=clYellow;//relleno del sol

        Form1->PaintBox1->Canvas->Ellipse(5+x1,5,40+x1,40);
        Form1->PaintBox1->Canvas->FloodFill(cw-100    ,10,(TColor)SUN,fsSurface);


}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
   Timer1->Enabled=true;
}
//------------------------


Este es el resultado:



aquí puedes descargar

No hay comentarios:

Publicar un comentario