fractus4_03_b.cpp
上传用户:hzcygd
上传日期:2022-04-30
资源大小:109k
文件大小:5k
源码类别:

分形几何

开发平台:

Visual C++

  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include "fractus4_03_b.h"
  5. //---------------------------------------------------------------------------
  6. #pragma package(smart_init)
  7. #pragma resource "*.dfm"
  8. TForm1 *Form1;
  9. #include "glib.h"
  10. const int KL=100;
  11. const double BOX=0.01;
  12. const int STEP=4;
  13. int sx,sy,dx=192,dy=192;
  14. double x_min=-2.0,x_max=2.0,y_min=-2.0,y_max=2.0;
  15. double xc,yc,x0,y0;
  16. double a1,b1,a2,b2;
  17. double delta=0.0001;TRect rect;
  18. int colors[16]={0x00000000,0x00FF0000,0x0000FF00,0x00FF8C00,
  19.                 0x000000FF,0x00FF00FF,0x0000FFFF,0x00A0A0A0,
  20.                 0x009BFFFF,0x009BFFFF,0x00A0FF00,0x00FFFF00,
  21.                 0x006E6EFF,0x00FF9BFF,0x009BFFFF,0x00FFFFFF};
  22. //---------------------------------------------------------------------------
  23. __fastcall TForm1::TForm1(TComponent* Owner)
  24.         : TForm(Owner)
  25. {
  26. }
  27. //---------------------------------------------------------------------------
  28. //Zc为复数的朱利亚集合
  29. void __fastcall coord(void)
  30.  {a1=(x_max-x_min)/(2*dx);
  31.   b1=(x_max+x_min)/2;
  32.   a2=(y_max-y_min)/(2*dy);
  33.   b2=(y_max+y_min)/2;
  34.  }
  35. //---------------------------------------------------------------------------
  36. void __fastcall plot(int col)
  37.  {putpixel(sx,-sy,colors[col]);
  38.   putpixel(-sx,sy,colors[col]);
  39.  }
  40. //---------------------------------------------------------------------------
  41. void julia(void)
  42.  {int k;double s;
  43.   static double x[KL+1],y[KL+1];
  44.   x[0]=x0;y[0]=y0;
  45.   for(k=1;k<=KL;k++)
  46.    {x[k]=x[k-1]*x[k-1]-y[k-1]*y[k-1]+xc;
  47.     y[k]=2*x[k-1]*y[k-1]+yc;
  48.     if((x[k]*x[k]+y[k]*y[k])>=4.0)
  49.       return;
  50.    }
  51.   for(k=1;k<=13;k++)
  52.    {if(fabs(x[KL]-x[KL-k])<BOX
  53.      &&fabs(y[KL]-y[KL-k])<BOX)
  54.      {plot(k);return;}
  55.    }
  56.   plot(15);
  57.  }
  58. //---------------------------------------------------------------------------
  59. void __fastcall TForm1::FormCreate(TObject *Sender)
  60. {
  61.   Image1->Height=0;Image1->Top=0;
  62.   Image1->Width=Screen->Width;Image1->Height=Screen->Height;
  63.   rect=Rect(0,0,Image1->Width,Image1->Height);//清屏
  64.   Image1->Canvas->CopyMode=cmBlackness;
  65.   Image1->Canvas->CopyRect(rect,Image1->Canvas,rect);
  66. }
  67. //---------------------------------------------------------------------------
  68. void __fastcall TForm1::N1Click(TObject *Sender)
  69. {
  70.   Image1->Canvas->CopyRect(rect,Image1->Canvas,rect);
  71.   set0(320,200);
  72.   coord();
  73.   Image1->Canvas->Pen->Color=colors[7];
  74.   Image1->Canvas->Pen->Style=psSolid;
  75.   Image1->Canvas->Brush->Style=bsSolid;
  76.   Image1->Canvas->Brush->Color=clBlack;
  77.   Image1->Canvas->Rectangle(-dx,-dy,dx,dy);
  78.   Image1->Canvas->Pen->Style=psDot;
  79.   for(sx=-dx+dx/4;sx<dx;sx+=dx/4)
  80.    {Form1->Image1->Canvas->MoveTo(sx,-dy);
  81.     Form1->Image1->Canvas->LineTo(sx,dy);
  82.    }
  83.   for(sy=-dy+dy/4;sy<dy;sy+=dy/4)
  84.    {Form1->Image1->Canvas->MoveTo(-dx,sy);
  85.     Form1->Image1->Canvas->LineTo(dx,sy);
  86.    }
  87.   for(sx=0;sx<=dx;sx+=STEP)
  88.    {x0=a1*sx+b1;
  89.     for(sy=-dy;sy<=dy;sy+=STEP)
  90.      {y0=a2*sy+b2;
  91.       julia();
  92.      }
  93.    }
  94. }
  95. //---------------------------------------------------------------------------
  96. void __fastcall TForm1::N2Click(TObject *Sender)
  97. {
  98.   if(SavePictureDialog1->Execute())
  99.     Image1->Picture->SaveToFile(SavePictureDialog1->FileName);
  100. }
  101. //---------------------------------------------------------------------------
  102. void __fastcall TForm1::N3Click(TObject *Sender)
  103. {
  104.   Close();        
  105. }
  106. //---------------------------------------------------------------------------
  107. void __fastcall TForm1::N4_1Click(TObject *Sender)
  108. {
  109.   xc=0;yc=0.6;
  110. }
  111. //---------------------------------------------------------------------------
  112. void __fastcall TForm1::N4_2Click(TObject *Sender)
  113. {
  114.   xc=-1.1;yc=0.2;
  115. }
  116. //---------------------------------------------------------------------------
  117. void __fastcall TForm1::N4_3Click(TObject *Sender)
  118. {
  119.   xc=-0.1;yc=0.77;
  120. }
  121. //---------------------------------------------------------------------------
  122. void __fastcall TForm1::N4_4Click(TObject *Sender)
  123. {
  124.   xc=0.3;yc=0.5;
  125. }
  126. //---------------------------------------------------------------------------
  127. void __fastcall TForm1::N4_5Click(TObject *Sender)
  128. {
  129.   xc=-0.52;yc=0.55;
  130. }
  131. //---------------------------------------------------------------------------
  132. void __fastcall TForm1::N4_6Click(TObject *Sender)
  133. {
  134.   xc=-1.15;yc=0.25;
  135. }
  136. //---------------------------------------------------------------------------
  137. void __fastcall TForm1::N4_7Click(TObject *Sender)
  138. {
  139.   xc=0.12;yc=0.61;        
  140. }
  141. //---------------------------------------------------------------------------
  142. void __fastcall TForm1::N4_8Click(TObject *Sender)
  143. {
  144.   xc=-0.37;yc=0.61;        
  145. }
  146. //---------------------------------------------------------------------------