[C# Helper]
Index Books FAQ Contact About Rod
[Beginning Database Design Solutions, Second Edition]

[Beginning Software Engineering, Second Edition]

[Essential Algorithms, Second Edition]

[The Modern C# Challenge]

[WPF 3d, Three-Dimensional Graphics with WPF and C#]

[The C# Helper Top 100]

[Interview Puzzles Dissected]

[C# 24-Hour Trainer]

[C# 5.0 Programmer's Reference]

[MCSD Certification Toolkit (Exam 70-483): Programming in C#]

Title: Draw a Heighway dragon fractal in C#

[Draw a Heighway dragon fractal in C#]

The Heighway dragon, which is also known as the Harter-Heighway dragon, is a recursive curve where each level of recursion provides more detail. The base case where the level of recursion is 0 is a straight line. From any given level, to produce the next level you replace straight line segments with a right-angle turn. In other words, you replace each segment with two new segments that make a right angle to each other and that are rotated 45 degrees alternately to the right and left.

The following picture shows the curve drawn with the first few levels. Black lines show one level and red lines show how they are replaced to produce the next level.

[Draw a Heighway dragon fractal in C#]

The DrawDragonLine method shown in the following code draws the curve.

// Recursively draw the dragon curve between the two points. private void DrawDragonLine(Graphics gr, int level, Direction turn_towards, float x1, float y1, float dx, float dy) { if (level <= 0) { gr.DrawLine(Pens.Red, x1, y1, x1 + dx, y1 + dy); } else { float nx = (float)(dx / 2); float ny = (float)(dy / 2); float dx2 = -ny + nx; float dy2 = nx + ny; if (turn_towards == Direction.Right) { // Turn to the right. DrawDragonLine(gr, level - 1, Direction.Right, x1, y1, dx2, dy2); float x2 = x1 + dx2; float y2 = y1 + dy2; DrawDragonLine(gr, level - 1, Direction.Left, x2, y2, dy2, -dx2); } else { // Turn to the left. DrawDragonLine(gr, level - 1, Direction.Right, x1, y1, dy2, -dx2); float x2 = x1 + dy2; float y2 = y1 - dx2; DrawDragonLine(gr, level - 1, Direction.Left, x2, y2, dx2, dy2); } } }

The method takes as parameters the coordinates of a starting point (x, y) and the direction <dx, dy> in which it should draw. It also takes a level of recursion and the direction it should turn (right or left) if it should replace the basic segment from (x, y) to (x + dx, y + dy) with two new ones.

If the level of recursion is 0, the program simply draws the segment from (x, y) to (x + dx, y + dy). This represents the base case that draws the segments that make up the curve.

If the level is greater than 0, then the code calculates where it should create the two new lines connected by a right angle that it should use to replace the simple segment. it then calls itself recursively to draw the two new segments with the level of recursion reduced by 1.

Note that the code includes a precompiler symbol that lets you easily draw one less than the maximum level of recursion so you can see how one level is turned into the next. Another symbol lets you draw the curve's points so they are easy to see. Modify the code to turn those symbols on.

Download the example to experiment with it and to see additional details.

© 2009-2023 Rocky Mountain Computer Consulting, Inc. All rights reserved.