<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <title>sethmabbott.com</title>
    <description>What's Seth up to?</description>
    <link>http://www.sethmabbott.com/posts.rss</link>
    <item>
      <title>A Nod from CityPages</title>
      <description>&lt;a href=&quot;http://sethmabbott.com/images/66&quot;&gt;&lt;img src=&quot;/images/dpu_flyer.jpg&quot;/&gt;&lt;/a&gt;&lt;br/&gt;&lt;br/&gt;
A poster I made for Twin Cities band &lt;a href=&quot;http://www.dragonspowerup.com/&quot;&gt;Dragons Power UP!&lt;/a&gt; was recently featured as Flyer of the Week in &lt;a href=&quot;http://blogs.citypages.com/gimmenoise/2011/05/wizards_are_real.php&quot;&gt;CityPages&lt;/a&gt;.  Thanks guys.</description>
      <pubDate>Sun, 15 May 2011 22:41:11 -0400</pubDate>
      <link>http://www.sethmabbott.com/posts/14</link>
      <guid>http://www.sethmabbott.com/posts/14</guid>
    </item>
    <item>
      <title>Dragons Power Up! CD Release Party Posters</title>
      <description>&lt;a href='http://sethmabbott.com/tags/15/images/64'&gt;&lt;img src='http://sethmabbott.com/images/kitty.jpg'&gt;&lt;/a&gt;&lt;br/&gt;&lt;br/&gt;
I had a frantic but productive time in the print shop tonight. I finished up some &lt;a href=&quot;http://sethmabbott.com/tags/15/images/64&quot;&gt;posters&lt;/a&gt; for DPU! using my shiny new aluminum screen.</description>
      <pubDate>Tue, 16 Mar 2010 03:37:14 -0400</pubDate>
      <link>http://www.sethmabbott.com/posts/13</link>
      <guid>http://www.sethmabbott.com/posts/13</guid>
    </item>
    <item>
      <title>James Freeman Gets a Makeover</title>
      <description>&lt;img src=&quot;/images/jmf_logo.jpg&quot;/&gt;&lt;br/&gt;&lt;br/&gt;
&lt;p&gt;&lt;a href=&quot;http://lawyerjimfreeman.com&quot;&gt;The Law Offices of James Freeman, P.C.&lt;/a&gt;, is a firm advocating for the rights of cyclists and pedestrians in Chicago. James Freeman's clients are the bike commuter who is hit by a cab and the pedestrian who is injured while crossing the street. In his words:&lt;/p&gt;
					
					&lt;blockquote&gt;&amp;ldquo;The Illinois Vehicle Code was not designed with cyclists or pedestrians as its focus. Accordingly, bicyclists and pedestrians are often overlooked when it comes to their rights to the road. Most road infrastructure in Illinois was designed with automobile traffic in mind, and the needs of cyclists and pedestrians are sometimes neglected.&amp;rdquo;&lt;/blockquote&gt;
						
&lt;p&gt;His firm's &lt;a href=&quot;http://lawyerjimfreeman.com/blog&quot;&gt;blog&lt;/a&gt; is a popular and valuable source of legal information for Chicago's commuters.&lt;/p&gt;
						
&lt;p&gt;I recently had the pleasure of providing a visual makeover for the site that hosts his blog and represents his firm on the web. The goals were to clean up the typography, match the colors to existing business materials and give it a clean professional look. Here are the before and after screenshots:&lt;/p&gt;
&lt;em&gt;before&lt;/em&gt;&lt;br/&gt;	
&lt;img src=&quot;/images/jmf_before.jpg&quot;/&gt;&lt;br/&gt;
&lt;br/&gt;&lt;br/&gt;
&lt;em&gt;after&lt;/em&gt;&lt;br/&gt;
&lt;img src=&quot;/images/jmf_after.jpg&quot;/&gt;&lt;br/&gt;&lt;br/&gt;
&lt;p&gt;&lt;a href=&quot;lawyerjimfreeman.com&quot;&gt;visit lawyerjimfreeman.com&lt;/a&gt;&lt;/p&gt;</description>
      <pubDate>Fri, 04 Dec 2009 12:58:30 -0500</pubDate>
      <link>http://www.sethmabbott.com/posts/12</link>
      <guid>http://www.sethmabbott.com/posts/12</guid>
    </item>
    <item>
      <title>Dynamic Drawing and Animation</title>
      <description>&lt;img src=&quot;/images/articles/1/header.jpg&quot; alt=&quot;Dynamic Drawing and Animation Header Image&quot;/&gt;
					&lt;br/&gt;
					&lt;br/&gt;

					&lt;p&gt;Lately I've been playing around with some code-generated drawing and animation. The following is a tutorial describing how I created a tree that sways in the wind. The user generates a small but powerful source of wind by clicking and dragging around the stage. Here is what the final movie looks like:&lt;/p&gt;
					
					&lt;span id=&quot;example&quot;&gt;&lt;em&gt;You will need to have javascript turned on and have flash player 9 or higher installed to view this animation.&lt;/em&gt;&lt;/span&gt;
					
					&lt;h2&gt;Drawing the Tree&lt;/h2&gt;

					&lt;p&gt;The tree movie consists of two classes &amp;mdash; the main movie or &lt;code&gt;Tree&lt;/code&gt; class which generates the tree and handles events, and the Branch class which represents each branch of the tree. Tree.as looks like this:&lt;/p&gt;

					&lt;div class=&quot;code&quot;&gt;
						&lt;pre&gt;// Tree.as
package {

  import flash.display.*;
  import flash.geom.Point;

  public class Tree extends MovieClip{
    private var trunk:Branch;

    public function Tree(){
      trunk = new Branch(-90, new Point(stage.stageWidth/2, stage.stageHeight));
      addChild(trunk);
    }
  }
}&lt;/pre&gt;
						&lt;/div&gt;
					&lt;p&gt;At this stage the &lt;code&gt;Tree&lt;/code&gt; class is pretty basic. It creates a new branch called &lt;code&gt;trunk&lt;/code&gt; at an angle of -90 degrees (straight up), and centers it horizontally at the bottom of the movie. &lt;/p&gt;

					&lt;p&gt;Branch.as looks like this:&lt;/p&gt;

					&lt;div class=&quot;code&quot;&gt;
						&lt;pre&gt;// Branch.as
package {

  import flash.display.*;
  import flash.geom.Point;

  public class Branch extends Shape{

    // class vars
    private static const baseLength:Number = 100;

    // instance vars
    private var angle:Number;
    private var endPoint:Point;

    public function Branch(initialAngle:Number, origin:Point){
      angle = initialAngle;
      x = origin.x;
      y = origin.y;
      endPoint = getEndPoint();
      drawBranch();
    }

    private function drawBranch():void{
      graphics.clear();
      graphics.moveTo(0, 0);
      graphics.lineStyle(10, 0x000000);
      graphics.lineTo(endPoint.x, endPoint.y);
    }

    // Using angle and baseLength as polar coordinates get the endPoint of the line as cartesian coordinates.
    private function getEndPoint():Point{
      return Point.polar(baseLength, degToRad(angle));
    }

     private static function degToRad(deg:Number):Number{
       return deg/57.3;
     }
    
     private static function radToDeg(rad:Number):Number{
       return rad*57.3;
     }

  }

}&lt;/pre&gt;
							&lt;/div&gt;
					&lt;p&gt;Branch() takes an initial angle and a point. The point is the branch's origin &amp;mdash; where it connects to the rest of the tree or the ground in the case of the &lt;code&gt;trunk&lt;/code&gt;. The function &lt;code&gt;drawBranch()&lt;/code&gt; uses the Shape class's graphics property to draw out the graphical representation of the branch. This is simply a line drawn from the branches origin to an end point. The &lt;code&gt;endPoint&lt;/code&gt; is found using the &lt;code&gt;Point&lt;/code&gt; class's &lt;code&gt;polar()&lt;/code&gt; method to convert the polar coordinates defined by &lt;code&gt;angle&lt;/code&gt; and &lt;code&gt;baseLength&lt;/code&gt;, to cartesian coordinates. This is done in &lt;code&gt;getEndPoint()&lt;/code&gt;. &lt;code&gt;degToRad()&lt;/code&gt; is a utility function that converts a given number of degrees into radians. The sister funciton to &lt;code&gt;degToRad()&lt;/code&gt; is &lt;code&gt;radToDeg()&lt;/code&gt; which unsurprisingly converts radians to degrees. These are particularly useful functions since some actionScript classes use radians and others use degrees. &lt;/p&gt;

					&lt;p&gt;Compiling and running &lt;code&gt;Tree&lt;/code&gt; will draw a line on the screen. &lt;/p&gt;

					&lt;img src=&quot;/images/articles/1/tree_1.jpg&quot; alt=&quot;Tree 1&quot; style=&quot;margin-bottom:11px&quot;/&gt;

					&lt;p&gt;Not too interesting yet, but this is about to change. The clever thing about what we are about to do is that we won't have to do much to Tree.as to draw the rest of the tree. The &lt;code&gt;Branch&lt;/code&gt; class will take care of that with a little bit of recursion. Once each branch is done initializing it will create two more slightly smaller branches at different angles. Updates are in bold.&lt;/p&gt;

					&lt;div class=&quot;code&quot;&gt;
&lt;pre&gt;// Branch.as
package {
  
  import flash.display.*;
  import flash.geom.Point;
  
  public class Branch extends Shape{
  
   // class vars
   private static const baseLength:Number = 100;
   &lt;strong&gt;private static const minScale:Number = 0.2;
   private static const slope:Number = 0.8;&lt;/strong&gt;
  
   // instance vars
   private var angle:Number;
   private var endPoint:Point;
   &lt;strong&gt;private var globalEndPoint:Point;
   private var thisParent:MovieClip
   private var children:Array = [];&lt;/strong&gt;
  
   public function Branch(initialAngle:Number, &lt;strong&gt;scale:Number,&lt;/strong&gt; origin:Point, &lt;strong&gt;parentClip:MovieClip&lt;/strong&gt;){
     angle = initialAngle;
     x = origin.x;
     y = origin.y;
     &lt;strong&gt;thisParent = parentClip;
     scaleX = scaleY = scale;&lt;/strong&gt;
     endPoint = getEndPoint();
     &lt;strong&gt;globalEndPoint = localToGlobal(endPoint);&lt;/strong&gt;
     drawBranch();
     &lt;strong&gt;thisParent.addChild(this);
     if(scale*slope &gt; minScale){
       sproutBranches(scale*slope);
     }&lt;/strong&gt;
   }
  
   &lt;strong&gt;private function sproutBranches(scale:Number):void{
     // new branch angles are augmented by a random +- 10 degrees
     var branchLeft:Branch = new Branch(angle - 30, scale, globalEndPoint, thisParent);
     var branchRight:Branch = new Branch(angle + 30, scale, globalEndPoint, thisParent);
     children.push(branchLeft);
     children.push(branchRight);
   }&lt;/strong&gt;
  
   private function drawBranch():void{
     graphics.clear();
     graphics.moveTo(0, 0);
     graphics.lineStyle(10, 0x000000);
     graphics.lineTo(endPoint.x, endPoint.y);
   }
  
   &lt;strong&gt;// Using angle and baseLength as polar coordinates get the endPoint of the line as cartesian coordinates. 
   private function getEndPoint():Point{
     return Point.polar(baseLength, degToRad(angle));
   }&lt;/strong&gt;
  
   // convert degerees to radians
   private static function degToRad(deg:Number):Number{
     return deg/57.3;
   }
  
   // convert radians to degrees
   private static function radToDeg(rad:Number):Number{
     return rad*57.3;
   }
  
  }

}&lt;/pre&gt;
					&lt;/div&gt;
					
					&lt;p&gt;One very important thing to take note of is the last few lines of the Branch constructor. Here we call &lt;code&gt;sproutBranches()&lt;/code&gt;, a function that attaches two new branches to the current branch. Here we check the current branch's scale against the &lt;code&gt;minScale&lt;/code&gt; constant &amp;mdash; the minimum allowed size for a branch. If a branch's scale has reached the lower limit for branch size, it will not call &lt;code&gt;sproutBranches()&lt;/code&gt;. It will not create any new child branches. If we did not do this check flash runtime would get caught in an infinite loop and would soon crash. &lt;/p&gt;

					&lt;p&gt;The &lt;code&gt;sproutBranches()&lt;/code&gt; function creates two new branches at + and - 30 degrees from the angle of the current branch. They are then added to the children array for later reference.&lt;/p&gt;

					&lt;p&gt;We've also added &lt;code&gt;globalEndPoint&lt;/code&gt; which will be used to keep track of each branch's &lt;code&gt;endPoint&lt;/code&gt; relative to the root coordinate plane. This is passed on to child branches to tell them where to set their origins. &lt;/p&gt;

					&lt;p&gt;We will need to change the parameters passed to &lt;code&gt;trunk&lt;/code&gt; in Tree.as. Here we are telling trunk to render at full scale and setting its parent to the root &lt;code&gt;MovieClip&lt;/code&gt;. We can also remove the &lt;code&gt;addChild(tree)&lt;/code&gt; line since the &lt;code&gt;Branch&lt;/code&gt; constructor takes care of adding branches to the stage.&lt;/p&gt;

					&lt;div class=&quot;code&quot;&gt;
&lt;pre&gt;// Tree.as
trunk = new Branch(-90, &lt;strong&gt;1&lt;/strong&gt;, new Point(stage.stageWidth/2, stage.stageHeight), &lt;strong&gt;this&lt;/strong&gt;);&lt;/pre&gt;
	&lt;/div&gt;

					&lt;p&gt;Compile and run this iteration. You should see a more densely populated tree. &lt;/p&gt;

					&lt;img src=&quot;/images/articles/1/tree_2.jpg&quot; alt=&quot;Tree 2&quot; style=&quot;margin-bottom:11px&quot;/&gt;
	
					&lt;p&gt;That's not bad but it doesn't look very natural. We could improve this by adding a little randomness to the branches. Change lines 37 and 38 of Branch.as to look like this:&lt;/p&gt;
					
					&lt;div class=&quot;code&quot;&gt;
&lt;pre&gt;// Branch.as
var branchLeft:Branch = new Branch(angle - 30 + Math.random()*20 - 10, scale, globalEndPoint, thisParent);
var branchRight:Branch = new Branch(angle + 30 + Math.random()*20 - 10, scale, globalEndPoint, thisParent);
&lt;/pre&gt;
					&lt;/div&gt;

					&lt;p&gt;Now all of the the branches should be rotated + or - 30 degrees from their parent branches plus and additional random amount somewhere between 10 and -10. This looks more natural: &lt;/p&gt;

					&lt;img src=&quot;/images/articles/1/tree_3.jpg&quot; alt=&quot;Tree 3&quot; style=&quot;margin-bottom:11px&quot;/&gt;

					&lt;h2&gt;Generating Wind&lt;/h2&gt;

					&lt;p&gt;I could easily spend all day tweaking the size and shape of this tree but then we'd never get to animate it. Time to move on.&lt;/p&gt;

					&lt;p&gt;The first thing we're going to do is write some event handlers. One handler will trigger the &quot;generation of wind&quot; when the user clicks somewhere on the stage. Another handler will do the wind generation by moving the branches on &lt;code&gt;ENTER_FRAME&lt;/code&gt; events. And finally, another handler will remove the wind generation event handler when the user releases the mouse button. &lt;/p&gt;

					&lt;div class=&quot;code&quot;&gt;
&lt;pre&gt;// Tree.as
package {  

  import flash.display.*;
  import flash.geom.Point;  
  &lt;strong&gt;import flash.events.*;&lt;/strong&gt;

  public class Tree extends MovieClip{
    private var trunk:Branch;

    public function Tree(){
      trunk = new Branch(-90, 1, new Point(stage.stageWidth/2, stage.stageHeight), this);
      &lt;strong&gt;stage.addEventListener(MouseEvent.MOUSE_DOWN, startWind);
      stage.addEventListener(MouseEvent.MOUSE_UP, endWind);&lt;/strong&gt;
    }

    &lt;strong&gt;private function startWind(e:MouseEvent):void{
      addEventListener(Event.ENTER_FRAME, generateWind);
    }

    private function endWind(e:MouseEvent):void{
      removeEventListener(Event.ENTER_FRAME, generateWind);
    }

    private function generateWind(e:Event):void{
      trunk.bend();
    }&lt;/strong&gt;

  }
}&lt;/pre&gt;
					&lt;/div&gt;

					&lt;p&gt;The other piece we need to add in this step is the &lt;code&gt;bend()&lt;/code&gt; method of the &lt;code&gt;Branch&lt;/code&gt; class:&lt;/p&gt;

					&lt;div class=&quot;code&quot;&gt;
						&lt;pre&gt;// Branch.as

public function bend():void{
  angle = radToDeg(Math.atan2(mouseY + endPoint.y, mouseX + endPoint.x));
  endPoint = getEndPoint();
  globalEndPoint = localToGlobal(endPoint);
  drawBranch();
}
					&lt;/pre&gt;&lt;/div&gt;

					&lt;p&gt;This method finds the angle of an imaginary line between the mouse and the &lt;code&gt;endPoint&lt;/code&gt; of the branch, moves the &lt;code&gt;endPoint&lt;/code&gt; so that it lies on that imaginary line, then redraws the branch. It will take a few frames but the branch will end up pointing at the mouse. &lt;/p&gt;

					&lt;p&gt;Next, we want this method to propagate through the whole tree. This part is a little bit magical. After the &lt;code&gt;bend()&lt;/code&gt; function we will reposition each of the branch's children to the the branch's &lt;code&gt;endPoint&lt;/code&gt; and then call &lt;code&gt;bend()&lt;/code&gt; on each. Remember that each branch has at most only 2 child branches, but since the &lt;code&gt;bend()&lt;/code&gt; method is the same for each child branch as it is for the trunk, it will in turn call &lt;code&gt;bend()&lt;/code&gt; on the child branches of each of those branches and so on until there are no child branches left. I've broken this task out into a function called &lt;code&gt;positionChildren()&lt;/code&gt;:&lt;/p&gt;

					&lt;div class=&quot;code&quot;&gt;
						&lt;pre&gt;// Branch.as

public function bend():void{
  angle = radToDeg(Math.atan2(mouseY + endPoint.y, mouseX + endPoint.x)) ;
  endPoint = getEndPoint();
  globalEndPoint = localToGlobal(endPoint);
  drawBranch();
  &lt;strong&gt;positionChildren();&lt;/strong&gt;
}

&lt;strong&gt;private function positionChildren():void{
  globalEndPoint = localToGlobal(endPoint);
  for each(var child:Branch in children){
    child.x = globalEndPoint.x;
    child.y = globalEndPoint.y;
    child.bend();
  }
}&lt;/strong&gt;
					&lt;/pre&gt;&lt;/div&gt;

					&lt;p&gt;Now all the branches point toward the mouse when the user clicks on the stage. &lt;/p&gt;
					
					&lt;p&gt;This movement happens a little too quickly. Also, if the mouse is supposed to be the point of wind generation then they should be moving away from it rather than towards it. Let's slow it down and reorient the branches by modifying the &lt;code&gt;bend()&lt;/code&gt; function of the &lt;code&gt;Branch&lt;/code&gt; class:&lt;/p&gt;
					
					&lt;div class=&quot;code&quot;&gt;
						&lt;pre&gt;// Branch.as
public function bend():void{
  &lt;strong&gt;var windAngle:Number = radToDeg(Math.atan2(mouseY + endPoint.y, mouseX + endPoint.x)) - 180;

  if(windAngle - angle &amp;lt; -180){
    windAngle += 360;
  } else if(windAngle - angle&gt; 360){
    windAngle -= 360;
  }

  if(Math.abs(windAngle - angle) &gt; 1){
    angle += (windAngle - angle)/8;
    endPoint = getEndPoint();
    globalEndPoint = localToGlobal(endPoint);
    drawBranch()
  }&lt;/strong&gt;

  positionChildren();
}&lt;/pre&gt;
					&lt;/div&gt;

					&lt;p&gt;First we flip the angle we're working with (the angle between the mouse and the endpoint of the branch) and store it in a variable called &lt;code&gt;windAngle&lt;/code&gt;. This will make the branches point away from the mouse rather than toward it. It is the target angle that we want the branch's angle to eventually reach. In the following &lt;code&gt;if()&lt;/code&gt; statement we fudge the numbers a bit so that the difference between the target angle and the current angel is always more than -180 and less than 180. This is to prevent erratic behavior at certain angles. For example, if the target angle were 10 and the branch's current angle were -10 we would want the difference to be considered to be 20 degrees (the short way around the circle) rather than -340 degrees (the long way around). &lt;/p&gt;

					&lt;p&gt;The next step determines how quickly the branch rotates to the target angle. We reset the branch's &lt;code&gt;angle&lt;/code&gt; to a number that is somewhere between the current angle and &lt;code&gt;windAngle&lt;/code&gt;. We adjust the &lt;code&gt;angle&lt;/code&gt; by 1/8 of the difference between &lt;code&gt;angle&lt;/code&gt; and &lt;code&gt;windAngle&lt;/code&gt;. This is similar to adding a motion tween with an ease out property to a frame in the timeline.&lt;/p&gt;

					&lt;p&gt;Next lets add resistance. We'll add an another &lt;code&gt;ENTER_FRAME&lt;/code&gt; event listener to the &lt;code&gt;Tree&lt;/code&gt; class. On every frame the event handler &lt;code&gt;resist()&lt;/code&gt; will call a method of the &lt;code&gt;Branch&lt;/code&gt; class called &lt;code&gt;returnToRest()&lt;/code&gt;. &lt;code&gt;returnToRest()&lt;/code&gt; will work in opposition to &lt;code&gt;bend()&lt;/code&gt; and attempt to move the branch back to it's original orientation. &lt;/p&gt;

					&lt;div class=&quot;code&quot;&gt;
&lt;pre&gt;// Tree.as
package {

  import flash.display.*;
  import flash.geom.Point;  
  import flash.events.*;


  public class Tree extends MovieClip{
    private var trunk:Branch;

    public function Tree(){
      trunk = new Branch(-90, 1, new Point(stage.stageWidth/2, stage.stageHeight), this);
      stage.addEventListener(MouseEvent.MOUSE_DOWN, startWind);
      stage.addEventListener(MouseEvent.MOUSE_UP, endWind);
      &lt;strong&gt;addEventListener(Event.ENTER_FRAME, resist);&lt;/strong&gt;
    }

    &lt;strong&gt;private function resist(e:Event):void{
      trunk.returnToRest();
    }&lt;/strong&gt;

    private function startWind(e:MouseEvent):void{
      addEventListener(Event.ENTER_FRAME, generateWind);
    }

    private function endWind(e:MouseEvent):void{
      removeEventListener(Event.ENTER_FRAME, generateWind);
    }

    private function generateWind(e:Event):void{
      trunk.bend();
    }

  }
}&lt;/pre&gt;
					&lt;/div&gt;

					&lt;p&gt;The first change to the Branch class is to store the angel that the branch starts out at. This is the variable &lt;code&gt;restAngle&lt;/code&gt;. Along with angle set it to &lt;code&gt;initialAngle&lt;/code&gt; in the constructor.&lt;/p&gt;
					
					&lt;div class=&quot;code&quot;&gt;
&lt;pre&gt;//Branch.as
&lt;strong&gt;public var restAngle:Number;&lt;/strong&gt;

  public function Branch(initialAngle:Number, scale:Number, origin:Point, parentClip:MovieClip){
  &lt;strong&gt;angle = restAngle = initialAngle;&lt;/strong&gt;
  ...&lt;/pre&gt;
					&lt;/div&gt;

					&lt;p&gt;The &lt;code&gt;returnToRest()&lt;/code&gt; function works similarly to &lt;code&gt;bend()&lt;/code&gt; in that once it is called on trunk it propagates through the entire tree. It uses the same technique that we used to bend the branch away from the mouse, to bend it back to &lt;code&gt;restAngle&lt;/code&gt;. &lt;code&gt;returnToRest()&lt;/code&gt; makes use of the function &lt;code&gt;unbendChildren()&lt;/code&gt; which does essentially the same thing as &lt;code&gt;positionChildren()&lt;/code&gt;. It moves child branches to the current branch's &lt;code&gt;endPoint&lt;/code&gt; and calls &lt;code&gt;returnToRest()&lt;/code&gt; on each of them.&lt;/p&gt;

					&lt;div class=&quot;code&quot;&gt;
&lt;pre&gt;// Branch.as

    public function returnToRest():void{
      if(Math.abs(restAngle - angle) &gt; 1){
        angle += (restAngle - angle)/8;
        endPoint = getEndPoint();
        globalEndPoint = localToGlobal(endPoint);
        // redraw
        drawBranch();
      }

      unbendChildren();
    }

    private function unbendChildren():void{
      globalEndPoint = localToGlobal(endPoint);
      for each(var child:Branch in children){
        child.x = globalEndPoint.x;
        child.y = globalEndPoint.y;
        child.returnToRest();
      }
    }
&lt;/pre&gt;
					&lt;/div&gt;

					&lt;p&gt;Compile and run the movie and you should have a tree that bends away from the mouse when you click and drag around the stage. &lt;/p&gt;

					&lt;p&gt;It seems to me that smaller branches closer to the source of wind should be affected more than those that are further away from it. I created a &lt;code&gt;strength&lt;/code&gt; variable in the &lt;code&gt;bend()&lt;/code&gt; function that is used to influence how much a branch will bend on each frame. The value of &lt;code&gt;strength&lt;/code&gt; is the distance between the mouse and the branch's &lt;code&gt;endPoint&lt;/code&gt;. This is found by using the pythagorean theorem (a&lt;sup&gt;2&lt;/sup&gt; + b&lt;sup&gt;2&lt;/sup&gt; = c&lt;sup&gt;2&lt;/sup&gt;) on &lt;code&gt;distX&lt;/code&gt; and &lt;code&gt;distY&lt;/code&gt;.&lt;/p&gt;
					
					&lt;div class=&quot;code&quot;&gt;
						&lt;pre&gt;// Branch.as

public function bend():void{
  var windAngle:Number = radToDeg(Math.atan2(mouseY + endPoint.y, mouseX + endPoint.x)) - 180;
  &lt;strong&gt;var distX:Number = mouseX + endPoint.x;
  var distY:Number = mouseY + endPoint.y;
  var strength:Number = Math.sqrt(distX*distX + distY*distY);&lt;/strong&gt;

  if(windAngle - angle &amp;lt; -180){
    windAngle += 360;
  } else if(windAngle - angle&gt; 360){
    windAngle -= 360;
  }


  if(Math.abs(windAngle - angle) &gt; 1){
    angle += (windAngle - angle)/&lt;strong&gt;strength&lt;/strong&gt;;
    endPoint = getEndPoint();
    globalEndPoint = localToGlobal(endPoint);
    drawBranch()
  }

  positionChildren();
}&lt;/pre&gt;
					&lt;/div&gt;
					
					&lt;p&gt;Additionally, larger branches should not bend as much as smaller ones since they are thicker and less flexible. The variable &lt;code&gt;strength&lt;/code&gt; can be truncated based on the branch's scale:&lt;/p&gt;

					&lt;div class=&quot;code&quot;&gt;&lt;pre&gt;var strength:Number = (Math.sqrt(distX*distX + distY*distY)*scaleX*scaleX)/5;&lt;/pre&gt;&lt;/div&gt;

					&lt;p&gt;Multiplying the distance by &lt;code&gt;scaleX&lt;/code&gt; twice exaggerates the difference in how much a smaller branch bends compared to how much a larger branch bends.  Dividing it all by 5 decreases the overall strength of the wind. &lt;/p&gt;

					&lt;p&gt;I have one final adjustment before calling it a day. The wind is a little too steady for my taste. Adding some turbulence will make it a little more realistic. Update the &lt;code&gt;generateWind()&lt;/code&gt; function of Tree to look like this:&lt;/p&gt;

					&lt;div class=&quot;code&quot;&gt;
						&lt;pre&gt;// Tree.as
private function generateWind(e:Event):void{
  &lt;strong&gt;var turbulence:Number = Math.random()*40 - 20;
  trunk.bend(turbulence);&lt;/strong&gt;
}
						&lt;/pre&gt;
					&lt;/div&gt;

					&lt;p&gt;&lt;code&gt;turbulence&lt;/code&gt; is a random number between -20 and 20. Pass this to &lt;code&gt;bend()&lt;/code&gt;. In &lt;code&gt;bend()&lt;/code&gt; add &lt;code&gt;turbulence&lt;/code&gt; to &lt;code&gt;windAngle&lt;/code&gt;:&lt;/p&gt;

					&lt;div class=&quot;code&quot;&gt;
						&lt;pre&gt;// Branch.as
public function bend(turbulence:Number):void{
  var windAngle:Number = radToDeg(Math.atan2(mouseY + endPoint.y, mouseX + endPoint.x)) - 180;
  windAngle += turbulence;
...&lt;/pre&gt;
					&lt;/div&gt;

					&lt;p&gt;Don't forget to pass it on to &lt;code&gt;positionChildren()&lt;/code&gt; at the end of&lt;code&gt; bend()&lt;/code&gt;...&lt;/p&gt;

					&lt;div class=&quot;code&quot;&gt;&lt;pre&gt;positionChildren(turbulence);&lt;/pre&gt;&lt;/div&gt;

					&lt;p&gt;...and to &lt;code&gt;bend()&lt;/code&gt; again in &lt;code&gt;positionChildren()&lt;/code&gt;:&lt;/p&gt;

					&lt;div class=&quot;code&quot;&gt;&lt;pre&gt;child.bend(turbulence);&lt;/pre&gt;&lt;/div&gt;

					&lt;p&gt;The final classes should look like this:&lt;/p&gt;

					&lt;div class=&quot;code&quot;&gt;
&lt;pre&gt;// Tree.as
package {

  import flash.display.*;
  import flash.geom.Point;  
  import flash.events.*;

  public class Tree extends MovieClip{
    private var trunk:Branch;

    public function Tree(){
      trunk = new Branch(-90, 1, new Point(stage.stageWidth/2, stage.stageHeight), this);
      stage.addEventListener(MouseEvent.MOUSE_DOWN, startWind);
      stage.addEventListener(MouseEvent.MOUSE_UP, endWind);
      addEventListener(Event.ENTER_FRAME, resist);
    }

    private function resist(e:Event):void{
      trunk.returnToRest();
    }

    private function startWind(e:MouseEvent):void{
      addEventListener(Event.ENTER_FRAME, generateWind);
    }

    private function endWind(e:MouseEvent):void{
      removeEventListener(Event.ENTER_FRAME, generateWind);
    }

    private function generateWind(e:Event):void{
      var turbulence:Number = Math.random()*40 - 20;
      trunk.bend(turbulence);
    }

  }
}&lt;/pre&gt;
					&lt;/div&gt;
					
					&lt;div class=&quot;code&quot;&gt;
&lt;pre&gt;// Branch.as
package {
  
  import flash.display.*;
  import flash.geom.Point;

  public class Branch extends Shape{

    // class vars
    private static const baseLength:Number = 100;
    private static const minScale:Number = 0.2;
    private static const slope:Number = 0.8;

    // instance vars
    private var angle:Number;
    private var endPoint:Point;
    private var globalEndPoint:Point;
    private var thisParent:MovieClip
    private var children:Array = [];
    public var restAngle:Number;

    public function Branch(initialAngle:Number, scale:Number, origin:Point, parentClip:MovieClip){
      angle = restAngle = initialAngle;
      x = origin.x;
      y = origin.y;
      thisParent = parentClip;
      scaleX = scaleY = scale;
      endPoint = getEndPoint();
      globalEndPoint = localToGlobal(endPoint);
      drawBranch();
      thisParent.addChild(this);
      if(scale*slope &gt; minScale){
        sproutBranches(scale*slope);
      }
    }

    private function sproutBranches(scale:Number):void{
      // new branch angles are augmented by a random +- 10 degrees
      var branchLeft:Branch = new Branch(angle - 30 + Math.random()*20 - 10, scale, globalEndPoint, thisParent);
      var branchRight:Branch = new Branch(angle + 30 + Math.random()*20 - 10, scale, globalEndPoint, thisParent);
      children.push(branchLeft);
      children.push(branchRight);
    }

    public function bend(turbulence:Number):void{
      var windAngle:Number = radToDeg(Math.atan2(mouseY + endPoint.y, mouseX + endPoint.x)) - 180;
      windAngle += turbulence;
      var distX:Number = mouseX + endPoint.x;
      var distY:Number = mouseY + endPoint.y;
      var strength:Number = (Math.sqrt(distX*distX + distY*distY)*scaleX*scaleX)/5;

      if(windAngle - angle &amp;lt; -180){
        windAngle += 360;
      } else if(windAngle - angle&gt; 360){
        windAngle -= 360;
      }


      if(Math.abs(windAngle - angle) &gt; 1){
        angle += (windAngle - angle)/strength;
        endPoint = getEndPoint();
        globalEndPoint = localToGlobal(endPoint);
        drawBranch()
      }

      positionChildren(turbulence);
    }

    private function positionChildren(turbulence:Number):void{
      globalEndPoint = localToGlobal(endPoint);
      for each(var child:Branch in children){
        child.x = globalEndPoint.x;
        child.y = globalEndPoint.y;
        child.bend(turbulence);
      }
    }

    private function drawBranch():void{
      graphics.clear();
      graphics.moveTo(0, 0);
      graphics.lineStyle(10, 0x000000);
      graphics.lineTo(endPoint.x, endPoint.y);
    }

    public function returnToRest():void{
      if(Math.abs(restAngle - angle) &gt; 1){
        angle += (restAngle - angle)/8;
        endPoint = getEndPoint();
        globalEndPoint = localToGlobal(endPoint);
        // redraw
        drawBranch();
      }

      unbendChildren();
    }

    private function unbendChildren():void{
      globalEndPoint = localToGlobal(endPoint);
      for each(var child:Branch in children){
        child.x = globalEndPoint.x;
        child.y = globalEndPoint.y;
        child.returnToRest();
      }
    }

    // Using angle and baseLength as polar coordinates get the endPoint of the line as cartesian coordinates. 
    private function getEndPoint():Point{
      return Point.polar(baseLength, degToRad(angle));
    }

    // convert degerees to radians
    private static function degToRad(deg:Number):Number{
      return deg/57.3;
    }

    // convert radians to degrees
    private static function radToDeg(rad:Number):Number{
      return rad*57.3;
    }

  }

}&lt;/pre&gt;
					&lt;/div&gt;
					&lt;p&gt;Now the tree quivers in the wind as well.&lt;/p&gt;

					&lt;h2&gt;Bonus&lt;/h2&gt;

					&lt;p&gt;Here are a few screenshots I took during the development of this movie. These are all unexpected results from testing and debugging. This demonstrates how tweaking one or two variables can result in vastly different patterns. There is much room for exploration here.&lt;/p&gt;

					&lt;img src=&quot;/images/articles/1/bonus.gif&quot; alt=&quot;Bonus screenshots&quot;/&gt;
					&lt;script type=&quot;text/javascript&quot;&gt;
							var flashvars = {};
							var params = { scale:'exactfit'};
							swfobject.embedSWF(&quot;/flash/Tree.swf&quot;, &quot;example&quot;, &quot;504&quot;, &quot;432&quot;, &quot;9.0.0&quot;, &quot;http://sethmabbott.com/flash/expressInstall.swf&quot;, flashvars, params);
					&lt;/script&gt;</description>
      <pubDate>Thu, 22 Oct 2009 18:38:56 -0400</pubDate>
      <link>http://www.sethmabbott.com/posts/11</link>
      <guid>http://www.sethmabbott.com/posts/11</guid>
    </item>
    <item>
      <title>New Dragons Power Up! Posters</title>
      <description>&lt;a href='http://sethmabbott.com/tags/15/images'&gt;&lt;img src='http://sethmabbott.com/images/crp.jpg'&gt;&lt;/a&gt;&lt;br/&gt;&lt;br/&gt;
St. Paul band &lt;a href=&quot;http://www.dragonspowerup.com/&quot;&gt;Dragons Power Up!&lt;/a&gt; recently released a new record and I recently finished printing some new posters for them.&lt;a href=&quot;http://sethmabbott.com/tags/15/images&quot;&gt;Here are four variations&lt;/a&gt;. </description>
      <pubDate>Sun, 20 Sep 2009 16:17:19 -0400</pubDate>
      <link>http://www.sethmabbott.com/posts/10</link>
      <guid>http://www.sethmabbott.com/posts/10</guid>
    </item>
    <item>
      <title>Music: Two New Tracks</title>
      <description>&lt;a href='http://sethmabbott.com/songs'&gt;&lt;img src='http://sethmabbott.com/images/flower-twostar.jpg'&gt;&lt;/a&gt;&lt;br/&gt;&lt;br/&gt;
I've been working on a series of tracks that are all built up from a single click sample. The sample is filtered, shaped and resampled to make a wide variety of sounds. You may now hear drafts of two of these tracks, &lt;a href=&quot;http://sethmabbott.com/songs/8&quot;&gt;Chickpea&lt;/a&gt; and &lt;a href=&quot;http://sethmabbott.com/songs/7&quot;&gt;Bocce Ball&lt;/a&gt;, in the &lt;a href='http://sethmabbott.com/songs'&gt;Sound&lt;/a&gt; section of sethmabbott.com. &lt;a href=&quot;mailto:hello@sethmabbott.com&quot;&gt;Let me know what you think&lt;/a&gt;.</description>
      <pubDate>Sat, 01 Aug 2009 21:32:32 -0400</pubDate>
      <link>http://www.sethmabbott.com/posts/9</link>
      <guid>http://www.sethmabbott.com/posts/9</guid>
    </item>
    <item>
      <title>The Grace of Falling vs Aliens!</title>
      <description>&lt;img src=&quot;/images/alienTech3_crop.jpg&quot;/&gt;
&lt;p&gt;Lately I've been working on some artwork for the Minneapolis band &lt;a href=&quot;http://www.myspace.com/tgof&quot;&gt;The Grace of Falling&lt;/a&gt;. I've been talking with the band about what sort of visual identity they would like to have. We started out with mathematical symbols and gravitated toward the triangle of dots which symbolize &quot;therefore&quot;. This branched out in a number of directions. At the moment I'm working on what has turned into the &quot;ancient alien amplifier knobs&quot; or &quot;flux capacitor&quot; direction.&lt;/p&gt;</description>
      <pubDate>Sat, 09 May 2009 23:47:26 -0400</pubDate>
      <link>http://www.sethmabbott.com/posts/8</link>
      <guid>http://www.sethmabbott.com/posts/8</guid>
    </item>
    <item>
      <title>Lost and Found Music: Draft for Caleb</title>
      <description>&lt;a href=&quot;http://sethmabbott.com/songs/6&quot;&gt;&lt;img src=&quot;/images/pattern4d.jpg&quot; class=&quot;has-border&quot; alt=&quot;Pattern4d&quot;/&gt;&lt;/a&gt;
&lt;p&gt;I was poking around some old files and came across &lt;a href=&quot;http://sethmabbott.com/songs/6&quot;&gt;this track&lt;/a&gt; I made for a &lt;a href=&quot;http://calebcoppock.com&quot;&gt;friend&lt;/a&gt; of mine for his birthday. If I remember correctly it is made up of appropriated sounds from a track of his that he had sent me. Credit goes to &lt;a href=&quot;http://calebcoppock.com&quot;&gt;Caleb Coppock&lt;/a&gt; for the source material. &lt;a href=&quot;http://sethmabbott.com/songs/6&quot;&gt;Listen&lt;/a&gt;&lt;/p&gt;</description>
      <pubDate>Sat, 02 May 2009 17:40:31 -0400</pubDate>
      <link>http://www.sethmabbott.com/posts/7</link>
      <guid>http://www.sethmabbott.com/posts/7</guid>
    </item>
    <item>
      <title>The I.D.E.S. of April</title>
      <description>I recorded a short &lt;a href=&quot;http://sethmabbott.com/songs/5&quot;&gt;dj mix&lt;/a&gt; today in honor of the Illinois Department of Employment Security. It includes some old favorites and a track or two that I didn't know I had. </description>
      <pubDate>Fri, 17 Apr 2009 22:04:03 -0400</pubDate>
      <link>http://www.sethmabbott.com/posts/6</link>
      <guid>http://www.sethmabbott.com/posts/6</guid>
    </item>
    <item>
      <title>Patterns</title>
      <description>&lt;a href=&quot;http://sethmabbott.com/tags/12/images&quot;&gt;&lt;img src=&quot;http://sethmabbott.com/images/pattern1a.jpg&quot;/&gt;&lt;/a&gt;
&lt;p&gt;I just uploaded a &lt;a href=&quot;http://sethmabbott.com/tags/12/images&quot;&gt;new set of images&lt;/a&gt;. These are sketches of patterns which will eventually make there way into a screen printed book.&lt;/p&gt; </description>
      <pubDate>Fri, 17 Apr 2009 13:38:56 -0400</pubDate>
      <link>http://www.sethmabbott.com/posts/5</link>
      <guid>http://www.sethmabbott.com/posts/5</guid>
    </item>
    <item>
      <title>Pool Party: Playing with Physics in Flash</title>
      <description>&lt;em&gt;Part 1 of a 3 part tutorial on physics for flash games. Originally posted on the &lt;a href='http://www.killswitchcollective.com/articles/50_pool_party_playing_with_physics_in_flash'&gt;Killswitch Collective Blog&lt;/a&gt;.&lt;/em&gt;&lt;br/&gt;

I thought this would be a good opportunity to put together the first Killswitch multi-part flash tutorial. In this tutorial we'll build a simple but highly customizable billiards game... &lt;a href='http://www.killswitchcollective.com/articles/50_pool_party_playing_with_physics_in_flash'&gt;more&lt;/a&gt;</description>
      <pubDate>Sun, 15 Feb 2009 16:17:15 -0500</pubDate>
      <link>http://www.sethmabbott.com/posts/4</link>
      <guid>http://www.sethmabbott.com/posts/4</guid>
    </item>
    <item>
      <title>Creating Communicative UI</title>
      <description>&lt;em&gt;Originally posted on the &lt;a href='http://www.killswitchcollective.com/blog/2008/08'&gt;Killswitch Collective Blog&lt;/a&gt;.&lt;/em&gt;&lt;br/&gt;

Actually, I'd prefer that people would forget tag clouds. I'm tired of tag clouds. Granted, a tag cloud is intended to digest data into a more immediately understandable form, but in most cases it comes out as the sort of thing that makes typographers cry... &lt;a href='http://www.killswitchcollective.com/blog/2008/08'&gt;more&lt;/a&gt;. </description>
      <pubDate>Sun, 30 Nov 2008 18:44:32 -0500</pubDate>
      <link>http://www.sethmabbott.com/posts/3</link>
      <guid>http://www.sethmabbott.com/posts/3</guid>
    </item>
  </channel>
</rss>

