Mandelbrot Zoomer in WPF
Mandelbrot Zoomer in WPF
Written by Mike James   
Tuesday, 20 July 2010
Article Index
Mandelbrot Zoomer in WPF
Displaying the result
Using the byte array
Final touches

All that remains is the mouseup event handler and this is comparatively easy. It simply has to cancel the selection operation, make the rectangle invisible again and compute the new view of the Mandelbrot set using the new Rectangle:

private void Canvas1_MouseLeftButtonUp(
object sender, MouseButtonEventArgs e)
mousedown = false;

To compute the new view of the Mandelbrot set we first need to convert the bitmap position as specified by selection into a Rect in terms of x,y co-ordinates. This is just the same calculation we have already performed in converting px,py to x,y in the drawSet method:

double xscale = (area.Right-area.Left)/
double yscale = (area.Top-area.Bottom)/
Point TopLeft= new Point(area.Left +
Canvas.GetLeft(selection) * xscale,
Point BottomRight = TopLeft+new Vector(
area=new Rect(TopLeft,BottomRight);

The only clever parts are the use of a Vector displacement to compute the bottom right corner rather than converting its co-ordinates from scratch.

Finally we can compute the new view:

 image1.Source = drawSet(area);

Now the user can zoom in on any area of the set. Notice that because WPF is a persistent vector graphic system we don't have to worry about drawing and erasing the rectangle that indicates the selected area - the system takes care of it. All we have to do is set its position and size.


Where next?

With the program as described above you can go exploring the Mandelbrot set to your heart’s content.

There’s a lot to see but be warned areas that are mostly blue take a long time to plot because these take the iteration loop to a full 1000 times around!

The fact that the computation takes so long makes it an ideal candidate for running on a thread other than the UI thread. You may notice that the UI freezes when the set is being computed and the only way to keep the application responsive is to use a worker thread.

When you are just trying to get it all working it is worth making the Image control small so that plots occur quickly.

You can add some additional features to make the program more usable – a plot interrupt button, for example, a pan, and so on..



There are lots of interesting areas to explore.


There is one small feature that I should warn you about before it wastes a lot of time when you try to “debug” the program. There are no limits on how far you can zoom in on any given region of the Mandelbrot set. You will discover that you can indeed use very high zooms but sooner or later you will select a very small region and the result will not look fractal but “blocky”. No you haven’t discovered that there is a limit to the structure of the Mandelbrot set!

What is happening is that you have zoomed to the point where double precision variables aren’t accurate enough to represent the position of each point. The result is that you get the same answer for whole rectangular regions for the iteration and hence the blocky structure in the final plot. The solution to the problem is fairly simple – increase the precision - but this is easier said than done and you just hit another limit sooner or later. .



No not the end of fractals – you’ve just zoomed too far.


To access the code for this project, once you have registered,  click on CodeBin.



Getting Started With Azure Linux VMs

If you are a Visual Studio user there are lots of advantages to making use of Azure as your Virtual Machine host. In this article the general ideas of using Azure and how to work with Linux VMs is des [ ... ]

Analytics As A Service

If you think that Google Analytics is the last word in finding out how your web site is being used then you need to think again. There may well be a better option.

Other Projects







all the code

Last Updated ( Tuesday, 03 December 2013 )

RSS feed of all content
I Programmer - full contents
Copyright © 2015 All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.