Sunflowers, Spirals, Flash

Recently a project came up that had the potential to use an abstract sunflower as a lovely motif. After looking into it a bit, there happened to be an equation describing the arrangement of seeds in a sunflower. The equations looked fairly straightforward, but I’m no math wizard and had no idea to figure out the x and y coordinates of the ‘seeds’, but lucky for me I happen to sit across from someone who knows what the hell they’re doing and as a result I have a simple sunflower generator:

To view this content, JavaScript must be enabled, and you need the latest version of the Adobe Flash Player.

The Code

You will need to have a Slider component in your library for this to compile (or remove all references to the slider until it compiles). If you’re into this type of thing then definitely play around with the constant scaling factor, max number of seeds, and the golden ratio number in the angle (137.5).

/**
 * Sunflower Generator
 * June 28, 2009
 *
 * @author      Ben Watts
 * @url         http://www.benwatts.ca/
 * @post        http://www.benwatts.ca/2009/06/28/sunflowers-spirals-flash/
 **/


package{
   
    import flash.display.*;
    import fl.controls.Slider;
    import fl.events.SliderEvent;
   
    public class Sunflower extends MovieClip{
       
       
        var maxSeeds:Number = 9000;  // # of seeds
        var c:Number = 4; // constant scaling factor (distance btw seeds)
        var angle:Number = 0;
       
       
        public function Sunflower(){
           
            drawSunflower(.1); // start with something
           
            var slider:Slider = new Slider();
            slider.liveDragging = true;
            slider.minimum = 1;
            slider.setSize(150, 5);
            slider.x = stage.stageWidth - slider.width - 10;
            slider.y = 10;
            slider.addEventListener(SliderEvent.CHANGE, sliderChange);
            addChild(slider);          
        }
       
        /**
         * Event handler, whenever the slider's value changes
         * @param       e   - SliderEvent
         */

        private function sliderChange(e:SliderEvent):void{
            drawSunflower(e.value/10); // returns value btw 1.0 and 10.0
        }
       
       
        /**
         * Draws the sunflower.
         *
         *  @param      value   - the % of the max number of seeds that are set to be drawn. Expects a float 0 - 1.0
         */

        private function drawSunflower(value:Number):void{
           
            var n:Number = 0; // current seed
            var numSeeds:Number = maxSeeds * value; // how many seeds should be made
           
            try {
                removeChild(getChildByName('seed-container'));
            } catch(e:Error){ /* this makes me a terrible person, i know. */ }
           
            var container:MovieClip = new MovieClip();
            container.name = 'seed-container';
            addChild(container);


            for( n; n < numSeeds; n++ ){
                var seed:Sprite = new Sprite();
                seed.graphics.beginFill(0xff0000 - (n*15), 1.0); // messing with seed colour, don't really know what i'm doing ;)
                seed.graphics.drawCircle(0,0,2);
                seed.graphics.endFill();
               
                var r:Number = c * Math.sqrt(n);
                var angle:Number = n * 137.5;
               
                seed.x = r * Math.sin(angle);
                seed.y = r * Math.cos(angle);
                container.addChild(seed);
            }

            // centre on the stage
            container.x += stage.stageWidth/2;
            container.y += stage.stageHeight/2;
                       
        }
       
       
       
       
    }
   
}

Leave a Reply