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

分形几何

开发平台:

Visual C++

  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include "fractus2_03.h"
  5. //---------------------------------------------------------------------------
  6. #pragma package(smart_init)
  7. #pragma resource "*.dfm"
  8. TForm1 *Form1;
  9. #include "glib.h"
  10. double a=0.0,s,t;//1/a为缩小率
  11. int n_gene;double *gene;
  12. int N;
  13. double sx,sy;
  14. double leng0;
  15. double gene_1[]={0.0,90.0,-150.0,90.0,END};
  16. double gene_2[]={90.0,-90.0,END};
  17. double gene_3[]={0.0,90.0,-90.0,-90.0,90.0,-90.0,90.0,90.0,-90.0,END};
  18. double gene_4[]={0.0,120.0,-150.0,120.0,-150.0,120.0,END};
  19. double gene_5[]={22.5,45.0,-180.0,112.5,-45.0,-180.0,-112.5,END};
  20. double delta=0.0001;TRect rect;
  21. //---------------------------------------------------------------------------
  22. __fastcall TForm1::TForm1(TComponent* Owner)
  23.         : TForm(Owner)
  24. {
  25. }
  26. //---------------------------------------------------------------------------
  27. void __fastcall fractal(double leng,int n)
  28. {
  29.   int i;
  30.   if(n>=N) move(leng);
  31.   else
  32.    {turn(-180*t/PI);
  33.     for(i=0;i<n_gene;i++)
  34.      {turn(gene[i]);
  35.       if((gene[i]+180.0)>delta||(gene[i]+180.0)<-delta)
  36.         fractal(leng/a,n+1);
  37.       else warp(leng/a);
  38.      }
  39.     turn(-s+180*t/PI);
  40.    }
  41. }
  42. void __fastcall TForm1::FormCreate(TObject *Sender)
  43. {
  44.   Image1->Height=0;Image1->Top=0;
  45.   Image1->Width=Screen->Width;Image1->Height=Screen->Height;
  46.   rect=Rect(0,0,Image1->Width,Image1->Height);//清屏
  47.   Image1->Canvas->CopyMode=cmBlackness;
  48.   Image1->Canvas->CopyRect(rect,Image1->Canvas,rect);
  49.   Image1->Canvas->Pen->Color=clGreen;
  50.   Image1->Canvas->Brush->Style=bsClear;
  51.   Image1->Canvas->Font->Color=clRed;
  52. }
  53. //---------------------------------------------------------------------------
  54. void __fastcall TForm1::N1Click(TObject *Sender)
  55. {
  56.   char str[10];
  57.   int b=0,i=0;
  58.   double c=0.0,D;
  59.   a=0;ANGLE=0;StrCopy(str,"D=");
  60.   Image1->Canvas->CopyRect(rect,Image1->Canvas,rect);
  61.   while((gene[i]-END)>delta||(gene[i]-END)<-delta)
  62.    {turn(gene[i]);
  63.     a+=cos(RADIAN);c+=sin(RADIAN);
  64.     if((gene[i]+180.0)>delta||(gene[i]+180.0)<-delta)
  65.       b++;
  66.     i++;
  67.    }
  68.   n_gene=i;
  69.   s=ANGLE;
  70.   t=atan(c/a);a/=cos(t);
  71.   D=log10((double)b)/log10(a);
  72.   Image1->Canvas->Rectangle(0,0,99,79);
  73.   setlp(10.0,sy/5);
  74.   setangle(0.0);
  75.   turn(-180*t/PI);
  76.   for(i=0;i<n_gene;i++)
  77.    {turn(gene[i]);
  78.     if((gene[i]-180.0)>delta||(gene[i]-180.0)<-delta)
  79.       move(80/a);
  80.     else warp(80/a);
  81.    }
  82.   StrPCopy(str+2,FloatToStrF(D,ffGeneral,5,1));
  83.   Image1->Canvas->TextOut(18,90,str);
  84.   setlp(sx,sy);
  85.   setangle(0.0);
  86.   fractal(leng0,0);
  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::N4_1Click(TObject *Sender)
  96. {
  97.   N=6;
  98.   sx=120.0;sy=240.0;
  99.   leng0=400.0;
  100.   gene=gene_1;
  101. }
  102. //---------------------------------------------------------------------------
  103. void __fastcall TForm1::N4_2Click(TObject *Sender)
  104. {
  105.   N=16;
  106.   sx=220.0;sy=300.0;
  107.   leng0=200.0;
  108.   gene=gene_2;
  109. }
  110. //---------------------------------------------------------------------------
  111. void __fastcall TForm1::N4_3Click(TObject *Sender)
  112. {
  113.   N=4;
  114.   sx=120.0;sy=200.0;
  115.   leng0=400.0;
  116.   gene=gene_3;
  117. }
  118. //---------------------------------------------------------------------------
  119. void __fastcall TForm1::N4_4Click(TObject *Sender)
  120. {
  121.   N=5;
  122.   sx=120.0;sy=200.0;
  123.   leng0=400.0;
  124.   gene=gene_4;
  125. }
  126. //---------------------------------------------------------------------------
  127. void __fastcall TForm1::N4_5Click(TObject *Sender)
  128. {
  129.   N=5;
  130.   sx=120.0;sy=200.0;
  131.   leng0=400.0;
  132.   gene=gene_5;
  133. }
  134. //---------------------------------------------------------------------------
  135. void __fastcall TForm1::N3Click(TObject *Sender)
  136. {
  137.   Close();        
  138. }
  139. //---------------------------------------------------------------------------