I usually doodle it and I wanted to recreate it in Python (see output).
Following explanation is for spiral_triangle.py. Also, it is to note that I developed this logic thinking that I will be drawing the triangle as horizontally aligned (turtle starts facing East) and not at an angle (turtle not headed say at 20 degrees North of East). I modified this in spiral_triangles_using_class.py
- I created a drawing board using turtle module's
Screen
method. I changed my turtle's shape to circle usingshape
method. I moved the turtle to x=-90, y=-90 usingpenup
,goto
,pendown
method. I have usedspeed()
method with0
argument, as0
makes the turtle move fastest possible. - Then, I defined polygon parameters.
n
denotes the number of sides in a polygon.n=3
denotes that it's a triangle.polygon_turn_angle
is exterior angle of the polygon. - See illustred image Construction of Spiral triangle to get the definition of
polygon_side_length
,segment_length
variables. - Initially, triangle's first side is drawn using turtle module's
forward
method. The trick to draw the spiral triangle is, when I am drawing the second side of triangle, I am not drawing it in one go.- Firstly, I drew the
segment_length
. Here I used turtle'sposition
method to get current location's x,y co-ordinates. I stored this in the listpt
. - Then I continued and marched forward
polygon_side_length-segment_length
distance to complete the second side of triangle. - Similar actions are done when drawing the third side.
- Firstly, I drew the
- To draw the first inward line segment (
side_length_1
) of the spiral, I utilized that point whose x,y co-ordinates I stored inpt
list while drawing second side of the triangle. After drawing the third side of the triangle:- I used turtle module's
distance
method to get the distance between current location and the point on the second side of the triangle. (Since I am back to starting point after completing the triangle, current location is starting point). Then I utilizedtowards
method to get the angle towards that point. - Then I used
left
method to orient the turtle's head towards that point on the second side of triangle. - Finally I used
forward
method to march and complete the spiral's first inward line segmentside_length_1
. Step 4's actions are executed in between to get the information of point on thisside_length_1
as this will help to draw the nextside_length_1
. - I am doing this over and over in
for
loop. I usedif
conditional tobreak
out of thefor
loop if spiral'sside_length_1
becomes < 1.
- I used turtle module's
I have hardcoded the number of sides in polygon n=3
for triangle. This can be modified and the code will work for any polygon.