Wednesday 31 October 2018

Making Some Trees

I like many other types of programmer enjoy 'hacking'. Hacking is a process that I like to think of as a quick iterative process whereby code is written swiftly to achieve a proof-of-concept, quite literally hacking things together until it works, without worrying about UML diagrams, unit tests, or other programming 'jargon'.

This blog post details my first attempt at generating trees algorithmically in 3D using open Frameworks.

The first thing I tried was simply to stack cone shapes on top of one another, with a random width and height. I used the constructor to simply stick 10 cone primitives into a vector, which then gets drawn inside our draw() function.




The first improvement was to change the shape primitive to be cylinder instead of a cone and join each cylinder together.  Also reducing the width of each section as the tree gets taller.




Trees in practice don't grow straight, so another thing I tried was applying a random amount of rotation to each section, 5 degrees in each dimension (X, Y, Z).


My idea soon became a lot more challenging, as joining up cylinders in 3D and rotating them requires a lot more maths that my naiveness first envisioned. Also sometimes things don't go to plan but still produce amusing effects (see below).




Firstly I went back to 2D, which is easier to understand. Starting at a point, generate a random length and angle, workout the end point of this line by using the point on a circle's circuframance formula and you end up with this.


We can now apply this to open frameworks, but posting a cylinder on the midpoint of each line section. Starting to look a little more tree trunk like.




And another with two trunks!





Time to add somre branches.












No comments:

Post a Comment