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

分形几何

开发平台:

Visual C++

  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include "fractus4_05.h"
  5. //---------------------------------------------------------------------------
  6. #pragma package(smart_init)
  7. #pragma resource "*.dfm"
  8. TForm1 *Form1;
  9. #include "glib.h"
  10. const int KL=300;
  11. const double BOX=0.01;
  12. const int STEP=1;
  13. int sx,sy,dx=192,dy=192;
  14. double x_min,x_max,y_min,y_max;
  15. double x0,y0;
  16. double a1,b1,a2,b2;
  17. int (*col)[2];
  18. int col_1[][2]={{6,4},{8,1},{12,2},{18,5},{KL+1,6}},
  19.     col_2[][2]={{18,4},{24,1},{36,2},{54,5},{KL+1,6}},
  20.     col_3[][2]={{30,4},{36,1},{48,2},{66,5},{KL+1,6}};
  21. double delta=0.0001;TRect rect;
  22. int colors[16]={0x00000000,0x00FF0000,0x0000FF00,0x00FF8C00,
  23.                 0x000000FF,0x00FF00FF,0x0000FFFF,0x00A0A0A0,
  24.                 0x009BFFFF,0x009BFFFF,0x00A0FF00,0x00FFFF00,
  25.                 0x006E6EFF,0x00FF9BFF,0x009BFFFF,0x00FFFFFF};
  26. //---------------------------------------------------------------------------
  27. __fastcall TForm1::TForm1(TComponent* Owner)
  28.         : TForm(Owner)
  29. {
  30. }
  31. //---------------------------------------------------------------------------
  32. //按发散区域分类的曼代尔波集合
  33. void __fastcall coord(void)
  34.  {a1=(x_max-x_min)/(2*dx);
  35.   b1=(x_max+x_min)/2;
  36.   a2=(y_max-y_min)/(2*dy);
  37.   b2=(y_max+y_min)/2;
  38.  }
  39. //---------------------------------------------------------------------------
  40. int __fastcall getcol(int k)
  41.  {int i=0;
  42.   while(k>col[i][0]&&k<=KL)
  43.     i++;
  44.   return col[i][1];
  45.  }
  46. //---------------------------------------------------------------------------
  47. void __fastcall plot(int col)
  48.  {putpixel(sx,-sy,colors[col]);
  49.   putpixel(sx,sy,colors[col]);
  50.  }
  51. //---------------------------------------------------------------------------
  52. void mandelbrot(void)
  53.  {int k;double s;
  54.   static double x[KL+1],y[KL+1];
  55.   x[0]=x0;y[0]=y0;
  56.   for(k=1;k<=KL;k++)
  57.    {x[k]=x[k-1]*x[k-1]-y[k-1]*y[k-1]+x0;
  58.     y[k]=2*x[k-1]*y[k-1]+y0;
  59.     if((x[k]*x[k]+y[k]*y[k])>=4.0)
  60.      {plot(getcol(k));return;}
  61.    }
  62.  }
  63. //---------------------------------------------------------------------------
  64. void __fastcall TForm1::FormCreate(TObject *Sender)
  65. {
  66.   Image1->Height=0;Image1->Top=0;
  67.   Image1->Width=Screen->Width;Image1->Height=Screen->Height;
  68.   rect=Rect(0,0,Image1->Width,Image1->Height);//清屏
  69.   Image1->Canvas->CopyMode=cmBlackness;
  70.   Image1->Canvas->CopyRect(rect,Image1->Canvas,rect);
  71. }
  72. //---------------------------------------------------------------------------
  73. void __fastcall TForm1::N1Click(TObject *Sender)
  74. {
  75.   Image1->Canvas->CopyRect(rect,Image1->Canvas,rect);
  76.   set0(320,200);
  77.   coord();
  78.   for(sx=-dx;sx<=dx;sx+=STEP)
  79.    {x0=a1*sx+b1;
  80.     for(sy=0;sy<=dy;sy+=STEP)
  81.      {y0=a2*sy+b2;
  82.       mandelbrot();
  83.      }
  84.    }
  85. }
  86. //---------------------------------------------------------------------------
  87. void __fastcall TForm1::N2Click(TObject *Sender)
  88. {
  89.   if(SavePictureDialog1->Execute())
  90.     Image1->Picture->SaveToFile(SavePictureDialog1->FileName);
  91. }
  92. //---------------------------------------------------------------------------
  93. void __fastcall TForm1::N3Click(TObject *Sender)
  94. {
  95.   Close();        
  96. }
  97. //---------------------------------------------------------------------------
  98. void __fastcall TForm1::N4_1Click(TObject *Sender)
  99. {
  100.   x_min=-2.2;x_max=0.6;
  101.   y_min=-1.4;y_max=1.4;
  102.   col=col_1;
  103. }
  104. //---------------------------------------------------------------------------
  105. void __fastcall TForm1::N4_2Click(TObject *Sender)
  106. {
  107.   x_min=-1.8;x_max=-1.74;
  108.   y_min=-0.03;y_max=0.03;
  109.   col=col_2;
  110. }
  111. //---------------------------------------------------------------------------
  112. void __fastcall TForm1::N4_3Click(TObject *Sender)
  113. {
  114.   x_min=-1.49;x_max=-1.47;
  115.   y_min=-0.01;y_max=0.01;
  116.   col=col_3;
  117. }
  118. //---------------------------------------------------------------------------