Thanks everyone. I have annotated and optimised the code. It still draws the same graphics, but it should be a tiny bit faster and easier to understand for anyone wanting to know how it works.
If there's something I haven't annotated clearly, please let me know and I'll explain it here.