public void Populate(int targetPopulation, double childDeviation, double piOffset, double relativeChildLength, Pen childPen) { //There's n children, growing from the End of this branch, each deviating from the parent in an angle between -childDeviation and childDeviation double angleThisBranch = AngleMath.GetAngleInDegrees(Origin, End); //What's the absolute orientation of this branch? double angleOfFirstChild = angleThisBranch - childDeviation; //What's the absolute orientation of the first child at -childDeviation? double angleNeighbourDifference = childDeviation * 2 / targetPopulation; //What's the angle difference between the first child and the next? while (Children.Count <= targetPopulation) //Until we hit the target population { //Find absolute angle for this child double childAngle = angleOfFirstChild + (angleNeighbourDifference * Children.Count); //Find the End of the child using the newfound childAngle PointF childEnd = AngleMath.GetPointOnEdgeOfCircle(End.X, End.Y, AngleMath.GetDistance(Origin, End) * relativeChildLength, childAngle, piOffset ); //Instantiate the child Branch child = new Branch(End, //The origin of the child is the End of this branch childEnd, //The End of the child childPen, this.Origin); //Add it to the children collection so that this method may end someday Children.Add(child); } }
List <Branch> BuildBranches(PointF center, int maxGenerations, int childCount, double childDeviation, double piOffset, double relativeChildLength, int rootCount, double zoomLevel, Pen rootPen, double childHueChange) { //Initialize tree List <Branch> tree = new List <Branch>(); //Build the roots int angleStep = 360 / rootCount; for (int i = 0; i < rootCount; i++) { Branch root = new Branch(center, AngleMath.GetPointOnEdgeOfCircle(center.X, center.Y, zoomLevel, (double)i * angleStep - 90, piOffset), rootPen, center); tree.Add(root); } //Build the tree int generation = 0; while (generation < maxGenerations) { //remember the branch count now, because you'll be adding new elements during the cycle //their new index will however be higher than the current population int branchCount = tree.Count; for (int i = 0; i < branchCount; i++) //do not exceed the original population in this generation { //if this branch has no kids yet (happens only once per branch) if (tree[i].Children.Count == 0) { Pen parentPen = tree[i].Pen; //ajdust the pen for the children if needed int a = parentPen.Color.A; float h = parentPen.Color.GetHue() + (float)childHueChange; float s = parentPen.Color.GetSaturation(); float b = parentPen.Color.GetBrightness(); Color childColor = ColorConverter.FromAhsb(a, h, s, b); Pen childPen = new Pen(childColor, parentPen.Width); //make some kids tree[i].Populate(childCount - 1, //no idea why I have to use a magic number here, TODO: Investigate childDeviation, piOffset, relativeChildLength, childPen); foreach (Branch babyBranch in tree[i].Children) { //add them to the main list tree.Add(babyBranch); } } } generation++; } return(tree); }