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

分形几何

开发平台:

Visual C++

  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include "fractus2_05.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. int N;
  13. double sx,sy;
  14. double leng0;
  15. double gene_1[][2]={{0.0,1.0},{-90.0,-1.0},END};
  16. double gene_2[][2]={{60.0,-1.0},{-60.0,1.0},{-60.0,-1.0},END};
  17. double delta=0.0001;TRect rect;
  18. //---------------------------------------------------------------------------
  19. __fastcall TForm1::TForm1(TComponent* Owner)
  20.         : TForm(Owner)
  21. {
  22. }
  23. //---------------------------------------------------------------------------
  24. void __fastcall fractal(double leng,int n)
  25. {
  26.   int i;
  27.   if(n>=N) move(leng);
  28.   else
  29.    {turn(-180*t/PI);
  30.     for(i=0;i<n_gene;i++)
  31.      {turn(gene[i][0]);
  32.       if((gene[i][0]+180.0)>delta||(gene[i][0]+180.0)<-delta)
  33.         if(gene[i][1]<0)
  34.          {warp(-gene[i][1]*leng/a);
  35.           turn(180.0);
  36.           fractal(-gene[i][1]*leng/a,n+1);
  37.           turn(180.0);
  38.           warp(-gene[i][1]*leng/a);
  39.          }
  40.         else fractal(gene[i][1]*leng/a,n+1);
  41.       else warp(fabs(gene[i][1])*leng/a);
  42.      }
  43.     turn(-s+180*t/PI);
  44.    }
  45. }
  46. void __fastcall TForm1::FormCreate(TObject *Sender)
  47. {
  48.   Image1->Height=0;Image1->Top=0;
  49.   Image1->Width=Screen->Width;Image1->Height=Screen->Height;
  50.   rect=Rect(0,0,Image1->Width,Image1->Height);//清屏
  51.   Image1->Canvas->CopyMode=cmBlackness;
  52.   Image1->Canvas->CopyRect(rect,Image1->Canvas,rect);
  53.   Image1->Canvas->Pen->Color=clGreen;
  54.   Image1->Canvas->Brush->Style=bsClear;
  55.   Image1->Canvas->Font->Color=clRed;
  56. }
  57. //---------------------------------------------------------------------------
  58. void __fastcall TForm1::N1Click(TObject *Sender)
  59. {
  60.   int i=0;
  61.   double b=0,c=0.0;
  62.   a=0;ANGLE=0;
  63.   Image1->Canvas->CopyRect(rect,Image1->Canvas,rect);
  64.   while((gene[i][0]-END)>delta||(gene[i][0]-END)<-delta)
  65.    {turn(gene[i][0]);
  66.     a+=fabs(gene[i][1])*cos(RADIAN);
  67.     c+=fabs(gene[i][1])*sin(RADIAN);
  68.     if((gene[i][0]+180.0)>delta||(gene[i][0]+180.0)<-delta)
  69.       b+=fabs(gene[i][1]);
  70.     i++;
  71.    }
  72.   n_gene=i;
  73.   s=ANGLE;
  74.   t=atan(c/a);a/=cos(t);
  75.   Image1->Canvas->Rectangle(0,0,99,79);
  76.   setlp(10.0,sy/5);
  77.   setangle(0.0);
  78.   turn(-180*t/PI);
  79.   for(i=0;i<n_gene;i++)
  80.    {turn(gene[i][0]);
  81.     if((gene[i][0]-180.0)>delta||(gene[i][0]-180.0)<-delta)
  82.       move(fabs(gene[i][1])*80/a);
  83.     else warp(fabs(gene[i][1])*80/a);
  84.    }
  85.   setlp(sx,sy);
  86.   setangle(0.0);
  87.   fractal(leng0,0);
  88. }
  89. //---------------------------------------------------------------------------
  90. void __fastcall TForm1::N2Click(TObject *Sender)
  91. {
  92.   if(SavePictureDialog1->Execute())
  93.     Image1->Picture->SaveToFile(SavePictureDialog1->FileName);
  94. }
  95. //---------------------------------------------------------------------------
  96. void __fastcall TForm1::N4_1Click(TObject *Sender)
  97. {
  98.   N=16;
  99.   sx=220.0;sy=300.0;
  100.   leng0=200.0;
  101.   gene=gene_1;
  102. }
  103. //---------------------------------------------------------------------------
  104. void __fastcall TForm1::N4_2Click(TObject *Sender)
  105. {
  106.   N=9;
  107.   sx=170.0;sy=320.0;
  108.   leng0=300.0;
  109.   gene=gene_2;
  110. }
  111. //---------------------------------------------------------------------------
  112. void __fastcall TForm1::N3Click(TObject *Sender)
  113. {
  114.   Close();        
  115. }
  116. //---------------------------------------------------------------------------