package hype.extended.layout { import hype.framework.layout.AbstractLayout; import hype.framework.layout.ILayout; import flash.geom.Point; /* Layout that produces a Koch Curve l-system */ public class KochCurveLayout extends AbstractLayout implements ILayout { private var _index:uint; private var _x:Number; private var _y:Number; private var _iterations:uint; //number of steps in sequence to draw - if you go much above 7 or 8, it's going to be huge private var _dist:Number = 10; //pixel distance between points private var _deg2rad:Number = Math.PI / 180; //used in our point calculation private var _angle:Number = 0; //angle we draw to from our current point private var _min:Number; private var _max:Number; private var _range:Number; public var total_points:uint; public var total_turns:uint; public function KochCurveLayout(x:Number, y:Number, iterations:Number = 4, min:Number = 10, max:Number = 10) { _index = 0; _x = x; _y = y; _iterations = iterations; getTotalPoints(); _min = min; _max = max; _range = Math.abs(_max - _min); } public function getTotalPoints() { //the total number of points we can plot total_points = Math.pow(5, _iterations) + 1;//the plus 1 is the special point for our 0 index iteration total_turns = total_points - 2; //turns is -1 and we also -1 the 0 index point return total_points; } public function getNextPoint():Point { var pt:Point = new Point(); pt.x = _x; pt.y = _y; changeDirection(_index); ++_index; return pt; } public function changeDirection(step:Number) { /* Special rule for index 0 as L-Systems assumes a draw forward for each iteration but we are simply plotting the next point */ if (step == 0) { //move straight to the left from where we are var temp_r:Number = _angle * _deg2rad; _dist = _min + (Math.random() * _range); _x = Math.cos(temp_r) * _dist + _x; _y = Math.sin(temp_r) * _dist + _y; return; } if (step % 5 == 0) { //not in LR pattern group changeDirection(step / 5); /* Just to note, I really don't like this recursive lookup, but I'm struggling to find an equation that works the way the below does for the LR combos */ } else { var dir:Number = step % 5; if (dir == 1 || dir == 4) { //turning left _angle = _angle - 90; } else { //turning right _angle = _angle + 90; } var r:Number = _angle * _deg2rad; _dist = _min + (Math.random() * _range); //_x = Math.round(Math.cos(r) * _dist + _x); _x = Math.cos(r) * _dist + _x; //_y = Math.round(Math.sin(r) * _dist + _y); _y = Math.sin(r) * _dist + _y; } } //end changeDirection } //end KochCurve class } //end package