How to create a fractal tree?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
3
down vote

favorite
1












I want to create a fractal tree, using translation and rotation. Code below is working for one level, but when I try loop, it is not giving output. Code is given below:



 threshold = 4;
L = 100;
While[
L > threshold,
line = Line[0, 0, 0, -L];
Graphics[
line,
Scale[#,
2/3, 0,
0] & /@ (Rotate[
Translate[line, 0, L], # Degree, 0, 0] & /@ -30,
30)]
L = L*0.67;
];






share|improve this question
















  • 1




    Related: 154213.
    – Henrik Schumacher
    Sep 2 at 12:09















up vote
3
down vote

favorite
1












I want to create a fractal tree, using translation and rotation. Code below is working for one level, but when I try loop, it is not giving output. Code is given below:



 threshold = 4;
L = 100;
While[
L > threshold,
line = Line[0, 0, 0, -L];
Graphics[
line,
Scale[#,
2/3, 0,
0] & /@ (Rotate[
Translate[line, 0, L], # Degree, 0, 0] & /@ -30,
30)]
L = L*0.67;
];






share|improve this question
















  • 1




    Related: 154213.
    – Henrik Schumacher
    Sep 2 at 12:09













up vote
3
down vote

favorite
1









up vote
3
down vote

favorite
1






1





I want to create a fractal tree, using translation and rotation. Code below is working for one level, but when I try loop, it is not giving output. Code is given below:



 threshold = 4;
L = 100;
While[
L > threshold,
line = Line[0, 0, 0, -L];
Graphics[
line,
Scale[#,
2/3, 0,
0] & /@ (Rotate[
Translate[line, 0, L], # Degree, 0, 0] & /@ -30,
30)]
L = L*0.67;
];






share|improve this question












I want to create a fractal tree, using translation and rotation. Code below is working for one level, but when I try loop, it is not giving output. Code is given below:



 threshold = 4;
L = 100;
While[
L > threshold,
line = Line[0, 0, 0, -L];
Graphics[
line,
Scale[#,
2/3, 0,
0] & /@ (Rotate[
Translate[line, 0, L], # Degree, 0, 0] & /@ -30,
30)]
L = L*0.67;
];








share|improve this question











share|improve this question




share|improve this question










asked Sep 2 at 11:20









BiSarfraz

625




625







  • 1




    Related: 154213.
    – Henrik Schumacher
    Sep 2 at 12:09













  • 1




    Related: 154213.
    – Henrik Schumacher
    Sep 2 at 12:09








1




1




Related: 154213.
– Henrik Schumacher
Sep 2 at 12:09





Related: 154213.
– Henrik Schumacher
Sep 2 at 12:09











3 Answers
3






active

oldest

votes

















up vote
5
down vote



accepted










ClearAll[next];

next[x1_, y1_, x2_, y2_] =
(
x1, y1, x2, y2 //
TranslationTransform[x2 - x1, y2 - y1] //
RotationTransform[# Pi/6, x2, y2] //
ScalingTransform[2/3 1, 1, x2, y2]
) & /@ -1, 1;

list = NestList[Join @@ next /@ # &, N@0, 0, 0, 1, 5];

Graphics[Line /@ list]


enter image description here






share|improve this answer






















  • Nice, Thank you so very much @chyang.
    – BiSarfraz
    Sep 2 at 14:33










  • @BiSarfraz You are welcome.
    – chyanog
    Sep 3 at 6:25

















up vote
6
down vote














I am not only new in Mathematica, but also new in Maths. I need a very simple approach




(This answer attempts to be a "reference answer" with relevant links.)



This has been discussed extensively in MSE and Mathematica blogs and demonstrations.




  • MSE:



    • How can I improve my code for drawing a tree?


    • L-System in Mathematica




  • Mathematica Journal:



    • A New Method of
      Constructing Fractals and
      Other Graphics



  • Demonstrations:



    • Fractal Trees


    • Radial FractalTree


    • FibonacciTree




  • Blogs:



    • Adventures into the Mathematical Forest of Fractal Trees.


    • Fractals at 4am







share|improve this answer






















  • Thank you so much.
    – BiSarfraz
    Sep 2 at 14:25










  • @BiSarfraz You are welcome, good luck!
    – Anton Antonov
    Sep 2 at 14:28

















up vote
3
down vote













The main issue with your code is that it's lacking a way to combine the different levels of the tree. Graphics are not automatically printed, unless they are the final result of a computation.



The following code should get you started:



tree[L_, th_] /; L > th := With[
line = Line[0, 0, 0, -L],

line,
Scale[#, 2/3, 0, 0] & /@ (Rotate[Translate[tree[0.67 L, th], 0, L], # Degree, 0, 0] & /@ -30, 30)
]
tree[__] :=


tree[100, 10] // Graphics


Mathematica graphics



The key ingredients are:



  • Recursion of the tree function to combine the different parts. The termination condition is implemented using Condition (/;)


  • Rotate,Scale,Transform can be arbitrarily nested, allowing for a clean specification of the nested levels

As you can see, the result is not correct yet (the lines are too short), but you should be able to easily fix that.






share|improve this answer




















  • Thank you so much for making things easy.
    – BiSarfraz
    Sep 2 at 14:28






  • 1




    I have fixed it: line, Scale[#, 2/3, 0, 0] & /@ (Rotate[Translate[tree[0.67 L, th], 0, 0.67L], # Degree, 0, 0] & /@ -30, 30)} ]
    – BiSarfraz
    Sep 3 at 10:12










Your Answer




StackExchange.ifUsing("editor", function ()
return StackExchange.using("mathjaxEditing", function ()
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
);
);
, "mathjax-editing");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "387"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f181095%2fhow-to-create-a-fractal-tree%23new-answer', 'question_page');

);

Post as a guest






























3 Answers
3






active

oldest

votes








3 Answers
3






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
5
down vote



accepted










ClearAll[next];

next[x1_, y1_, x2_, y2_] =
(
x1, y1, x2, y2 //
TranslationTransform[x2 - x1, y2 - y1] //
RotationTransform[# Pi/6, x2, y2] //
ScalingTransform[2/3 1, 1, x2, y2]
) & /@ -1, 1;

list = NestList[Join @@ next /@ # &, N@0, 0, 0, 1, 5];

Graphics[Line /@ list]


enter image description here






share|improve this answer






















  • Nice, Thank you so very much @chyang.
    – BiSarfraz
    Sep 2 at 14:33










  • @BiSarfraz You are welcome.
    – chyanog
    Sep 3 at 6:25














up vote
5
down vote



accepted










ClearAll[next];

next[x1_, y1_, x2_, y2_] =
(
x1, y1, x2, y2 //
TranslationTransform[x2 - x1, y2 - y1] //
RotationTransform[# Pi/6, x2, y2] //
ScalingTransform[2/3 1, 1, x2, y2]
) & /@ -1, 1;

list = NestList[Join @@ next /@ # &, N@0, 0, 0, 1, 5];

Graphics[Line /@ list]


enter image description here






share|improve this answer






















  • Nice, Thank you so very much @chyang.
    – BiSarfraz
    Sep 2 at 14:33










  • @BiSarfraz You are welcome.
    – chyanog
    Sep 3 at 6:25












up vote
5
down vote



accepted







up vote
5
down vote



accepted






ClearAll[next];

next[x1_, y1_, x2_, y2_] =
(
x1, y1, x2, y2 //
TranslationTransform[x2 - x1, y2 - y1] //
RotationTransform[# Pi/6, x2, y2] //
ScalingTransform[2/3 1, 1, x2, y2]
) & /@ -1, 1;

list = NestList[Join @@ next /@ # &, N@0, 0, 0, 1, 5];

Graphics[Line /@ list]


enter image description here






share|improve this answer














ClearAll[next];

next[x1_, y1_, x2_, y2_] =
(
x1, y1, x2, y2 //
TranslationTransform[x2 - x1, y2 - y1] //
RotationTransform[# Pi/6, x2, y2] //
ScalingTransform[2/3 1, 1, x2, y2]
) & /@ -1, 1;

list = NestList[Join @@ next /@ # &, N@0, 0, 0, 1, 5];

Graphics[Line /@ list]


enter image description here







share|improve this answer














share|improve this answer



share|improve this answer








edited Sep 3 at 6:25

























answered Sep 2 at 12:55









chyanog

6,75921544




6,75921544











  • Nice, Thank you so very much @chyang.
    – BiSarfraz
    Sep 2 at 14:33










  • @BiSarfraz You are welcome.
    – chyanog
    Sep 3 at 6:25
















  • Nice, Thank you so very much @chyang.
    – BiSarfraz
    Sep 2 at 14:33










  • @BiSarfraz You are welcome.
    – chyanog
    Sep 3 at 6:25















Nice, Thank you so very much @chyang.
– BiSarfraz
Sep 2 at 14:33




Nice, Thank you so very much @chyang.
– BiSarfraz
Sep 2 at 14:33












@BiSarfraz You are welcome.
– chyanog
Sep 3 at 6:25




@BiSarfraz You are welcome.
– chyanog
Sep 3 at 6:25










up vote
6
down vote














I am not only new in Mathematica, but also new in Maths. I need a very simple approach




(This answer attempts to be a "reference answer" with relevant links.)



This has been discussed extensively in MSE and Mathematica blogs and demonstrations.




  • MSE:



    • How can I improve my code for drawing a tree?


    • L-System in Mathematica




  • Mathematica Journal:



    • A New Method of
      Constructing Fractals and
      Other Graphics



  • Demonstrations:



    • Fractal Trees


    • Radial FractalTree


    • FibonacciTree




  • Blogs:



    • Adventures into the Mathematical Forest of Fractal Trees.


    • Fractals at 4am







share|improve this answer






















  • Thank you so much.
    – BiSarfraz
    Sep 2 at 14:25










  • @BiSarfraz You are welcome, good luck!
    – Anton Antonov
    Sep 2 at 14:28














up vote
6
down vote














I am not only new in Mathematica, but also new in Maths. I need a very simple approach




(This answer attempts to be a "reference answer" with relevant links.)



This has been discussed extensively in MSE and Mathematica blogs and demonstrations.




  • MSE:



    • How can I improve my code for drawing a tree?


    • L-System in Mathematica




  • Mathematica Journal:



    • A New Method of
      Constructing Fractals and
      Other Graphics



  • Demonstrations:



    • Fractal Trees


    • Radial FractalTree


    • FibonacciTree




  • Blogs:



    • Adventures into the Mathematical Forest of Fractal Trees.


    • Fractals at 4am







share|improve this answer






















  • Thank you so much.
    – BiSarfraz
    Sep 2 at 14:25










  • @BiSarfraz You are welcome, good luck!
    – Anton Antonov
    Sep 2 at 14:28












up vote
6
down vote










up vote
6
down vote










I am not only new in Mathematica, but also new in Maths. I need a very simple approach




(This answer attempts to be a "reference answer" with relevant links.)



This has been discussed extensively in MSE and Mathematica blogs and demonstrations.




  • MSE:



    • How can I improve my code for drawing a tree?


    • L-System in Mathematica




  • Mathematica Journal:



    • A New Method of
      Constructing Fractals and
      Other Graphics



  • Demonstrations:



    • Fractal Trees


    • Radial FractalTree


    • FibonacciTree




  • Blogs:



    • Adventures into the Mathematical Forest of Fractal Trees.


    • Fractals at 4am







share|improve this answer















I am not only new in Mathematica, but also new in Maths. I need a very simple approach




(This answer attempts to be a "reference answer" with relevant links.)



This has been discussed extensively in MSE and Mathematica blogs and demonstrations.




  • MSE:



    • How can I improve my code for drawing a tree?


    • L-System in Mathematica




  • Mathematica Journal:



    • A New Method of
      Constructing Fractals and
      Other Graphics



  • Demonstrations:



    • Fractal Trees


    • Radial FractalTree


    • FibonacciTree




  • Blogs:



    • Adventures into the Mathematical Forest of Fractal Trees.


    • Fractals at 4am








share|improve this answer














share|improve this answer



share|improve this answer








edited Sep 2 at 15:06









Szabolcs

152k13415896




152k13415896










answered Sep 2 at 14:07









Anton Antonov

21.5k164107




21.5k164107











  • Thank you so much.
    – BiSarfraz
    Sep 2 at 14:25










  • @BiSarfraz You are welcome, good luck!
    – Anton Antonov
    Sep 2 at 14:28
















  • Thank you so much.
    – BiSarfraz
    Sep 2 at 14:25










  • @BiSarfraz You are welcome, good luck!
    – Anton Antonov
    Sep 2 at 14:28















Thank you so much.
– BiSarfraz
Sep 2 at 14:25




Thank you so much.
– BiSarfraz
Sep 2 at 14:25












@BiSarfraz You are welcome, good luck!
– Anton Antonov
Sep 2 at 14:28




@BiSarfraz You are welcome, good luck!
– Anton Antonov
Sep 2 at 14:28










up vote
3
down vote













The main issue with your code is that it's lacking a way to combine the different levels of the tree. Graphics are not automatically printed, unless they are the final result of a computation.



The following code should get you started:



tree[L_, th_] /; L > th := With[
line = Line[0, 0, 0, -L],

line,
Scale[#, 2/3, 0, 0] & /@ (Rotate[Translate[tree[0.67 L, th], 0, L], # Degree, 0, 0] & /@ -30, 30)
]
tree[__] :=


tree[100, 10] // Graphics


Mathematica graphics



The key ingredients are:



  • Recursion of the tree function to combine the different parts. The termination condition is implemented using Condition (/;)


  • Rotate,Scale,Transform can be arbitrarily nested, allowing for a clean specification of the nested levels

As you can see, the result is not correct yet (the lines are too short), but you should be able to easily fix that.






share|improve this answer




















  • Thank you so much for making things easy.
    – BiSarfraz
    Sep 2 at 14:28






  • 1




    I have fixed it: line, Scale[#, 2/3, 0, 0] & /@ (Rotate[Translate[tree[0.67 L, th], 0, 0.67L], # Degree, 0, 0] & /@ -30, 30)} ]
    – BiSarfraz
    Sep 3 at 10:12














up vote
3
down vote













The main issue with your code is that it's lacking a way to combine the different levels of the tree. Graphics are not automatically printed, unless they are the final result of a computation.



The following code should get you started:



tree[L_, th_] /; L > th := With[
line = Line[0, 0, 0, -L],

line,
Scale[#, 2/3, 0, 0] & /@ (Rotate[Translate[tree[0.67 L, th], 0, L], # Degree, 0, 0] & /@ -30, 30)
]
tree[__] :=


tree[100, 10] // Graphics


Mathematica graphics



The key ingredients are:



  • Recursion of the tree function to combine the different parts. The termination condition is implemented using Condition (/;)


  • Rotate,Scale,Transform can be arbitrarily nested, allowing for a clean specification of the nested levels

As you can see, the result is not correct yet (the lines are too short), but you should be able to easily fix that.






share|improve this answer




















  • Thank you so much for making things easy.
    – BiSarfraz
    Sep 2 at 14:28






  • 1




    I have fixed it: line, Scale[#, 2/3, 0, 0] & /@ (Rotate[Translate[tree[0.67 L, th], 0, 0.67L], # Degree, 0, 0] & /@ -30, 30)} ]
    – BiSarfraz
    Sep 3 at 10:12












up vote
3
down vote










up vote
3
down vote









The main issue with your code is that it's lacking a way to combine the different levels of the tree. Graphics are not automatically printed, unless they are the final result of a computation.



The following code should get you started:



tree[L_, th_] /; L > th := With[
line = Line[0, 0, 0, -L],

line,
Scale[#, 2/3, 0, 0] & /@ (Rotate[Translate[tree[0.67 L, th], 0, L], # Degree, 0, 0] & /@ -30, 30)
]
tree[__] :=


tree[100, 10] // Graphics


Mathematica graphics



The key ingredients are:



  • Recursion of the tree function to combine the different parts. The termination condition is implemented using Condition (/;)


  • Rotate,Scale,Transform can be arbitrarily nested, allowing for a clean specification of the nested levels

As you can see, the result is not correct yet (the lines are too short), but you should be able to easily fix that.






share|improve this answer












The main issue with your code is that it's lacking a way to combine the different levels of the tree. Graphics are not automatically printed, unless they are the final result of a computation.



The following code should get you started:



tree[L_, th_] /; L > th := With[
line = Line[0, 0, 0, -L],

line,
Scale[#, 2/3, 0, 0] & /@ (Rotate[Translate[tree[0.67 L, th], 0, L], # Degree, 0, 0] & /@ -30, 30)
]
tree[__] :=


tree[100, 10] // Graphics


Mathematica graphics



The key ingredients are:



  • Recursion of the tree function to combine the different parts. The termination condition is implemented using Condition (/;)


  • Rotate,Scale,Transform can be arbitrarily nested, allowing for a clean specification of the nested levels

As you can see, the result is not correct yet (the lines are too short), but you should be able to easily fix that.







share|improve this answer












share|improve this answer



share|improve this answer










answered Sep 2 at 13:01









Lukas Lang

5,1531525




5,1531525











  • Thank you so much for making things easy.
    – BiSarfraz
    Sep 2 at 14:28






  • 1




    I have fixed it: line, Scale[#, 2/3, 0, 0] & /@ (Rotate[Translate[tree[0.67 L, th], 0, 0.67L], # Degree, 0, 0] & /@ -30, 30)} ]
    – BiSarfraz
    Sep 3 at 10:12
















  • Thank you so much for making things easy.
    – BiSarfraz
    Sep 2 at 14:28






  • 1




    I have fixed it: line, Scale[#, 2/3, 0, 0] & /@ (Rotate[Translate[tree[0.67 L, th], 0, 0.67L], # Degree, 0, 0] & /@ -30, 30)} ]
    – BiSarfraz
    Sep 3 at 10:12















Thank you so much for making things easy.
– BiSarfraz
Sep 2 at 14:28




Thank you so much for making things easy.
– BiSarfraz
Sep 2 at 14:28




1




1




I have fixed it: line, Scale[#, 2/3, 0, 0] & /@ (Rotate[Translate[tree[0.67 L, th], 0, 0.67L], # Degree, 0, 0] & /@ -30, 30)} ]
– BiSarfraz
Sep 3 at 10:12




I have fixed it: line, Scale[#, 2/3, 0, 0] & /@ (Rotate[Translate[tree[0.67 L, th], 0, 0.67L], # Degree, 0, 0] & /@ -30, 30)} ]
– BiSarfraz
Sep 3 at 10:12

















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f181095%2fhow-to-create-a-fractal-tree%23new-answer', 'question_page');

);

Post as a guest













































































Comments

Popular posts from this blog

What does second last employer means? [closed]

List of Gilmore Girls characters

Confectionery