Trees

Trees


I learnt about recursion.  There are lots of examples of drawing trees using recursion and Python Turtle.  I wanted to try these and also see if I can do something different that wasn't done before.

Basic Trees

Here is an output of a basic tree with code adapted from here, which draws a simple tree with random lengths and angles.

Basic tree
Basic tree (seed 26441)

Here is the code to draw these basic trees:

Note: You can run, edit and make your own changes to all the code samples in this post and try them out (thanks to trinket).  Each run produces a different random output in all the examples below.  You can use the seed mentioned in the image captions to reproduce the exact same output.


Curved Trees

To me these trees looked a bit unnatural as they are made entirely of straight lines.  I wanted to try and see if we could draw these with curved instead of straight lines to make them look a bit more natural.

To connect two straight lines with a curve, I used the method shown below:

Curved line method
Curved line method

This is an example of how a tree looks when it is drawn with curved lines:

Curved tree
Curved tree (seed 64205)

Here is the code to draw the same trees shown before with curved lines:


Here is an image comparing trees with straight lines (left), a bit curved (center) and more curved (right).  The curve factor used to generate these variants are shown under the image below.
 
Curve comparisons
Curved tree comparison (original, 0.25, 0.5)

Hollow Trees

I haven't seen any examples of trees which are hollow (or double lined) in Python Turtle, similar to pencil drawn trees.  So, I wondered if we could draw these and also shade them if possible, just like in pencil shading.

This is what I had in mind:
Expected hollow tree
Expected hollow tree

To draw a hollow branch, we have to draw three separate curves (c1, c2, c3 as shown in the image below).  The beginning and end points are at right angles to each other.  Recursion takes care of repeating these inside.
Hollow tree method
Hollow tree method

Once I could draw this, this was the output that I got:

Hollow tree
Hollow tree (seed 61327)

Here is the code to draw these hollow trees:


Shaded Trees

Now that we can draw hollow trees with curved lines, I wanted to shade these using a hatching technique used in pencil shading.

Here is an output that I got when I drew a shaded tree:

Shaded tree
Shaded tree (seed 65094)

Here is the code to draw these shaded trees:

    

Variants

We can also change the Turtle pen widths according to the recursion level.  Here is a comparison to the previous ones on how they look:

Shading comparison
Shading comparison (seed 37380)

Here is an output with varying pen widths:

Shaded variant
Shaded variant (seed 16605)
 
Here is the code to draw this variant:


Summary

In summary, we can draw lots of different types of trees using Python Turtle which look nice and natural.

All the code in this post is available on GitHub.

- Pardhav Maradani

Popular posts from this blog

Interactive Mandalas

Auto Generated Mandalas

Bézier Curve Animations