Silverlight Kata: IFS Fractals: Full Solution

by Bruce Abernethy 5. February 2010 09:23

If you have been following along with Part 1, Part 2, and Part 3, then you know we have described basically what the IFS Fractal “quest” is, how we did the rotatation/translation/scale transforms, and how we wired up the MessageBus to send our messages.

Complete Code Solution

So we are ready to put it all together: [ Download Source Code (if desired) IFSContentControl.zip (1.4Mb) ]

And, since you’ve been so patient, here is a live demo of the application.

Live Demo (Requires Silverlight 3)

 

Get Microsoft Silverlight

Pulling it Together

So, once we had the idea, the IFSContentControl, and the MessageBus wired up, pulling the rest of the Solution together is pretty straightforward.  You will see the typical Silverlight Solution structure of a Silverlight project and the hosting Web Application project.

In “polishing” this up a little I did add a few more Messages.

public class SetStartupIFSControl : CompositePresentationEvent<IFSControl> { }
public class SelfDestruct : CompositePresentationEvent<DateTime> { }

Adding SetStartupIFSControl let me add the ability to have the three buttons at the bottom with different “starter” IFS Objects.

The “Serpinski Triangle / Gasket” that I’ve been showing since Part 1 was defined like this …

public static IFSControl StarterSerpinski()
{
  var starter = new IFSControl
                    {
                        IsProbabalistic = false,
                        Background = new SolidColorBrush(Colors.LightGray),
                        XOffset = 25,
                        Age = 1
                    };

  starter.Content = starter.MyRootControl;

  starter.IFSTransforms.Transforms.Add(new IFSTransform(.5, .5, 0, 0, 0, 1));
  starter.IFSTransforms.Transforms.Add(new IFSTransform(.5, .5, 0, 200, 0, 1));
  starter.IFSTransforms.Transforms.Add(new IFSTransform(.5, .5, 0, 100, 200, 1));

  return starter;
}

The more complex “Barnsley Fern” looks like this …

public static IFSControl BarnsleyFern()
{

  var starter = new IFSControl
                    {
                        IsProbabalistic = false,
                        Background = new SolidColorBrush(Colors.Black),
                        XOffset = 100,
                        Age = 1
                    };

  starter.RectangleTemplate = new Rectangle
                                  {
                                      Height = 400,
                                      Width = 200,
                                      Stroke = new SolidColorBrush(Colors.Green),
                                      Fill = new SolidColorBrush(Color.FromArgb(200, 128, 255, 128)),
                                      StrokeThickness = 8
                                  };

  starter.Content = starter.MyRootControl;

  starter.IFSTransforms.Transforms.Add(new IFSTransform(.01, .16, 0, 0, 0, 1));
  starter.IFSTransforms.Transforms.Add(new IFSTransform(.34, .3, -50, 2, 50, 7));
  starter.IFSTransforms.Transforms.Add(new IFSTransform(.36, .28, 40, -2, 50, 7));
  starter.IFSTransforms.Transforms.Add(new IFSTransform(.85, .85, -3, -0, 50, 75));



  return starter;
}

These are both in the StarterIFSObjects static class.

The RectangleTemplate allowed for different shape Rectangles (which the “fern” required”) and also gave me a place to put the foreground and background colors – which is nice.  I added “Age” so I could tell how many iterations each object had gone through (surfaces as a ToolTip).

How well does it work?

For the Serpinski Triangle / Gasket, I was very pleased:

triangle8it

After 8 iterations, we have a fine (nearly textbook) example of what we were looking for.

For the “Checkered ‘X’”, also very nice:

cross5it

More predictable, in my opinion, than the triangle, but after only 5 iterations we have the multi-layered self-similar “X”

But for the Barnsley Fern:

fern7it

Not what I was hoping for.  Even after 7 iterations (creating 16384 IFSContentControls), this is only starting to look like what I was hoping for.  Iterating one more time left the browser “Not Responding” and was not generally a good idea.

The fern has many intricate details that do not surface well with this algorithm.  There are “bitmap” algorithms that create very nice ferns in a short amount of time (“classic” GDI+ routines) …

bitmapfern

But these do not track each translation and each message going out, which was what I was hoping to learn about from this.

So, the goal last weekend (which turned in to a goal to blog about it before “next” weekend (i.e. tomorrow)) was to learn more about TransformGroups and to see how much I could stress the EventAggregator/MessageBus without it caving.  More about that in the last installment.

  • Part 1 – Fractals with TransformGroup and MessageBus
  • Part 2 – Automating transformations by creating an IFS Content Control
  • Part 3 – Wiring up the Iterations using a basic MessageBus implementation.
  • Part 4 – But will it work for a fractal Fern?
  • Part 5 – Lessons Learned (aka limitations of the MessageBus and recursion in Silverlight)

  • Tags:

    Comments


    February 3. 2010 10:00
    tiffany and co
    i like!


    February 5. 2010 16:38
    Powwownow
    Wow! This is insanely awesome! Never imagined that Silverlight can do something like that.


    February 8. 2010 16:18
    trackback
    Social comments and analytics for this post

    This post was mentioned on Twitter by babernethy: Blogged: "Silverlight Kata: IFS Fractals: Full Solution" Part 4 of 5 w/ working sample - http://bit.ly/bDvzMn


    February 9. 2010 06:18
    Farbkorrektur masken
    Its really nice blog....


    February 13. 2010 04:18
    JORDANIEN REISEN
    very nice post.... thanks for the information...


    February 15. 2010 19:22
    Cesar Schwalb
    Superb post, this is very similar to a site that I have. Please check it out sometime and feel free to leave me a comenet on it and tell me what you think. Im always looking for feedback.


    February 16. 2010 01:17
    Seymour Halburnt
    I don't agree with everything in this posting, but you do make some very good points. Im very interested in this topic and I myself do alot of research as well. Either way it was a well thoughtout and nice read so I figured I would leave you a comment. Feel free to check out my website sometime and let me know what you think.


    February 17. 2010 13:02
    Luis Riser
    I have read a few of the articles on your website now, and I really like your style of blogging. I added it to my favorites website list and will be checking back soon. Please check out my site as well and let me know what you think.


    February 18. 2010 06:56
    Jarred Woodert
    This is a excellent post, but I was wondering how do I suscribe to the RSS feed?


    February 19. 2010 03:22
    Coralee Orozco
    This is a very fascinating post, I was looking for this info. Just so you know I located your webpage when I was doing research for blogs like mine, so please check out my site sometime and leave me a comment to let me know what you think.


    February 19. 2010 12:46
    certified bmw cars long island
    I read your post . it was amazing.Your thought process is wonderful.
    The way you tell about things is awesome.

    Comments are closed

    RecentComments

    Comment RSS
    Disclaimer
    The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

    © Copyright 2010 Bruce's Blog