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

分形几何

开发平台:

Visual C++

  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include "fractus3_01.h"
  5. //---------------------------------------------------------------------------
  6. #pragma package(smart_init)
  7. #pragma resource "*.dfm"
  8. TForm1 *Form1;
  9. #include "glib.h"
  10. int N=7;
  11. double sx=320.0,sy=390.0;
  12. double leng1;
  13. double (*branch)[2];
  14. double branch_1[][2]={{30.0,0.7},{-30.0,0.7},END};
  15. double branch_2[][2]={{20.0,0.7},{-30.0,0.6},END};
  16. double branch_3[][2]={{45.0,0.7},{15.0,0.7},{-15.0,0.7},{-45.0,0.7},END};
  17. double delta=0.0001;TRect rect;
  18. int colors[16]={0x00000000,0x00FF0000,0x0000FF00,0x00FF8C00,
  19.                 0x000000FF,0x00FF00FF,0x0000FFFF,0x00D7D7D7,
  20.                 0x009BFFFF,0x009BFFFF,0x00A0FF00,0x00FFFF00,
  21.                 0x006E6EFF,0x00FF9BFF,0x009BFFFF,0x00FFFFFF};
  22. //---------------------------------------------------------------------------
  23. __fastcall TForm1::TForm1(TComponent* Owner)
  24.         : TForm(Owner)
  25. {
  26. }
  27. //---------------------------------------------------------------------------
  28. //树木曲线
  29. void __fastcall tree(double leng,int n)
  30. {
  31.   int i=0;
  32.   if(n<=N)
  33.    {Form1->Image1->Canvas->Pen->Color=colors[n];
  34.     if(n==1)
  35.      {move(leng);
  36.       tree(leng,n+1);
  37.      }
  38.     else
  39.       while((branch[i][0]-END)>delta||(branch[i][0]-END)<-delta)
  40.        {turn(branch[i][0]);
  41.         move(branch[i][1]*leng);
  42.         tree(branch[i][1]*leng,n+1);
  43.         turn(180.0);
  44.         warp(branch[i][1]*leng);
  45.         turn(180-branch[i][0]);
  46.         i++;
  47.        }
  48.    }
  49.   else
  50.    {Form1->Image1->Canvas->Pen->Color=clWhite;
  51.     warp(leng*0.3);
  52.     Form1->Image1->Canvas->Ellipse(LPX-leng*0.3,LPY-leng*0.3,
  53.                                    LPX+leng*0.3,LPY+leng*0.3);
  54.     turn(180.0);
  55.     warp(leng*0.3);
  56.     turn(180.0);
  57.    }
  58.   Form1->Image1->Canvas->Pen->Color=colors[n-1];
  59. }
  60. void __fastcall TForm1::FormCreate(TObject *Sender)
  61. {
  62.   Image1->Height=0;Image1->Top=0;
  63.   Image1->Width=Screen->Width;Image1->Height=Screen->Height;
  64.   rect=Rect(0,0,Image1->Width,Image1->Height);//清屏
  65.   Image1->Canvas->CopyMode=cmBlackness;
  66.   Image1->Canvas->CopyRect(rect,Image1->Canvas,rect);
  67. }
  68. //---------------------------------------------------------------------------
  69. void __fastcall TForm1::N1Click(TObject *Sender)
  70. {
  71.   Image1->Canvas->CopyRect(rect,Image1->Canvas,rect);
  72.   setlp(sx,sy);
  73.   setangle(90.0);
  74.   tree(leng1,1);
  75. }
  76. //---------------------------------------------------------------------------
  77. void __fastcall TForm1::N2Click(TObject *Sender)
  78. {
  79.   if(SavePictureDialog1->Execute())
  80.     Image1->Picture->SaveToFile(SavePictureDialog1->FileName);
  81. }
  82. //---------------------------------------------------------------------------
  83. void __fastcall TForm1::N3Click(TObject *Sender)
  84. {
  85.   Close();        
  86. }
  87. //---------------------------------------------------------------------------
  88. void __fastcall TForm1::N4_1Click(TObject *Sender)
  89. {
  90.   leng1=120.0;branch=branch_1;
  91. }
  92. //---------------------------------------------------------------------------
  93. void __fastcall TForm1::N4_2Click(TObject *Sender)
  94. {
  95.   leng1=130.0;branch=branch_2;
  96. }
  97. //---------------------------------------------------------------------------
  98. void __fastcall TForm1::N4_3Click(TObject *Sender)
  99. {
  100.   leng1=120.0;branch=branch_3;        
  101. }
  102. //---------------------------------------------------------------------------