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

分形几何

开发平台:

Visual C++

  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include "fractus4_03.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=1;
  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.   b1=(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.   putpixel(-sx,-sy,colors[col]);
  40.   putpixel(-sx,sy,colors[col]);
  41.  }
  42. //---------------------------------------------------------------------------
  43. void julia(void)
  44.  {int k;double s;
  45.   static double x[KL+1],y[KL+1];
  46.   x[0]=x0;y[0]=y0;
  47.   for(k=1;k<=KL;k++)
  48.    {x[k]=x[k-1]*x[k-1]-y[k-1]*y[k-1]+xc;
  49.     y[k]=2*x[k-1]*y[k-1]+yc;
  50.     if((x[k]*x[k]+y[k]*y[k])>=4.0)
  51.       return;
  52.    }
  53.   for(k=1;k<=13;k++)
  54.    {if(fabs(x[KL]-x[KL-k])<BOX
  55.      &&fabs(y[KL]-y[KL-k])<BOX)
  56.      {plot(k);return;}
  57.    }
  58.   plot(15);
  59.  }
  60. //---------------------------------------------------------------------------
  61. void __fastcall TForm1::FormCreate(TObject *Sender)
  62. {
  63.   Image1->Height=0;Image1->Top=0;
  64.   Image1->Width=Screen->Width;Image1->Height=Screen->Height;
  65.   rect=Rect(0,0,Image1->Width,Image1->Height);//清屏
  66.   Image1->Canvas->CopyMode=cmBlackness;
  67.   Image1->Canvas->CopyRect(rect,Image1->Canvas,rect);
  68. }
  69. //---------------------------------------------------------------------------
  70. void __fastcall TForm1::N1Click(TObject *Sender)
  71. {
  72.   Image1->Canvas->CopyRect(rect,Image1->Canvas,rect);
  73.   set0(320,200);
  74.   coord();
  75.   Image1->Canvas->Pen->Color=colors[7];
  76.   Image1->Canvas->Pen->Style=psSolid;
  77.   Image1->Canvas->Brush->Style=bsSolid;
  78.   Image1->Canvas->Brush->Color=clBlack;
  79.   Image1->Canvas->Rectangle(-dx,-dy,dx,dy);
  80.   Image1->Canvas->Pen->Style=psDot;
  81.   for(sx=-dx+dx/4;sx<dx;sx+=dx/4)
  82.    {Form1->Image1->Canvas->MoveTo(sx,-dy);
  83.     Form1->Image1->Canvas->LineTo(sx,dy);
  84.    }
  85.   for(sy=-dy+dy/4;sy<dy;sy+=dy/4)
  86.    {Form1->Image1->Canvas->MoveTo(-dx,sy);
  87.     Form1->Image1->Canvas->LineTo(dx,sy);
  88.    }
  89.   for(sx=0;sx<=dx;sx+=STEP)
  90.    {x0=a1*sx+b1;
  91.     for(sy=0;sy<=dy;sy+=STEP)
  92.      {y0=a2*sy+b2;
  93.       julia();
  94.      }
  95.    }
  96. }
  97. //---------------------------------------------------------------------------
  98. void __fastcall TForm1::N2Click(TObject *Sender)
  99. {
  100.   if(SavePictureDialog1->Execute())
  101.     Image1->Picture->SaveToFile(SavePictureDialog1->FileName);
  102. }
  103. //---------------------------------------------------------------------------
  104. void __fastcall TForm1::N3Click(TObject *Sender)
  105. {
  106.   Close();        
  107. }
  108. //---------------------------------------------------------------------------
  109. void __fastcall TForm1::N4_1Click(TObject *Sender)
  110. {
  111.   xc=-0.6;yc=0;
  112. }
  113. //---------------------------------------------------------------------------
  114. void __fastcall TForm1::N4_2Click(TObject *Sender)
  115. {
  116.   xc=-1.1;yc=0;
  117. }
  118. //---------------------------------------------------------------------------
  119. void __fastcall TForm1::N4_3Click(TObject *Sender)
  120. {
  121.   xc=-1.35;yc=0;
  122. }
  123. //---------------------------------------------------------------------------
  124. void __fastcall TForm1::N4_4Click(TObject *Sender)
  125. {
  126.   xc=-1.76;yc=0;
  127. }
  128. //---------------------------------------------------------------------------
  129. void __fastcall TForm1::N4_5Click(TObject *Sender)
  130. {
  131.   xc=-1.65;yc=0;x0=0.2;y0=0;
  132. }
  133. //---------------------------------------------------------------------------
  134. void __fastcall TForm1::N4_6Click(TObject *Sender)
  135. {
  136.   xc=-1.76;yc=0;x0=0.1;y0=0.04;
  137. }
  138. //---------------------------------------------------------------------------