Blog hecho para la Materia de Graficacion de la carrera de ing. en sistemas computacionales del Instituto Tecnologico de la Laguna. Las publicaciones de este blog seran conforme vallamos avanzando en el curso, asi que se publicara codigo e imagenes para que sean de provecho para todos que quieran aprender.
domingo, 25 de septiembre de 2011
Escena Fractal
En este fractal se utilzan 3 metodos para dibujarlo, los cuales en 2 de ellos se usa recursividad. Abajo el codigo. El fractal se puede generar mediante un timer (automaticamente) o con un boton. Usted elige.
class TForm1 : public TForm
{
__published: // IDE-managed Components
TPaintBox *PaintBox1;
TButton *Button1;
TTimer *Timer1;
void __fastcall Button1Click(TObject *Sender);
void __fastcall Timer1Timer(TObject *Sender);
private: // User declarations
void subdivide(int first, int last, double std, double ratio);
void fractal(int y1, int y2, int maxlevel, double h, double scale);
void draw_fractal(void);
public: // User declarations
__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
#include <vcl.h>
#include <math.h>
#pragma hdrstop
#include "EscenaFractal.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
const int MAXSIZE = 1000;
const int MAXLEVEL = 6;
double frct1[MAXSIZE];
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void TForm1::subdivide(int p1, int p2, double std, double ratio)
{
int midpnt;
double stdmid;
midpnt = (p1 + p2) / 2;
if(midpnt != p1 && midpnt != p2)
{
frct1[midpnt] = (frct1[p1] + frct1[p2]) / 2 +
(double)((random(16) - 8)) / 8.0 * std;
stdmid = std * ratio;
subdivide(p1, midpnt, stdmid, ratio);
subdivide(midpnt, p2, stdmid, ratio);
}
}
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);
frct1[first] = y1;
frct1[last] = y2;
ratio = 1.0 / pow(2.0,h);
std = scale * ratio;
subdivide(first, last, std, ratio);
}
void TForm1::draw_fractal(void)
{
int i, x, xinc, l;
//int cw = Form1->ClientWidth;
l = (int)pow(2.0, (double)MAXLEVEL);
xinc = ClientWidth / l * 3 / 2;
Form1->Canvas->MoveTo(0, 100);
for(i = 0, x = 0; i < l; i++, x +=xinc)
{
Form1->Canvas->LineTo(x, (int)frct1[i]);
}
}
void __fastcall TForm1::Button1Click(TObject *Sender)
{
randomize();
PaintBox1->Refresh();
PaintBox1->Canvas->Rectangle(0,0, PaintBox1->Width, PaintBox1->Height);
PaintBox1->Canvas->Brush->Color = clSkyBlue;
fractal(100,100, MAXLEVEL, 0.5, 50.0);
draw_fractal();
PaintBox1->Canvas->FloodFill(1,1, clBlack, fsBorder);
PaintBox1->Canvas->Brush->Color = clBlue; //(TColor);
fractal(170, 170, MAXLEVEL, 0.9, 30.0);
draw_fractal();
PaintBox1->Canvas->FloodFill(1, 240, clBlack, fsBorder);
PaintBox1->Canvas->Brush->Color = clYellow; //;(TColor)SUN;
PaintBox1->Canvas->Ellipse(250, 20, 290, 60);
PaintBox1->Canvas->FloodFill(270, 50, clBlack, fsBorder);
PaintBox1->Canvas->Brush->Color = clGreen;
PaintBox1->Canvas->FloodFill(270, 150, clBlack, fsBorder);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
randomize();
PaintBox1->Refresh();
PaintBox1->Canvas->Rectangle(0,0, PaintBox1->Width, PaintBox1->Height);
PaintBox1->Canvas->Brush->Color = clSkyBlue;
fractal(100,100, MAXLEVEL, 0.5, 50.0);
draw_fractal();
PaintBox1->Canvas->FloodFill(1,1, clBlack, fsBorder);
PaintBox1->Canvas->Brush->Color = clBlue; //(TColor);
fractal(170, 170, MAXLEVEL, 0.9, 30.0);
draw_fractal();
PaintBox1->Canvas->FloodFill(1, 240, clBlack, fsBorder);
PaintBox1->Canvas->Brush->Color = clYellow; //;(TColor)SUN;
PaintBox1->Canvas->Ellipse(250, 20, 290, 60);
PaintBox1->Canvas->FloodFill(270, 50, clBlack, fsBorder);
PaintBox1->Canvas->Brush->Color = clGreen;
PaintBox1->Canvas->FloodFill(270, 150, clBlack, fsBorder);
}
//---------------------------------------------------------------------------
Suscribirse a:
Enviar comentarios (Atom)
No hay comentarios:
Publicar un comentario