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

分形几何

开发平台:

Visual C++

  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include "fractus4_02.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 UX=4;
  13. int d=160;
  14. double x_min=-2.0,x_max=2.0;
  15. double xc,yc,x0,y0;
  16. double delta=0.0001;TRect rect;
  17. int colors[16]={0x00000000,0x00FF0000,0x0000FF00,0x00FF8C00,
  18.                 0x000000FF,0x00FF00FF,0x0000FFFF,0x00A0A0A0,
  19.                 0x009BFFFF,0x009BFFFF,0x00A0FF00,0x00FFFF00,
  20.                 0x006E6EFF,0x00FF9BFF,0x009BFFFF,0x00FFFFFF};
  21. //---------------------------------------------------------------------------
  22. __fastcall TForm1::TForm1(TComponent* Owner)
  23.         : TForm(Owner)
  24. {
  25. }
  26. //---------------------------------------------------------------------------
  27. //坐标的变动
  28. void __fastcall TForm1::FormCreate(TObject *Sender)
  29. {
  30.   Image1->Height=0;Image1->Top=0;
  31.   Image1->Width=Screen->Width;Image1->Height=Screen->Height;
  32.   rect=Rect(0,0,Image1->Width,Image1->Height);//清屏
  33.   Image1->Canvas->CopyMode=cmBlackness;
  34.   Image1->Canvas->CopyRect(rect,Image1->Canvas,rect);
  35. }
  36. //---------------------------------------------------------------------------
  37. void __fastcall TForm1::N1Click(TObject *Sender)
  38. {
  39.   int sx,sy,i,j,k,k_max,prd,col;
  40.   double a1,b1;
  41.   double xx[20],yy[20],x[KL+1],y[KL+1];
  42.   Image1->Canvas->CopyRect(rect,Image1->Canvas,rect);
  43.   set0(120.0,200.0);
  44.   a1=2*(double)d/(x_max-x_min);
  45.   b1=-(double)d*(x_max+x_min)/(x_max-x_min);
  46.   Image1->Canvas->Pen->Color=colors[7];
  47.   Image1->Canvas->Brush->Style=bsSolid;
  48.   Image1->Canvas->Brush->Color=clBlack;
  49.   for(sx=0;sx<=KL*UX;sx+=KL*UX/10)
  50.    {if(sx==0||sx==KL*UX)
  51.       Image1->Canvas->Pen->Style=psSolid;
  52.     else
  53.       Image1->Canvas->Pen->Style=psDot;
  54.     Form1->Image1->Canvas->MoveTo(sx,-d);
  55.     Form1->Image1->Canvas->LineTo(sx,d);
  56.    }
  57.   for(sy=-d;sy<=d;sy+=d/4)
  58.    {if(sy==-d||sy==0||sy==d)
  59.       Image1->Canvas->Pen->Style=psSolid;
  60.     else
  61.       Image1->Canvas->Pen->Style=psDot;
  62.     Form1->Image1->Canvas->MoveTo(0,sy);
  63.     Form1->Image1->Canvas->LineTo(KL*UX,sy);
  64.    }
  65.   x[0]=x0;y[0]=y0;
  66.   for(k=1;k<=KL;k++)
  67.    {x[k]=x[k-1]*x[k-1]-y[k-1]*y[k-1]+xc;
  68.     y[k]=2*x[k-1]*y[k-1]+yc;
  69.     if((x[k]*x[k]+y[k]*y[k])>=4.0)
  70.      {k_max=k;prd=0;goto BR;}
  71.    }
  72.   k_max=KL;
  73.   for(k=1;k<=13;k++)
  74.    {if(fabs(x[k_max]-x[k_max-k])<BOX
  75.      &&fabs(y[k_max]-y[k_max-k])<BOX)
  76.      {prd=k;goto BR;}
  77.    }
  78.   prd=0;
  79.   BR:for(k=0;k<=k_max;k++)
  80.    {sx=a1*x[k]+b1;
  81.     col=(prd)?(1+k%prd):15;
  82.     Image1->Canvas->Pen->Color=colors[col];
  83.     Image1->Canvas->Brush->Color=colors[col];
  84.     Image1->Canvas->Ellipse(k*UX-2,-sx-2,k*UX+2,-sx+2);
  85.    }
  86.   for(k=0;k<prd;k++)
  87.    {xx[k]=x[k_max-k];
  88.     yy[k]=y[k_max-k];
  89.    }
  90.   for(i=0;i<prd-1;i++)
  91.    {double s;
  92.     for(j=i+1;j<prd;j++)
  93.       if(xx[i]<xx[j])
  94.        {s=xx[i];xx[i]=xx[j];xx[j]=s;
  95.         s=yy[i];yy[i]=yy[j];yy[j]=s;
  96.        }
  97.    }
  98. }
  99. //---------------------------------------------------------------------------
  100. void __fastcall TForm1::N2Click(TObject *Sender)
  101. {
  102.   if(SavePictureDialog1->Execute())
  103.     Image1->Picture->SaveToFile(SavePictureDialog1->FileName);
  104. }
  105. //---------------------------------------------------------------------------
  106. void __fastcall TForm1::N3Click(TObject *Sender)
  107. {
  108.   Close();        
  109. }
  110. //---------------------------------------------------------------------------
  111. void __fastcall TForm1::N4_1Click(TObject *Sender)
  112. {
  113.   xc=-0.6;yc=0;x0=0.2;y0=0.5;
  114. }
  115. //---------------------------------------------------------------------------
  116. void __fastcall TForm1::N4_2Click(TObject *Sender)
  117. {
  118.   xc=-1.1;yc=0;x0=0.2;y0=0.2;
  119. }
  120. //---------------------------------------------------------------------------
  121. void __fastcall TForm1::N4_3Click(TObject *Sender)
  122. {
  123.   xc=-1.35;yc=0;x0=0.2;y0=0.05;
  124. }
  125. //---------------------------------------------------------------------------
  126. void __fastcall TForm1::N4_4Click(TObject *Sender)
  127. {
  128.   xc=-1.39;yc=0;x0=0.05;y0=0.02;
  129. }
  130. //---------------------------------------------------------------------------
  131. void __fastcall TForm1::N4_5Click(TObject *Sender)
  132. {
  133.   xc=-1.65;yc=0;x0=0.2;y0=0;
  134. }
  135. //---------------------------------------------------------------------------
  136. void __fastcall TForm1::N4_6Click(TObject *Sender)
  137. {
  138.   xc=-1.76;yc=0;x0=0.1;y0=0.04;
  139. }
  140. //---------------------------------------------------------------------------