看C# Windows程序设计的第十三章,作者Petzold实现了一个bezier art,然后我稍微改变了一下颜色变换,觉得非查的神奇,这里将代码贴出来,需要申明的是,这个程序代码绝大部分是原作者的:
然后得到了一些好看的结果:
这里的图片不是一个程序生成的,而是对颜色变换作过修改之后得到的。
- //BezierArt.cs
- //build BezierArt.cs PrintableForm.cs /main:BezierArt
- using System;
- using System.Drawing;
- using System.Windows.Forms;
- class BezierArt : PrintableForm
- {
- const int iNum = 100;
- float r = 1;
- float g = 1;
- float b = 1;
- public new static void Main()
- {
- Application.Run(new BezierArt());
- }
- public BezierArt()
- {
- this.Text = "Bezier Art";
- }
- protected override void DoPage(Graphics grfx, Color clr, int cx, int cy)
- {
- Pen pen = new Pen(clr);
- PointF[] aptf = new PointF[4];
- Random rand = new Random();
- for (int i=0; i<iNum; i++)
- {
- double dAngle = 2*i*Math.PI/iNum;
- aptf[0].X = cx/2+cx/ 2*(float)Math.Cos(dAngle);
- aptf[0].Y = 5*cy/8+cy/16*(float)Math.Sin(dAngle);
- aptf[1] = new PointF(cx/2, -cy);
- aptf[2] = new PointF(cx/2, 2*cy);
- dAngle += Math.PI;
- aptf[3].X = cx/2+cx/ 4*(float)Math.Cos(dAngle);
- aptf[3].Y = cy/2+cy/16*(float)Math.Sin(dAngle);
- //clr = Color.FromArgb(rand.Next(256), rand.Next(256), rand.Next(256));
- //pen = new Pen(clr);
- g += 0.2f;
- b += 0.3f;
- r += 0.4f;
- clr = Color.FromArgb(((int)r)%256, ((int)g)%256, ((int)b)%256);
- pen = new Pen(clr);
- grfx.DrawBeziers(pen, aptf);
- }
- }
- }
- /**
- */
然后得到了一些好看的结果:
这里的图片不是一个程序生成的,而是对颜色变换作过修改之后得到的。