I tried building a tool that could draw using commands like Python Turtle module
Friday, April 28, 2023.
Earlier this week I was doing some work on a tool I'm currently building in stealth (no worries, checkout Tinker Quest).
I thought of implementing a mini drawing tool that works programmatically. The idea was to have something like Python's Turtle module but without much functionality for a start. Honestly I struggled a little, but I eventually figured the math in order to do this sort of thing.
The aim of this post is to share with you how it works and how I achieved this.
The application has a mini command language that directs the tool where to plot the points on a virtual coordinate system and then link them.
Here is an example
M 200 200|R 90|F 100|R 90|F 100|R 90|F 100|R 90|F 100|
As you can see it is pretty simple. Briefly:
It stars off with a point vector,
That basically means that if we run the above command, these will happen:
We end up with a square.
The accumulated rotations are kept in memory though. So, every rotation is added to a variable accAngle
and when the above command has been completely executed, the value of accAngle
will be 360.
But how is the new point calculated on every iteration? That's where trigonometry comes in.
Say we have a triangle like the one below. We want to move a certain distance
Considering our current point,
That means to get the new point, we will have