C#實現一個貝賽爾藝術

看C# Windows程序設計的第十三章,作者Petzold實現了一個bezier art,然後我稍微改變了一下顏色變換,覺得非查的神奇,這裏將代碼貼出來,需要申明的是,這個程序代碼絕大部分是原作者的:
  1. //BezierArt.cs
  2. //build BezierArt.cs PrintableForm.cs /main:BezierArt
  3. using System;
  4. using System.Drawing;
  5. using System.Windows.Forms;
  6. class BezierArt : PrintableForm
  7. {
  8.     const int iNum = 100;
  9.     float r = 1;
  10.     float g = 1;
  11.     float b = 1;
  12.     public new static void Main()
  13.     {
  14.         Application.Run(new BezierArt());
  15.     }
  16.     public BezierArt()
  17.     {
  18.         this.Text  = "Bezier Art";
  19.     }
  20.     protected override void DoPage(Graphics grfx, Color clr, int cx, int cy)
  21.     {
  22.         Pen      pen  = new Pen(clr);
  23.         PointF[] aptf = new PointF[4];
  24.         Random   rand = new Random();
  25.         for (int i=0; i<iNum; i++)
  26.         {
  27.             double dAngle = 2*i*Math.PI/iNum;
  28.             aptf[0].X =   cx/2+cx/ 2*(float)Math.Cos(dAngle);
  29.             aptf[0].Y = 5*cy/8+cy/16*(float)Math.Sin(dAngle);
  30.             
  31.             aptf[1]   = new PointF(cx/2,  -cy);
  32.             aptf[2]   = new PointF(cx/2, 2*cy);
  33.             
  34.             dAngle   += Math.PI;
  35.             
  36.             aptf[3].X = cx/2+cx/ 4*(float)Math.Cos(dAngle);
  37.             aptf[3].Y = cy/2+cy/16*(float)Math.Sin(dAngle);
  38.             //clr = Color.FromArgb(rand.Next(256), rand.Next(256), rand.Next(256));
  39.             //pen = new Pen(clr);
  40.             
  41.             g += 0.2f;
  42.             b += 0.3f;
  43.             r += 0.4f;
  44.             clr = Color.FromArgb(((int)r)%256, ((int)g)%256, ((int)b)%256);
  45.             pen = new Pen(clr);
  46.             grfx.DrawBeziers(pen, aptf);
  47.         }
  48.     }
  49. }
  50. /**
  51.  */

然後得到了一些好看的結果:
019786dc464543285982ddb0.jpg
079fc50668f908d17b8947b1.jpg
6267462db2db47f08a1399b3.jpg
fbd41d32424105e41a4cffbc.jpg
224f5217de24a15620a4e9bd.jpg
8d211b7a9f0495e80ad187bb.jpg
f0ed342c1927c628349bf784.jpg

這裏的圖片不是一個程序生成的,而是對顏色變換作過修改之後得到的。




發佈了97 篇原創文章 · 獲贊 4 · 訪問量 17萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章