package hype.extended.layout { import hype.framework.layout.AbstractLayout; import hype.framework.layout.ILayout; import flash.geom.Point; /* Layout that produces a terdragon l-system */ public class TerdragonLayout 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 = 120; //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; // total_turns is basically same as total_points - 1. // Not much use for this yet. It can be used to determine // if we've finished drawing the terdragon. public function TerdragonLayout(x:Number, y:Number, iterations:Number = 4, min:Number = 10, max:Number = 10) { _index = 0; _x = x; _y = y; _dist = min; _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(3, _iterations) + 1; total_turns = total_points - 2; 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) { if (step == 0) { var temp_r:Number = 120 * _deg2rad; _dist = _min + (Math.random() * _range); _x = Math.cos(temp_r) * _dist + _x; _y = Math.sin(temp_r) * _dist + _y; return; } if (step % 3 == 0) { //not in LR pattern group changeDirection(step / 3); /* 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 { if ((step - 1) % 3 == 0) { //turning left _angle = _angle - 120; } else { //turning right _angle = _angle + 120; } 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 Terdragon class } //end package