BLOG.CSHARPHELPER.COM: Draw a curlicue fractal in C#
Draw a curlicue fractal in C#
See Curlicue Fractal by Eric W. Weisstein from MathWorld, a Wolfram Web Resource.
The program starts by drawing a line segment in the direction of the X axis. It then iteratively calculates theta and phi using these equations:
theta(n + 1) = (theta(n) + 2 * Pi * S) Mod (2 * Pi) phi(n + 1) = (theta(n) + phi(n)) Mod (2 * Pi)
for some irrational number S. At each step, the program draws a new line segment in the direction given by phi.
Subroutine DrawCurlicue draws 10,000 segments following these rules.
// Draw the curve. private void DrawCurlicue(Graphics gr) { const int SCALE = 2; gr.ScaleTransform( SCALE, SCALE, MatrixOrder.Append); gr.TranslateTransform( picCanvas.ClientSize.Width / 2, picCanvas.ClientSize.Width / 2, MatrixOrder.Append);
// Use a zero-width pen so it draws as thin as possible // even after scaling. using (Pen thin_pen = new Pen(Color.Black, 0)) { for (int i = 1; i <= 10000; i++) { x1 = x0 + Math.Cos(phi); y1 = y0 + Math.Sin(phi); gr.DrawLine(thin_pen, (float)x0, (float)-y0, (float)x1, (float)-y1); x0 = x1; y0 = y1;
Comments