fractus4_04.cpp
资源名称:fenxin.rar [点击查看]
上传用户:hzcygd
上传日期:2022-04-30
资源大小:109k
文件大小:3k
源码类别:
分形几何
开发平台:
Visual C++
- //---------------------------------------------------------------------------
- #include <vcl.h>
- #pragma hdrstop
- #include "fractus4_04.h"
- //---------------------------------------------------------------------------
- #pragma package(smart_init)
- #pragma resource "*.dfm"
- TForm1 *Form1;
- #include "glib.h"
- const int KL=300;
- const double BOX=0.01;
- const int STEP=4;
- int sx,sy,dx=192,dy=192;
- double x_min=-2.25,x_max=0.75,y_min=-1.5,y_max=1.5;
- double x0,y0;
- double a1,b1,a2,b2;
- double delta=0.0001;TRect rect;
- int colors[16]={0x00000000,0x00FF0000,0x0000FF00,0x00FF8C00,
- 0x000000FF,0x00FF00FF,0x0000FFFF,0x00A0A0A0,
- 0x009BFFFF,0x009BFFFF,0x00A0FF00,0x00FFFF00,
- 0x006E6EFF,0x00FF9BFF,0x009BFFFF,0x00FFFFFF};
- //---------------------------------------------------------------------------
- __fastcall TForm1::TForm1(TComponent* Owner)
- : TForm(Owner)
- {
- }
- //---------------------------------------------------------------------------
- //曼代尔波集合
- void __fastcall coord(void)
- {a1=(x_max-x_min)/(2*dx);
- b1=(x_max+x_min)/2;
- a2=(y_max-y_min)/(2*dy);
- b2=(y_max+y_min)/2;
- }
- //---------------------------------------------------------------------------
- void __fastcall plot(int col)
- {putpixel(sx,-sy,colors[col]);
- putpixel(sx,sy,colors[col]);
- }
- //---------------------------------------------------------------------------
- void mandelbrot(void)
- {int k;double s;
- static double x[KL+1],y[KL+1];
- x[0]=x0;y[0]=y0;
- for(k=1;k<=KL;k++)
- {x[k]=x[k-1]*x[k-1]-y[k-1]*y[k-1]+x0;
- y[k]=2*x[k-1]*y[k-1]+y0;
- if((x[k]*x[k]+y[k]*y[k])>=4.0)
- return;
- }
- for(k=1;k<=13;k++)
- {if(fabs(x[KL]-x[KL-k])<BOX
- &&fabs(y[KL]-y[KL-k])<BOX)
- {plot(k);return;}
- }
- plot(15);
- }
- //---------------------------------------------------------------------------
- void __fastcall TForm1::FormCreate(TObject *Sender)
- {
- Image1->Height=0;Image1->Top=0;
- Image1->Width=Screen->Width;Image1->Height=Screen->Height;
- rect=Rect(0,0,Image1->Width,Image1->Height);//清屏
- Image1->Canvas->CopyMode=cmBlackness;
- Image1->Canvas->CopyRect(rect,Image1->Canvas,rect);
- }
- //---------------------------------------------------------------------------
- void __fastcall TForm1::N1Click(TObject *Sender)
- {
- Image1->Canvas->CopyRect(rect,Image1->Canvas,rect);
- set0(320,200);
- coord();
- Image1->Canvas->Pen->Color=colors[7];
- Image1->Canvas->Pen->Style=psSolid;
- Image1->Canvas->Brush->Style=bsSolid;
- Image1->Canvas->Brush->Color=clBlack;
- Image1->Canvas->Rectangle(-dx,-dy,dx,dy);
- Image1->Canvas->Pen->Style=psDot;
- for(sx=-dx+dx/6;sx<dx;sx+=dx/3)
- {Form1->Image1->Canvas->MoveTo(sx,-dy);
- Form1->Image1->Canvas->LineTo(sx,dy);
- }
- for(sy=-dy+dy/3;sy<dy;sy+=dy/3)
- {Form1->Image1->Canvas->MoveTo(-dx,sy);
- Form1->Image1->Canvas->LineTo(dx,sy);
- }
- for(sx=-dx;sx<=dx;sx+=STEP)
- {x0=a1*sx+b1;
- for(sy=0;sy<=dy;sy+=STEP)
- {y0=a2*sy+b2;
- mandelbrot();
- }
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TForm1::N2Click(TObject *Sender)
- {
- if(SavePictureDialog1->Execute())
- Image1->Picture->SaveToFile(SavePictureDialog1->FileName);
- }
- //---------------------------------------------------------------------------
- void __fastcall TForm1::N3Click(TObject *Sender)
- {
- Close();
- }
- //---------------------------------------------------------------------------