示例#1
0
      private void Draw(WriteableBitmap writeableBmp, Branch branch)
      {
         int[] pts = new int[] 
         { 
            branch.Start.X,   branch.Start.Y,
            branch.Middle.X,  branch.Middle.Y,
            branch.End.X,     branch.End.Y,
         };

         // Draw with cardinal spline
         writeableBmp.DrawCurve(pts, Tension, this.Color);

         foreach (var b in branch.Branches)
         {
            Draw(writeableBmp, b);
         }
      }
示例#2
0
      private void Grow(Branch branch, int generation)
      {
         if (generation <= MaxGenerations)
         {
            if (branch.End.Y >= 0 && branch.End.Y <= MaxHeight
             && branch.End.X >= 0 && branch.End.X <= MaxWidth)
            {
               // Grow it
               branch.Grow();

               // Branch?
               foreach (var bp in BranchPoints)
               {
                  if (!branchesPerGen.ContainsKey(generation))
                  {
                     branchesPerGen.Add(generation, 0);
                  }
                  if (branchesPerGen[generation] < MaxBranchesPerGeneration)
                  {
                     if (branch.Life >= bp.Time && branch.Life <= bp.Time + branch.GrowthRate)
                     {
                        // Length and angle of the branch
                        var branchLen = rand.Next(BranchLenMin, BranchLenMax);
                        branchLen -= (int)(branchLen * 0.01f * generation);
                        // In radians
                        var angle = rand.Next(bp.Angle - BranchAngleVariance, bp.Angle + BranchAngleVariance) * 0.017453292519943295769236907684886;

                        // Desired end of new branch
                        var endTarget = new Vector(branch.End.X + ((int)(Math.Sin(angle) * branchLen) * Scale.X),
                                                   branch.End.Y + ((int)(Math.Cos(angle) * branchLen) * Scale.Y));

                        // Desired middle point
                        angle -= Math.Sign(bp.Angle) * BendingFactor;
                        var middleTarget = new Vector(endTarget.X - ((int)(Math.Sin(angle) * (branchLen >> 1)) * Scale.X),
                                                      endTarget.Y - ((int)(Math.Cos(angle) * (branchLen >> 1)) * Scale.Y));

                        // Add new branch
                        branch.Branches.Add(new Branch(branch.End, middleTarget, endTarget, GetRandomGrowthRate()));
                        branchesPerGen[generation]++;
                     }
                  }
               }
            }

            // Grow the child branches
            foreach (var b in branch.Branches)
            {
               Grow(b, generation+1);
            }
         }
      }