fractal word tree in WPF C#

Brandon Boone 1 Reputation point
2021-06-13T01:03:51.157+00:00

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);
             }


        }
Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,760 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,839 questions
{count} votes

1 answer

Sort by: Most helpful
  1. DaisyTian-1203 11,621 Reputation points
    2021-06-14T06:09:39.35+00:00

    @Brandon Boone
    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.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.