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

分形几何

开发平台:

Visual C++

  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include "fractus3_07.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=4;
  12. double ang,scale,kr,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. //星形为构成元素的递归图形(2)
  26. void __fastcall star(double r)
  27.  {TPoint points[16];
  28.   int i;
  29.   turn(180.0);
  30.   warp(r);
  31.   turn(180-ang);
  32.   for(i=0;i<n_rot;i++)
  33.    {move(r*ks);
  34.     turn(180-2*ang);
  35.    }
  36.   turn(ang);
  37.   warp(r);
  38.  }
  39. //---------------------------------------------------------------------------
  40. void __fastcall recurs(double r,int n)
  41. {
  42.   int i,j,k;
  43.   if(n<=N)
  44.    {Form1->Image1->Canvas->Pen->Color=colors[n];
  45.     Form1->Image1->Canvas->Brush->Color=colors[n];
  46.     star(r);
  47.     if(n==1)
  48.      {j=n_rot;k=0;}
  49.     else
  50.      {j=n_rot-1;k=1;}
  51.     for(i=0;i<j;i++)
  52.      {turn(180.0);
  53.       turn((i+k)*kr*ang);
  54.       warp(r*(1+scale));
  55.       recurs(r*scale,n+1);
  56.       turn(180.0);
  57.       warp(r*(1+scale));
  58.       turn(-(i+k)*kr*ang);
  59.      }
  60.    }
  61.   Form1->Image1->Canvas->Pen->Color=colors[n-1];
  62. }
  63. void __fastcall TForm1::FormCreate(TObject *Sender)
  64. {
  65.   Image1->Height=0;Image1->Top=0;
  66.   Image1->Width=Screen->Width;Image1->Height=Screen->Height;
  67.   rect=Rect(0,0,Image1->Width,Image1->Height);//清屏
  68.   Image1->Canvas->CopyMode=cmBlackness;
  69.   Image1->Canvas->CopyRect(rect,Image1->Canvas,rect);
  70. }
  71. //---------------------------------------------------------------------------
  72. void __fastcall TForm1::N1Click(TObject *Sender)
  73. {
  74.   Image1->Canvas->CopyRect(rect,Image1->Canvas,rect);
  75.   if(!(n_rot%2))
  76.    {ang=180.0/n_rot;
  77.     ks=2*cos(PI/n_rot);
  78.     kr=2;
  79.    }
  80.   else
  81.    {ang=90.0/n_rot;
  82.     ks=2*cos(PI/(n_rot*2));
  83.     kr=4;
  84.    }
  85.   setlp(320.0,200.0);
  86.   setangle(-90.0);
  87.   recurs(r1,1);
  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::N3Click(TObject *Sender)
  97. {
  98.   Close();        
  99. }
  100. //---------------------------------------------------------------------------
  101. void __fastcall TForm1::N4_1Click(TObject *Sender)
  102. {
  103.   n_rot=7;
  104.   scale=0.3;r1=110.0;
  105. }
  106. //---------------------------------------------------------------------------
  107. void __fastcall TForm1::N4_2Click(TObject *Sender)
  108. {
  109.   n_rot=8;
  110.   scale=0.28;r1=110.0;
  111. }
  112. //---------------------------------------------------------------------------
  113. void __fastcall TForm1::N4_3Click(TObject *Sender)
  114. {
  115.   n_rot=9;
  116.   scale=0.25;r1=120.0;
  117. }
  118. //---------------------------------------------------------------------------