# Inspired By Flash Math Creativity: WPF Flowers

I've recently picked up a great book called Flash Math Creativity.  I been inspired by some of the techniques it outlines and have played around with similar ideas in WPF.  The exercise has been a fruitful one, in that I'm making some pleasing computer art while also learning about the differences between Action Script animation and WPF animation.  The first one I played with is flowers, inspired by the work of Glen Rhodes, which is actually featured on the cover of the book.  You can see the results here and download the code here.

The first flower, which has no animation, is pretty simple. I just create 125 rectangles with a fill using a DrawingBrush I created in Blend.  I place each of the petals on the "stage", which in the case is a Grid.  I use Grid instead of Canvas so that I get the goodness of the WPF layout engine for free without having to handle any positioning of the rectangles.  I then tranform the rotation and scale of each one to create the flower effect.  I also swap the ZIndex so that the large petals are in back and the smaller ones are in front.

{
int r = 0;
double s = 0;
for (int i=0; i < 125; i++)
{
Rectangle rect = new Rectangle();
rect.Width = 50;
rect.Height = 50;
rect.Fill = (DrawingBrush)this.FindResource("petal");
TransformGroup tg = new TransformGroup();
s += .01;
ScaleTransform st = new ScaleTransform(s * s * s + .1, s * s * s + .1);
r += 27;
RotateTransform rt = new RotateTransform(r);
rect.RenderTransform = tg;
Grid.SetZIndex(rect, -i);
}
}

Where things get a little more interesting is when it comes to animating the flower. In Action Script, this kind of dynamic animation is handled through the very convenient onEnterFrame.  This handler provides a per-frame callback for each individual movie clip, in which a function can be wired up.  There is no equivalent in WPF such that you get a per-frame callback for each element.  The closest thing to this concept is the CompositionTarget.Rendering handler, which gets called per-frame for the entire tree (or page or stage, if you will.)

It is possible to find each element you are looking for when this event gets fired and achieve a similar effect to onEnterFrame.  I did just this in Page3.xaml, which ends up adding the following in addition to the loaded method:

void CompositionTarget_Rendering(object sender, EventArgs e)
{
foreach (UIElement uie in stage.Children)
{
TransformGroup tg = uie.RenderTransform as TransformGroup;
ScaleTransform st = tg.Children[0] as ScaleTransform;
if (st.ScaleX > -3)
st.ScaleX -= .1;
}
}

You can see how I end up having to walk the tree, extracting the children and then acting on the children.  While this works, there are several disadvantages to this methodology.  First, having to figure out the animation for every UI element within a single callback has the potential to get quite unwieldy.  Second, using this method means we don't get all of the goodness of the WPF animation system, things like frame rate indepence, timelines, storyboards, repeat behaviors, autoreverse, etc.  So, let's see what this animation would look like using the WPF animation system:

{
int r = 0;
double s = 0;
DoubleAnimation d = new DoubleAnimation(-3, new Duration(TimeSpan.FromSeconds(2)));
d.AutoReverse = true;
d.RepeatBehavior = RepeatBehavior.Forever;
for (int i = 0; i < 125; i++)
{
Rectangle rect = new Rectangle();
rect.Width = 50;
rect.Height = 50;
rect.Opacity = .5;
rect.Fill = (DrawingBrush)this.FindResource("petal2");
TransformGroup tg = new TransformGroup();
s += .01;
ScaleTransform st = new ScaleTransform(s * s * s + .1, s * s * s + .1);
r += 27;
RotateTransform rt = new RotateTransform(r);