13,052,870 members (61,238 online)
alternative version

#### Stats

175K views
84 bookmarked
Posted 13 Dec 2003

# Fractal Snow

, 13 Dec 2003
 Rate this:
Describes how to draw snowflakes using fractals and contains a nice snow screensaver.

## Introduction

This article describes some basic characteristics of fractals and shows how to draw snowflakes using fractals. Source code contains object for drawing one snowflake and Windows forms control for falling snow. This control is used for creating screensaver applications. In the screensaver applications, snowflakes are created using random settings, so there are almost unlimited variations of snowflakes.

### Fractals

Fractals are geometrical shapes, where a certain pattern repeats itself depending on zoom. That means, when you zoom in a part of the object, it looks the same way or is very similar like the original object before the zoom.

In programs, fractals are created using recursion. Same (or very similar) drawing operation is repeated until depth of recursion exceeds specified number. In my application, the drawing operation is performed on the line connecting the center of the snowflake with its edge and then on new lines created in first step.

## Snowflakes

Application uses two slightly different algorithms to make more various snowflakes. These two methods have same input parameters with same meaning, so it is very simple to use both methods for drawing snowflakes.

If you want to learn more about how snowflakes are generated and what each input parameter does, install screensaver and go to Settings - Snowflake Preview.

### First type (LineType)

#### Drawing snowflake

1. Six fractal branches are drawn from the center of the snowflake to the edge.
2. Line is divided to two parts with lengths in specified ratio.
3. Two new lines are drawn under specified angle from found point.
4. Operation (from number 2 to 4) is repeated on two newly created lines.

### Second type (PointType)

#### Drawing snowflake

1. Six fractal branches are created (no drawing at this time!) from the center of the snowflake to the edge.
2. Line is divided to two parts with lengths in specified ratio.
3. Two new lines are created (no drawing) under specified angle from found point.
4. Operation (from number 2 to 4) is repeated on two newly created lines and on third line from found point to the point at the edge.
5. After count of repetitions is exceeded, all residuary lines are drawn.

## Source code

### Snow flake drawing

Following function is the simplified recursive function used for drawing the first type of snowflakes:

This code uses function `Rotate`. This function rotates point around specified center and is implemented in SnowFlake.cs.

```// Ratio used for calculating dividing point
private const float fBranching=0.3f;
// Ratio used for decreasing length of lines
private const float fShrinking=0.6f;
// Maximal depth of recursion
private const int   iMaxDepth=5;
// Angle between parent and child line (in degrees)
private const int   iBranchAngle=35;

// Draw snowflake branch
// output - Graphics to draw on
// start  - Starting point of line
// end    - Ending point of line
// depth  - Depth of recursion
private void DrawBranch(Graphics output,
PointF start,PointF end,int depth)
{
if (depth==iMaxDepth) return;
DrawLine(output,pn,start,end);

// calculate dividing point
PointF cnt=new PointF(start.X+(end.X-start.X)*fBranching,
start.Y+(end.Y-start.Y)*fBranching);
// calculate point used as end (after rotation) of two new lines
PointF nend=new PointF(cnt.X+(end.X-start.X)*fShrinking,
cnt.Y+(end.Y-start.Y)*fShrinking);

// recursion - draw two new lines
DrawBranch(output,cnt,Rotate(nend,cnt,iBranchAngle),depth+1);
DrawBranch(output,cnt,Rotate(nend,cnt,-iBranchAngle),depth+1);
}```

### Using this code

#### Using SnowFlake

`SnowFlake` object can be used to draw snowflakes. It has two methods for drawing. First method `Draw(Graphics, PointF)` can be used for normal drawing and second method `Draw(Graphics, PointF, int)` allows you to specify alpha value for drawn pixels. Second method is used in screensavers for snowflake melting. `RandomSnowFlake` is class derived from `SnowFlake`. Only one difference is that, all properties of snowflake are generated from random numbers in constructor, so you don't have to set all properties if you want to create snowflake.

```// Draw snowflakes in Paint event handler
private void Form_Paint(object sender, PaintEventArgs pe)
{
Random rndGen=new Random();
pe.Graphics.Clear(Color.Black);
SnowFlake flake=new SnowFlake(),
random=new RandomSnowFlake(rndGen.Next(Int32.MaxValue));

// draw snowflake
flake.Draw(pe.Graphics,new PointF(70.0f,70.0f));

// draw melting snowflake
flake.Draw(pe.Graphics,new PointF(70.0f,140.0f,128));

// draw random snowflake
random.Draw(pe.Graphics,new PointF(105.0f,105.0f));
}```

#### Using SnowControl

`SnowControl` is the control for creating falling snowflakes. This is exactly the same control as control filling the whole screen of screensaver. This control can be added to a `Form` using designer, or manually using code similar to following:

This control has a lot of properties, that are described in Screen saver settings section.

```SnowControl snowCtrl;

public Form()
{
// Create control
snowCtrl=new SnowControl();
snowCtrl.Dock=DockStyle.Fill;
}

{
// Start animation
snowCtrl.Start();
}```

## SnowControl and Screensaver settings

#### Environment

Property called `StopFalling` is length in pixels from bottom of the screen, where falling snowflakes will stop falling. If this property is less than zero, snowflakes will fall outside the screen. Falling snowflakes are affected by wind. The `WindForce` specifies how the snowflakes will be inflected by wind and `WindChanging` determinates how fast will the direction of wind change.

#### Snowflakes

You can change number of snowflakes displayed on screen (`MaxFlakes`) and color of snowflakes (`FlakeColor`). Size and speed properties are randomly generated between specified maximal and minimal values (`MinSpeed`, `MaxSpeed` and `MinSize`, `MaxSize`).

When snowflake falls to the bottom of the screen, it stays at this location for a specified number of frames (`MeltingStart`), after this time the snowflake starts melting and it melts for a number of frames specified by `MeltingFluency` property.

#### Background

Background of screensaver can be any image compatible with .NET, solid color or gradient from two colors (top and bottom color). Note that if you set background to image and you have more snowflakes, then the screensaver can be very slow!

## Thanks to...

Thanks to Xenik (Adam Abonyi) for this idea, useful remarks and big help during the composition of this English article. Additional thanks to the authors of the following articles.

### Related articles

Marc's fractal tree was a great inspiration for this article and other two contains useful information about creating screensavers, including multiple monitors support and how to draw screensaver preview in .NET.

1. Create a fractal Christmas tree by Marc Clifton.
2. Christian and James' Code Project Screensaver by Christian Graus and James T. Johnson.
3. How to develop a screen saver in C# by Rakesh Rajan.

## Share

 Czech Republic
I live in Prague, the capital city of Czech republic (most of the time ). I've been very interested in functional programming recently and I have a passion for the new Microsoft F# language. I'm writing a book about Functional Programming in the Real World that shows the ideas using examples in C# 3.0 and F#.

I've been Microsoft MVP (for C#) since 2004 and I'm one of the most active members of the F# community. I'm a computer science student at Charles University of Prague. My hobbies include photography, fractals and of course many things related to computers (except fixing them). My favorite book writers are Terry Pratchett and Philip K Dick and I like paintings by M. C. Escher.

PS: My favorite codeproject icon is .

## You may also be interested in...

 First PrevNext
 My vote of 5 manoj kumar choubey26-Feb-12 21:41 manoj kumar choubey 26-Feb-12 21:41
 Code HELP! =) StephLeda21-Oct-09 1:25 StephLeda 21-Oct-09 1:25
 multithreading zsevgymko25-Feb-05 2:29 zsevgymko 25-Feb-05 2:29
 Screen saver preview (VB.NET) John Wells5-Jun-04 18:34 John Wells 5-Jun-04 18:34
 window in top left corner? jeffreylreed17-Apr-04 14:54 jeffreylreed 17-Apr-04 14:54
 Re: window in top left corner? Tomas Petricek18-Apr-04 6:17 Tomas Petricek 18-Apr-04 6:17
 Nice one Rajesh Pillai26-Dec-03 0:31 Rajesh Pillai 26-Dec-03 0:31
 Great job. Todd Smith18-Dec-03 8:41 Todd Smith 18-Dec-03 8:41
 Missing XML Bill Seddon14-Dec-03 22:37 Bill Seddon 14-Dec-03 22:37
 Re: Missing XML Tomas Petricek15-Dec-03 6:07 Tomas Petricek 15-Dec-03 6:07
 Now we just have to combine the two Marc Clifton14-Dec-03 14:02 Marc Clifton 14-Dec-03 14:02
 Re: Now we just have to combine the two Tomas Petricek15-Dec-03 6:17 Tomas Petricek 15-Dec-03 6:17
 Excellent! jdunlap14-Dec-03 10:40 jdunlap 14-Dec-03 10:40
 Re: Excellent! Tomas Petricek15-Dec-03 6:08 Tomas Petricek 15-Dec-03 6:08
 Multiple Monitor Support? Derek Price14-Dec-03 6:54 Derek Price 14-Dec-03 6:54
 Re: Multiple Monitor Support? Tomas Petricek14-Dec-03 7:12 Tomas Petricek 14-Dec-03 7:12
 Re: Multiple Monitor Support? Derek Price14-Dec-03 8:00 Derek Price 14-Dec-03 8:00
 Re: Multiple Monitor Support? Tomas Petricek14-Dec-03 8:25 Tomas Petricek 14-Dec-03 8:25
 Re: Multiple Monitor Support? Derek Price14-Dec-03 8:40 Derek Price 14-Dec-03 8:40
 Re: Multiple Monitor Support? Tomas Petricek14-Dec-03 9:14 Tomas Petricek 14-Dec-03 9:14
 Re: Multiple Monitor Support? Derek Price14-Dec-03 9:25 Derek Price 14-Dec-03 9:25
 Re: Multiple Monitor Support? Derek Price15-Dec-03 10:41 Derek Price 15-Dec-03 10:41
 Re: Multiple Monitor Support? Hugo Hallman21-May-04 9:58 Hugo Hallman 21-May-04 9:58
 Re: Multiple Monitor Support? Derek Price21-May-04 10:14 Derek Price 21-May-04 10:14
 Last Visit: 31-Dec-99 18:00     Last Update: 26-Jul-17 18:46 Refresh 12 Next »