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

分形几何

开发平台:

Visual C++

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