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

分形几何

开发平台:

Visual C++

  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include "fractus2_04A.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)[2];
  12. double sx,sy;
  13. double leng0;
  14. double gene_1[][2]={{0.0,1.0},{90.0,1.0},{-90.0,1.0},{-90.0,2.0},
  15.                     {90.0,1.0},{90.0,1.0},{-90.0,1.0},END};
  16. double gene_2[][2]={{90.0,1.0},{-90.0,2.0},END};
  17. double gene_3[][2]={{0.0,1.0},{85.0,1.0},{-170.0,1.0},
  18.                     {85.0,2.0},END};
  19. double gene_4[][2]={{0.0,2.0},{90.0,1.0},{150.0,1.0},
  20.                     {90.0,2.0},END};
  21. double gene_5[][2]={{0.0,0.6},{80.0,1.0},{-180.0,1.0},{20.0,1.0},
  22.                     {-180.0,1.0},{-100.0,0.55},{80.0,0.9},{-180.0,0.9},
  23.                     {20.0,0.9},{-180.0,0.9},{-100.0,0.5},{80.0,0.8},
  24.                     {-180.0,0.8},{20.0,0.8},{-180.0,0.8},{-100.0,0.45},
  25.                     {80.0,0.7},{-180.0,0.7},{20.0,0.7},{-180.0,0.7},
  26.                     {-100.0,0.4},{80.0,0.6},{-180.0,0.6},{20.0,0.6},
  27.                     {-180.0,0.6},{-100.0,0.35},{80.0,0.5},{-180.0,0.5},
  28.                     {20.0,0.5},{-180.0,0.5},{-100.0,0.3},{80.0,0.4},
  29.                     {-180.0,0.4},{20.0,0.4},{-180.0,0.4},{-100.0,0.25},
  30.                     {80.0,0.3},{-180.0,0.3},{20.0,0.3},{-180.0,0.3},
  31.                     {-100.0,0.2},{80.0,0.2},{-180.0,0.2},{20.2,0.2},
  32.                     {-180.0,0.2},{-100.0,0.15},{80.0,0.1},{-180.0,0.1},
  33.                     {20.0,0.1},{-180.0,0.1},{-100.0,0.1},END};
  34. double delta=0.0001;TRect rect;
  35. //---------------------------------------------------------------------------
  36. __fastcall TForm1::TForm1(TComponent* Owner)
  37.         : TForm(Owner)
  38. {
  39. }
  40. //---------------------------------------------------------------------------
  41. void __fastcall fractal(double leng)
  42. {
  43.   int i;
  44.   if(leng<=a) move(leng);
  45.   else
  46.    {turn(-180*t/PI);
  47.     for(i=0;i<n_gene;i++)
  48.      {turn(gene[i][0]);
  49.       if((gene[i][0]+180.0)>delta||(gene[i][0]+180.0)<-delta)
  50.         fractal(gene[i][1]*leng/a);
  51.       else warp(gene[i][1]*leng/a);
  52.      }
  53.     turn(-s+180*t/PI);
  54.    }
  55. }
  56. void __fastcall TForm1::FormCreate(TObject *Sender)
  57. {
  58.   Image1->Height=0;Image1->Top=0;
  59.   Image1->Width=Screen->Width;Image1->Height=Screen->Height;
  60.   rect=Rect(0,0,Image1->Width,Image1->Height);//清屏
  61.   Image1->Canvas->CopyMode=cmBlackness;
  62.   Image1->Canvas->CopyRect(rect,Image1->Canvas,rect);
  63.   Image1->Canvas->Pen->Color=clGreen;
  64.   Image1->Canvas->Brush->Style=bsClear;
  65.   Image1->Canvas->Font->Color=clRed;
  66. }
  67. //---------------------------------------------------------------------------
  68. void __fastcall TForm1::N1Click(TObject *Sender)
  69. {
  70.   int i=0;
  71.   double b=0,c=0.0;
  72.   a=0;ANGLE=0;
  73.   Image1->Canvas->CopyRect(rect,Image1->Canvas,rect);
  74.   while((gene[i][0]-END)>delta||(gene[i][0]-END)<-delta)
  75.    {turn(gene[i][0]);
  76.     a+=gene[i][1]*cos(RADIAN);
  77.     c+=gene[i][1]*sin(RADIAN);
  78.     if((gene[i][0]+180.0)>delta||(gene[i][0]+180.0)<-delta)
  79.       b+=gene[i][1];
  80.     i++;
  81.    }
  82.   n_gene=i;
  83.   s=ANGLE;
  84.   t=atan(c/a);a/=cos(t);
  85.   Image1->Canvas->Rectangle(0,0,99,79);
  86.   setlp(10.0,sy/5);
  87.   setangle(0.0);
  88.   turn(-180*t/PI);
  89.   for(i=0;i<n_gene;i++)
  90.    {turn(gene[i][0]);
  91.     if((gene[i][0]-180.0)>delta||(gene[i][0]-180.0)<-delta)
  92.       move(gene[i][1]*80/a);
  93.     else warp(gene[i][1]*80/a);
  94.    }
  95.   setlp(sx,sy);
  96.   setangle(0.0);
  97.   fractal(leng0);
  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::N4_1Click(TObject *Sender)
  107. {
  108.   sx=120.0;sy=200.0;
  109.   leng0=400.0;
  110.   gene=gene_1;
  111. }
  112. //---------------------------------------------------------------------------
  113. void __fastcall TForm1::N4_2Click(TObject *Sender)
  114. {
  115.   sx=220.0;sy=280.0;
  116.   leng0=200.0;
  117.   gene=gene_2;
  118. }
  119. //---------------------------------------------------------------------------
  120. void __fastcall TForm1::N4_3Click(TObject *Sender)
  121. {
  122.   sx=120.0;sy=280.0;
  123.   leng0=400.0;
  124.   gene=gene_3;
  125. }
  126. //---------------------------------------------------------------------------
  127. void __fastcall TForm1::N4_4Click(TObject *Sender)
  128. {
  129.   sx=120.0;sy=300.0;
  130.   leng0=400.0;
  131.   gene=gene_4;
  132. }
  133. //---------------------------------------------------------------------------
  134. void __fastcall TForm1::N4_5Click(TObject *Sender)
  135. {
  136.   sx=120.0;sy=200.0;
  137.   leng0=400.0;
  138.   gene=gene_5;
  139. }
  140. //---------------------------------------------------------------------------
  141. void __fastcall TForm1::N3Click(TObject *Sender)
  142. {
  143.   Close();        
  144. }
  145. //---------------------------------------------------------------------------