Click here to Skip to main content
Click here to Skip to main content

Fractal Snow

, 13 Dec 2003
Rate this:
Please Sign up or sign in to vote.
Describes how to draw snowflakes using fractals and contains a nice snow screensaver.

Snow generatedd using fractals

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)

First type of snow flakes

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)

Second type of snow flakes

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

UML class diagram

Uml class diagram

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();
  Controls.Add(snowCtrl);
  snowCtrl.Dock=DockStyle.Fill;
}

private void Form_Load(object sender,EventArgs e)
{
  // Start animation
  snowCtrl.Start();
}

Screen saver

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.

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)

About the Author

Tomas Petricek

Czech Republic Czech Republic
I live in Prague, the capital city of Czech republic (most of the time Smile | :) ). 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 Sheep | [baah] .

Comments and Discussions

 
GeneralMy vote of 5 Pinmembermanoj kumar choubey26-Feb-12 21:41 
GeneralMy vote of 5 Pinmembernamidadraco5-Oct-10 6:24 
GeneralCode HELP! =) PinmemberStephLeda21-Oct-09 1:25 
Generalmultithreading Pinmemberzsevgymko25-Feb-05 2:29 
GeneralScreen saver preview (VB.NET) PinmemberJohn Wells5-Jun-04 18:34 
Questionwindow in top left corner? Pinmemberjeffreylreed17-Apr-04 14:54 
AnswerRe: window in top left corner? PinmemberTomas Petricek18-Apr-04 6:17 
GeneralNice one PinmemberRajesh Pillai26-Dec-03 0:31 
GeneralGreat job. PinmemberTodd Smith18-Dec-03 8:41 
GeneralMissing XML PinmemberBill Seddon14-Dec-03 22:37 
GeneralRe: Missing XML PinmemberTomas Petricek15-Dec-03 6:07 
GeneralNow we just have to combine the two PineditorMarc Clifton14-Dec-03 14:02 
GeneralRe: Now we just have to combine the two PinmemberTomas Petricek15-Dec-03 6:17 
GeneralExcellent! Pinmemberjdunlap14-Dec-03 10:40 
GeneralRe: Excellent! PinmemberTomas Petricek15-Dec-03 6:08 
QuestionMultiple Monitor Support? PinmemberDerek Price14-Dec-03 6:54 
AnswerRe: Multiple Monitor Support? PinmemberTomas Petricek14-Dec-03 7:12 
GeneralRe: Multiple Monitor Support? PinmemberDerek Price14-Dec-03 8:00 
GeneralRe: Multiple Monitor Support? PinmemberTomas Petricek14-Dec-03 8:25 
GeneralRe: Multiple Monitor Support? PinmemberDerek Price14-Dec-03 8:40 
GeneralRe: Multiple Monitor Support? PinmemberTomas Petricek14-Dec-03 9:14 
GeneralRe: Multiple Monitor Support? PinmemberDerek Price14-Dec-03 9:25 
GeneralRe: Multiple Monitor Support? PinmemberDerek Price15-Dec-03 10:41 
GeneralRe: Multiple Monitor Support? PinmemberHugo Hallman21-May-04 9:58 
GeneralRe: Multiple Monitor Support? PinmemberDerek Price21-May-04 10:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.140721.1 | Last Updated 14 Dec 2003
Article Copyright 2003 by Tomas Petricek
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid