Draw a colored butterfly curve in C#

This program uses the following equations to draw the butterfly curve:

    x =  Sin(t) * (eCos(t) - 2 * Cos(4 * t) - Sin(t / 12)5)
    y = -Cos(t) * (eCos(t) - 2 * Cos(4 * t) - Sin(t / 12)5)

The form's Paint event handler loops variable t through the values 0 to 24 * Pi to generate the curve's points and connects them. For each line, the code calls the GetColor method to get the line's color.

private const int period = 24;
private Color[] Colors;

// Draw the butterfly.
private void Form1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.SmoothingMode =
System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
e.Graphics.Clear(this.BackColor);

// Scale and translate.
RectangleF world_rect =
new RectangleF(-4.0f, -4.4f, 8.0f, 7.3f);
float cx = (world_rect.Left + world_rect.Right) / 2;
float cy = (world_rect.Top + world_rect.Bottom) / 2;

// Center the world coordinates at origin.
e.Graphics.TranslateTransform(-cx, -cy);

// Scale to fill the form.
float scale = Math.Min(
this.ClientSize.Width / world_rect.Width,
this.ClientSize.Height / world_rect.Height);
e.Graphics.ScaleTransform(scale, scale,
System.Drawing.Drawing2D.MatrixOrder.Append);

// Move the result to center on the form.
e.Graphics.TranslateTransform(
this.ClientSize.Width / 2,
this.ClientSize.Height / 2,
System.Drawing.Drawing2D.MatrixOrder.Append);

// Generate the points.
PointF pt0, pt1;
double t = 0;
double expr =
Math.Exp(Math.Cos(t))
- 2 * Math.Cos(4 * t)
- Math.Pow(Math.Sin(t / 12), 5);
pt1 = new PointF(
(float)(Math.Sin(t) * expr),
(float)(-Math.Cos(t) * expr));
using (Pen the_pen = new Pen(Color.Blue, 0))
{
const long num_lines = 5000;
for (long i = 0; i < num_lines; i++)
{
t = i * period * Math.PI / num_lines;
expr =
Math.Exp(Math.Cos(t))
- 2 * Math.Cos(4 * t)
- Math.Pow(Math.Sin(t / 12), 5);
pt0 = pt1;
pt1 = new PointF(
(float)(Math.Sin(t) * expr),
(float)(-Math.Cos(t) * expr));
the_pen.Color = GetColor(t);
e.Graphics.DrawLine(the_pen, pt0, pt1);
}
}
}

// Return an appropriate color for this segment.
private Color GetColor(double t)
{
return Colors[(int)(t / Math.PI)];
}

   

 

What did you think of this article?




Trackbacks
  • No trackbacks exist for this post.
Comments

  • 6/25/2010 12:34 PM Richard Moss wrote:
    Pretty but your math based examples always fry my mind :(
    Reply to this
  • 6/25/2010 1:51 PM Rod Stephens wrote:
    Sorry about that ;-)

    Even if you don't follow the equations (I don't really understand them too well, either), there are some things to learn from this example. For example, how to plot a parametric function X(t) and Y(t).

    The next two examples will also highlight techniques used by this example.
    Reply to this
  • 8/1/2010 1:45 AM Ebrima wrote:
    Fantastic, I wish I have a maths background like you, it is extra ordinary
    Reply to this
  • 3/27/2012 8:45 AM Yelinna wrote:
    There's a tiny tiny (not so tiny) mistake in the original formulas:
    x = Cos(t) * Exp(Cos(t)) - 2 * Cos(4 * t) - Sin(t / 12) ^ 5
    y = Sin(t) * Exp(Cos(t)) - 2 * Cos(4 * t) - Sin(t / 12) ^ 5

    They really are:
    x = Cos(t) * (Exp(Cos(t)) - 2 * Cos(4 * t) - Sin(t / 12) ^ 5)
    y = Sin(t) * (Exp(Cos(t)) - 2 * Cos(4 * t) - Sin(t / 12) ^ 5)

    Then I got my butterfly :D
    Reply to this
    1. 4/1/2012 5:46 PM Rod Stephens wrote:
      Sorry about that. You're right. I didn't transcribe from the source code correctly.
      Reply to this
Leave a comment

Submitted comments are subject to moderation before being displayed.

 Name

 Email (will not be published)

 Website

Your comment is 0 characters limited to 3000 characters.