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

The beauty of fractals - A simple fractal rendering program done in C#

, 27 Jul 2009
Rate this:
Please Sign up or sign in to vote.
A fractal rendering application demonstrating many .NET programming techniques.

Introduction

I have written many fractal applications in my life. Starting on the C64, on the Amiga, on the PC, using different languages (even on a TI calculator in BASIC). Rendering fractals is a good practice when learning programming. Last week, I spent some time writing a simple fractal engine in C#. The purpose was to pack many of the programming techniques I was using during the last years into one application. In this application, you will find many interesting topics covered like:

  • General C# programming
  • MDI applications done in .NET
  • Usage of interface classes in C#
  • Use of delegate functions
  • Asynchronous calculations
  • Making use of multiple CPU cores
  • Making multi-threaded mathematic calculations
  • Bilinear filtering of bitmaps
  • Complex number class done in C#
  • Mathematic background of Mandelbrot, Julia, and Newton fractals
  • and many more ...

Background

I have been fascinated by fractals ever since I was a kid. I can remember the first Mandelbrot engines on the commodore 64. Again and again, I am fascinated by the beauty of the outcome of such simple algorithms:

A Newton fractal

frac2.png

The Mandelbrot fractal from a bird's eye view

frac1.png

The Julia fractal

frac3.png

A Mandelbrot fractal without and with 'exact' rendering

frac3.png

frac3.png

Exact rendering means that the number of iteration steps is not interpreted as int but as double, and the color used to represent the result is interpolated between the previous and the next color in the palette. Using exact rendering does not need much more CPU power, but greatly improves the quality of the outcome.

Somewhere inside the Mandelbrot fractal

frac3.png

The code

I don't want to put any code snippets here. Just open the solution and have a look into it. I think the code is very simple and clear, everything should be self-explanatory. I think it will be a good tutorial for beginners going through the code. You will find many things here that took me a long time to learn Wink | ;-)

There is one interface class IFractal from which a base fractal class will be derived. From this base fractal class, the Mandelbrot, Julia, and Newton fractals are derived.

You have an MDI container and a general MDI child form that can handle displaying dragging and zooming of fractals. You can drag a rectangle with the left mouse button pressed to zoom into a fractal. You can move the mouse with the right button pressed to scroll the fractal around. You can move the mouse with the middle parameter pressed, to change the control point used for the Julia fractal.

Fractals can be stored to and loaded from XML files. Fractal renderings can be stored as PNG files.

The engine renders at decent speed. It detects the number of available CPU cores and creates one rendering thread for each. The time needed to render a fractal nearly halves when going from a single core to a dual core machine. On my dual core Notebook, rendering a bird's eye Mandelbrot fractal with 2000x2000 pixels and 256 iterations per pixel takes approximately 650 ms, which is an acceptable speed.

This program is far from being finished, and probably will never be, due to lack of time. But, it is already capable of doing very beautiful fractal renderings. And, it shows how simple things can be done in C#. I remember doing the same in C++ some years ago, and it was a lot more work.

Next steps ...

There are some things that I want to implement in the near future, for the next release (I hope I will find the time):

  • A screensaver generating random fractals.
  • A fractal movie renderer.
  • Handle dynamic palette sizes up to an unlimited amount of colors. Right now, the palette is fixed to 256 colors, though the palette form can display an arbitrary amount of colors.

If you have any good suggestions, just post them here.

History

  • 27.07.2009: Initial version.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Zimmermann Stephan
Software Developer (Senior)
Austria Austria
I have started programming at the age of 13 on the commodore 64.
 
Ever since then I have been programming on many systems in many languages.
 
During the last 12 years I have been working as professional programmer in different companies and different areas.
 
Now I am working as freelancer programmer / consultant

Comments and Discussions

 
QuestionCan't open the code Pinmemberganesh19961-Jul-13 23:32 
GeneralMy vote of 4 PinmemberMember 327776020-Sep-12 4:14 
GeneralMy vote of 5 Pinmemberyumeng.liu12-Jul-12 18:32 
GeneralMy vote of 5 Pinmemberconmajia8-Jul-12 0:45 
GeneralMy vote of 5 Pinmemberdescartes18-Jun-12 22:59 
GeneralMy vote of 2 PinmemberGuillaume Waser21-May-12 1:08 
GeneralMy vote of 5 PinmemberSergio Andrés Gutiérrez Rojas30-Mar-12 7:49 
GeneralMy vote of 5 PinmemberAkram El Assas22-Feb-12 12:25 
GeneralMy vote of 5 PinmemberMember 337837323-Jun-11 9:38 
GeneralMy vote of 5 PinmemberFIRMANSYAH.MS6-May-11 9:51 
GeneralMy vote of 5 PinmemberSauruxum26-Jan-11 23:05 
GeneralMy vote of 5 Pinmemberdim13122-Jan-11 0:43 
Generalawesome PinmemberJFergulbops10-Oct-10 22:58 
GeneralMy vote of 3 PinmemberLeft of Zen6-Oct-10 0:21 
GeneralComments PinmemberLeft of Zen5-Oct-10 22:53 
GeneralMy vote of 5 Pinmembernamidadraco5-Oct-10 6:11 
GeneralNice PinmemberCIDev1-Oct-10 6:16 
GeneralMy vote of 5 Pinmembervelkanzi2-Aug-10 16:52 
GeneralMy vote of 5 PinmemberShahzad Raja26-Jul-10 19:05 
GeneralMy vote of 5 Pinmemberjack48822-Jul-10 10:58 
GeneralReally Nice PinmemberKenJohnson17-Jul-10 20:15 
GeneralMy vote of 5 PinmemberMohsen Kazemi1-Jul-10 10:15 
GeneralThank you very much PinmembertianweiCn15-Sep-09 15:43 
GeneralCool Pinmemberthbernt4-Sep-09 0:59 
GeneralAwesome.....gets my 5 PinmemberVivek_Minhas1-Sep-09 2:29 
General5 & bookmarked Pinmembermrrandom21-Aug-09 10:41 
GeneralBeautiful fractals! PinmemberMTWill5-Aug-09 16:15 
GeneralRe: Beautiful fractals! Pinmemberzimstep5-Aug-09 20:50 
GeneralRe: Beautiful fractals! Pinmemberthbernt6-Sep-09 20:32 
QuestionModern art? PinmemberGünther M. FOIDL28-Jul-09 4:38 
AnswerRe: Modern art? Pinmemberzimstep28-Jul-09 7:32 
GeneralWow Pinmemberprasad0227-Jul-09 19:10 
GeneralGot my 5 PinmemberMarcelo Ricardo de Oliveira27-Jul-09 3:44 
GeneralRe: Got my 5 Pinmemberzimstep27-Jul-09 6:44 
GeneralMy vote of 5 PinmemberHristo Bojilov27-Jul-09 0:32 
GeneralRe: My vote of 5 Pinmemberzimstep27-Jul-09 6:43 
GeneralRe: My vote of 5 Pinmemberddarko1003-Nov-09 8:13 

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
Web01 | 2.8.140721.1 | Last Updated 27 Jul 2009
Article Copyright 2009 by Zimmermann Stephan
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid