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

分形几何

开发平台:

Visual C++

  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include "fractus4_05_b.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=4;
  13. int sx,sy,dx=192,dy=192;
  14. double x_min,x_max,y_min,y_max;
  15. double x0,y0,xc,yc;
  16. double a1,b1,a2,b2;
  17. int (*col)[2];
  18. int col_1[][2]={{2,2},{4,4},{8,1},{16,6},{32,3},{KL+1,5}},
  19.     col_2[][2]={{2,1},{4,2},{8,4},{16,3},{32,5},{KL+1,6}},
  20.     col_3[][2]={{2,4},{4,1},{8,2},{16,5},{32,6},{KL+1,3}},
  21.     col_4[][2]={{2,2},{4,5},{8,3},{16,4},{32,1},{KL+1,6}},
  22.     col_5[][2]={{2,1},{4,6},{8,3},{16,4},{36,2},{81,5},{KL+1,7}},
  23.     col_6[][2]={{2,3},{4,6},{8,5},{16,2},{32,4},{64,1},{KL+1,7}};
  24. double delta=0.0001;TRect rect;
  25. int colors[16]={0x00000000,0x00FF0000,0x0000FF00,0x00FF8C00,
  26.                 0x000000FF,0x00FF00FF,0x0000FFFF,0x00A0A0A0,
  27.                 0x009BFFFF,0x009BFFFF,0x00A0FF00,0x00FFFF00,
  28.                 0x006E6EFF,0x00FF9BFF,0x009BFFFF,0x00FFFFFF};
  29. //---------------------------------------------------------------------------
  30. __fastcall TForm1::TForm1(TComponent* Owner)
  31.         : TForm(Owner)
  32. {
  33. }
  34. //---------------------------------------------------------------------------
  35. //按发散区域分类的朱利亚集合
  36. void __fastcall coord(void)
  37.  {a1=(x_max-x_min)/(2*dx);
  38.   b1=(x_max+x_min)/2;
  39.   a2=(y_max-y_min)/(2*dy);
  40.   b2=(y_max+y_min)/2;
  41.  }
  42. //---------------------------------------------------------------------------
  43. int __fastcall getcol(int k)
  44.  {int i=0;
  45.   while(k>col[i][0]&&k<=KL)
  46.     i++;
  47.   return col[i][1];
  48.  }
  49. //---------------------------------------------------------------------------
  50. void __fastcall plot(int col)
  51.  {putpixel(sx,-sy,colors[col]);
  52.  }
  53. //---------------------------------------------------------------------------
  54. void julia(void)
  55.  {int k;double s;
  56.   static double x[KL+1],y[KL+1];
  57.   x[0]=x0;y[0]=y0;
  58.   for(k=1;k<=KL;k++)
  59.    {x[k]=x[k-1]*x[k-1]-y[k-1]*y[k-1]+xc;
  60.     y[k]=2*x[k-1]*y[k-1]+yc;
  61.     if((x[k]*x[k]+y[k]*y[k])>=4.0)
  62.      {plot(getcol(k));return;}
  63.    }
  64.  }
  65. //---------------------------------------------------------------------------
  66. void __fastcall TForm1::FormCreate(TObject *Sender)
  67. {
  68.   Image1->Height=0;Image1->Top=0;
  69.   Image1->Width=Screen->Width;Image1->Height=Screen->Height;
  70.   rect=Rect(0,0,Image1->Width,Image1->Height);//清屏
  71.   Image1->Canvas->CopyMode=cmBlackness;
  72.   Image1->Canvas->CopyRect(rect,Image1->Canvas,rect);
  73. }
  74. //---------------------------------------------------------------------------
  75. void __fastcall TForm1::N1Click(TObject *Sender)
  76. {
  77.   Image1->Canvas->CopyRect(rect,Image1->Canvas,rect);
  78.   set0(320,200);
  79.   coord();
  80.   for(sx=-dx;sx<=dx;sx+=STEP)
  81.    {x0=a1*sx+b1;
  82.     for(sy=-dy;sy<=dy;sy+=STEP)
  83.      {y0=a2*sy+b2;
  84.       julia();
  85.      }
  86.    }
  87. }
  88. //---------------------------------------------------------------------------
  89. void __fastcall TForm1::N2Click(TObject *Sender)
  90. {
  91.   if(SavePictureDialog1->Execute())
  92.     Image1->Picture->SaveToFile(SavePictureDialog1->FileName);
  93. }
  94. //---------------------------------------------------------------------------
  95. void __fastcall TForm1::N3Click(TObject *Sender)
  96. {
  97.   Close();        
  98. }
  99. //---------------------------------------------------------------------------
  100. void __fastcall TForm1::N4_1Click(TObject *Sender)
  101. {
  102.   x_min=-1.4;x_max=1.4;
  103.   y_min=-1.4;y_max=1.4;
  104.   xc=0.38;yc=0.15;
  105.   col=col_1;
  106. }
  107. //---------------------------------------------------------------------------
  108. void __fastcall TForm1::N4_2Click(TObject *Sender)
  109. {
  110.   x_min=-1.6;x_max=1.6;
  111.   y_min=-1.6;y_max=1.6;
  112.   xc=-0.704;yc=0.28;
  113.   col=col_2;
  114. }
  115. //---------------------------------------------------------------------------
  116. void __fastcall TForm1::N4_3Click(TObject *Sender)
  117. {
  118.   x_min=-0.27;x_max=0.13;
  119.   y_min=0.51;y_max=0.91;
  120.   xc=-0.704;yc=0.28;
  121.   col=col_2;
  122. }
  123. //---------------------------------------------------------------------------
  124. void __fastcall TForm1::N4_4Click(TObject *Sender)
  125. {
  126.   x_min=-0.76;x_max=0.76;
  127.   y_min=-0.76;y_max=0.76;
  128.   xc=-1.27;yc=0.04;
  129.   col=col_3;
  130. }
  131. //---------------------------------------------------------------------------
  132. void __fastcall TForm1::N4_5Click(TObject *Sender)
  133. {
  134.   x_min=-1.5;x_max=1.5;
  135.   y_min=-1.5;y_max=1.5;
  136.   xc=-0.035;yc=0.795;
  137.   col=col_4;
  138. }
  139. //---------------------------------------------------------------------------
  140. void __fastcall TForm1::N4_6Click(TObject *Sender)
  141. {
  142.   x_min=-0.38;x_max=0.12;
  143.   y_min=0.65;y_max=1.15;
  144.   xc=0.035;yc=0.795;
  145.   col=col_4;
  146. }
  147. //---------------------------------------------------------------------------
  148. void __fastcall TForm1::N4_7Click(TObject *Sender)
  149. {
  150.   x_min=-1.6;x_max=1.6;
  151.   y_min=-1.6;y_max=1.6;
  152.   xc=-0.6;yc=0.425;
  153.   col=col_5;
  154. }
  155. //---------------------------------------------------------------------------
  156. void __fastcall TForm1::N4_8Click(TObject *Sender)
  157. {
  158.   x_min=-1.5;x_max=1.5;
  159.   y_min=-1.5;y_max=1.5;
  160.   xc=-0.23;yc=0.67;
  161.   col=col_6;
  162. }
  163. //---------------------------------------------------------------------------