question

BrandonBoone-5778 avatar image
0 Votes"
BrandonBoone-5778 asked DaisyTian-1203 commented

fractal word tree in WPF C#

I am trying to code a function that takes in a string of words and turns it in to a fractal word tree . My tree look just weird and I do not how to turn a string into a shape

here is what I have :

 public Shape PrintTree(Brush customColor)
         {
    
             Path treePath = new Path();
           //  treePath.Fill = customColor;
             treePath.Stroke = Brushes.Black;
             treePath.StrokeThickness = 5;
    
             PathFigure pf = new PathFigure();
          //  
             pf.IsClosed = false;
    
             MakeTree( pf);
             // PreOrderTraversing(_root, pf);
               
                
             PathGeometry pg = new PathGeometry();
             pg.Figures.Add(pf);
             treePath.Data = pg;
    
             return treePath;
         }
    
    
         public void MakeTree(PathFigure pf)
         {
             pf.StartPoint = new Point(0, 0);
                pf.Segments.Add(new LineSegment(new Point(0, 10), true));
             double direction =  Math.PI * 0.5D;
             double length = 100;
             int level =1;
    
             double aScale = 0.9;
             double bScale = 0.5;
                 Insert(pf,  0, 10, direction,45, 45 ,level, length, aScale, bScale);
    
         }
    
    
    
    
          void Insert(PathFigure pf, double x, double y, double direction, double angleA, double angleB ,  int level, double length, double aScale, double bScale)
          {
              if (length < 0.1)
                  return;
    
              x += length * Math.Cos(direction);
              y += length * Math.Sin(direction);
              LineSegment line = new LineSegment(new Point(x, y), true);
                  pf.Segments.Add(line);
    
              if (level > 0)
              {
                     Insert(pf, x, y, direction + angleA, angleA, angleB, level - 1, length * aScale , aScale, bScale);
                   Insert(pf, x, y, direction + angleB, angleA, angleB, level - 1, length * bScale, aScale, bScale);
              }
                 
               
         }


dotnet-csharpwindows-wpf
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@BrandonBoone-5778
Is there any update for your issue? Did my answer give you any help?

0 Votes 0 ·

1 Answer

DaisyTian-1203 avatar image
0 Votes"
DaisyTian-1203 answered

@BrandonBoone-5778
I do some updates for your code as below:

  PathSegmentCollection bbb = new PathSegmentCollection();
         PathFigure pf = new PathFigure();
         int levelNew;
         double oldX = 0, oldY = 0;
         double newX = 0, newY = 0;
    
         public Shape PrintTree()
         {
             Path treePath = new Path();
             treePath.Stroke = Brushes.Black;
             treePath.StrokeThickness = 5;
             MakeTree();
    
    
             PathGeometry pg = new PathGeometry()
             {
                 Figures = new PathFigureCollection()
                 {
                     new PathFigure()
                     {
                         IsClosed = false,
                         StartPoint = new Point(50,0),
                         Segments = pf.Segments
                     },
                     new PathFigure()
                     {
                         IsClosed = false,
                         StartPoint = new Point(oldX,oldY),
                         Segments = bbb
                     }
                 }
             };
             treePath.Data = pg;
    
             return treePath;
         }
    
         public void MakeTree()
         {
             double direction = Math.PI * 0.5D;
             double length = 100;
             int level = 1;
    
             double aScale = 0.9;
             double bScale = 0.5;
             Insert(pf, 50, 50, direction, 45, 45, level, length, aScale, bScale);
    
         }
    
           
         void Insert(PathFigure pf, double x, double y, double direction, double angleA, double angleB, int level, double length, double aScale, double bScale)
         {
             if (length < 0.1)
                 return;
    
             x += length * Math.Cos(direction);
             y += length * Math.Sin(direction);
    
             levelNew = level;
             LineSegment line = new LineSegment(new Point(x, y), true);
    
             pf.Segments.Add(line);
    
             if (level > 0)
             {
                 Insert(pf, x, y, direction + angleA, angleA, angleB, level-1, length * aScale, aScale, bScale);
                 if (levelNew >= 0)
                 {
                     oldX = x; 
                     oldY = y;
                     newX = length * Math.Cos(direction + angleB);
                     newY = length * Math.Sin(direction + angleB);
                     bbb.Add(new LineSegment(new Point(newX, newY), true));
                     levelNew--;
                 }
                    
             }
         }

The result picture is:
105271-capture.png


If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


capture.png (3.6 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.