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

分形几何

开发平台:

Visual C++

  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include "fractus3_06.h"
  5. //---------------------------------------------------------------------------
  6. #pragma package(smart_init)
  7. #pragma resource "*.dfm"
  8. TForm1 *Form1;
  9. #include "glib.h"
  10. const int n_rot_MAX=8;
  11. int n_rot;
  12. int N;
  13. double ang,scale,ks;
  14. double r1;
  15. double delta=0.0001;TRect rect;
  16. int colors[16]={0x00000000,0x00FF0000,0x0000FF00,0x00FF8C00,
  17.                 0x000000FF,0x00FF00FF,0x0000FFFF,0x00D7D7D7,
  18.                 0x009BFFFF,0x009BFFFF,0x00A0FF00,0x00FFFF00,
  19.                 0x006E6EFF,0x00FF9BFF,0x009BFFFF,0x00FFFFFF};
  20. //---------------------------------------------------------------------------
  21. __fastcall TForm1::TForm1(TComponent* Owner)
  22.         : TForm(Owner)
  23. {
  24. }
  25. //---------------------------------------------------------------------------
  26. //星形为构成元素的递归图形(1)
  27. void __fastcall star(double r)
  28.  {TPoint points[2*n_rot_MAX+1];
  29.   int i;
  30.   turn(180.0);
  31.   warp(r);
  32.   turn(90.0+ang);points[0]=Point(LPX,LPY);
  33.   for(i=0;i<n_rot;i++)
  34.    {warp(r*ks);
  35.     points[2*i+1]=Point(LPX,LPY);
  36.     turn(-ang);
  37.     warp(r*ks);
  38.     points[2*i+2]=Point(LPX,LPY);
  39.     turn(2*ang);
  40.    }
  41.   turn(90-ang);
  42.   warp(r);
  43.   Form1->Image1->Canvas->Polygon(points,2*n_rot);
  44.  }
  45. //---------------------------------------------------------------------------
  46. void __fastcall recurs(double r,int n)
  47. {
  48.   int i,j,k;
  49.   if(n<=N)
  50.    {Form1->Image1->Canvas->Pen->Color=colors[n];
  51.     Form1->Image1->Canvas->Brush->Color=colors[n];
  52.     star(r);
  53.     if(n==1)
  54.      {j=n_rot;k=0;}
  55.     else
  56.      {j=n_rot-1;k=1;}
  57.     for(i=0;i<j;i++)
  58.      {turn(180.0);
  59.       turn((i+k)*ang);
  60.       warp(r*(1+scale));
  61.       recurs(r*scale,n+1);
  62.       turn(180.0);
  63.       warp(r*(1+scale));
  64.       turn(-(i+k)*ang);
  65.      }
  66.    }
  67.   Form1->Image1->Canvas->Pen->Color=colors[n-1];
  68. }
  69. void __fastcall TForm1::FormCreate(TObject *Sender)
  70. {
  71.   Image1->Height=0;Image1->Top=0;
  72.   Image1->Width=Screen->Width;Image1->Height=Screen->Height;
  73.   rect=Rect(0,0,Image1->Width,Image1->Height);//清屏
  74.   Image1->Canvas->CopyMode=cmBlackness;
  75.   Image1->Canvas->CopyRect(rect,Image1->Canvas,rect);
  76. }
  77. //---------------------------------------------------------------------------
  78. void __fastcall TForm1::N1Click(TObject *Sender)
  79. {
  80.   Image1->Canvas->CopyRect(rect,Image1->Canvas,rect);
  81.   ang=360.0/n_rot;
  82.   ks=tan(PI/n_rot);
  83.   setlp(320.0,200.0);
  84.   setangle(-90.0);
  85.   recurs(r1,1);
  86. }
  87. //---------------------------------------------------------------------------
  88. void __fastcall TForm1::N2Click(TObject *Sender)
  89. {
  90.   if(SavePictureDialog1->Execute())
  91.     Image1->Picture->SaveToFile(SavePictureDialog1->FileName);
  92. }
  93. //---------------------------------------------------------------------------
  94. void __fastcall TForm1::N3Click(TObject *Sender)
  95. {
  96.   Close();        
  97. }
  98. //---------------------------------------------------------------------------
  99. void __fastcall TForm1::N4_1Click(TObject *Sender)
  100. {
  101.   n_rot=5;N=5;
  102.   scale=0.38;r1=90.0;
  103. }
  104. //---------------------------------------------------------------------------
  105. void __fastcall TForm1::N4_2Click(TObject *Sender)
  106. {
  107.   n_rot=6;N=4;
  108.   scale=0.34;r1=100.0;
  109. }
  110. //---------------------------------------------------------------------------
  111. void __fastcall TForm1::N4_3Click(TObject *Sender)
  112. {
  113.   n_rot=8;N=4;
  114.   scale=0.28;r1=110.0;
  115. }
  116. //---------------------------------------------------------------------------