I used canvas and trigonometry to calculate the vertices of the triangles. I wrote a function that would fill an empty array with the information about each triangle. I then went through the array to draw the triangles using canvas. This was a very tedious process that was extremely error-prone as you can see above. Because the function only drew one triangle with four smaller triangles within it, the function had to be called several times to achieve the Sierpinski triangle.
In retrospect, there was no need to create an array for the vertices. I could have used a recursive function to draw the triangles within the triangles until the side of the triangle reached a certain limit. It was difficult to think of rewriting from scratch after all the work/careful comments I had put into my tedious function.
I think the biggest lesson I learned writing this page as not canvas or trigonometry, but the idea of ‘deleting code’. It’s easy to add code or to copy and paste code but to simplify the code is the hard part. You can’t get attached to your code and resist changes. I guess the hard part for me is I enjoyed working on difficult math problems but even if the solution was incorrect, I could still see the work I had done. There’s something oddly satisfying about seeing a whole page of practice problems in a notebook. You feel good about the work you have done and gain confidence in your abilities.
When you have to delete your code and start from scratch, it almost feels as if you did not learn anything… which is of course not true.
“Simplicity is the ultimate sophistication.” – Leonardo da Vinci
Day 34 – Sierpinski Triangle