Search on blog:

# Drawing with Turtle - Triangle

## Equilateral Triangle

To draw equilateral triangle you have to repeat three times: draw side, turn left (or right) 120 degrees.

During drawing turtle rotate totally 360 degrees and because it makes three turns so every turn has 360/3 = 120 degrees.

```import turtle

turtle.forward(300)
turtle.left(120)

turtle.forward(300)
turtle.left(120)

turtle.forward(300)
turtle.left(120)

turtle.mainloop()
```

In code above last turn left is not really needed but after last turn turtle is in the same position as before drawing. It helps to plan next figure.

It can be also easy recreated with for-loop.

```import turtle

for _ in range(3):
turtle.forward(300)
turtle.left(120)

turtle.mainloop()
```

Warning: I use for _ in ... instead of for x in ... because I don't need value x inside loop so I can skip variable. Using _ I don't have to use new variable which could accidently overwrite value in already exisitng variable (especially in long code). Variable _ is popular variable to show that we will not use this value inside loop.

As you can see during this drawing you don't have to calculate positions for edges (which would need to use sin, cos or other math). turtle let you see coordinates for tops (with turtle) using turtle.pos(). It can be useful in some situations.

```import turtle

for _ in range(3):
print(turtle.pos())
turtle.forward(300)
turtle.left(120)

turtle.mainloop()
```
```(0.00,0.00)
(300.00,0.00)
(150.00,259.81)
```

This code can be convert to function to draw many triangles - ie. with different size.

```import turtle

def equilateral_triangle(side)
for _ in range(3):
turtle.forward(side)
turtle.left(120)

equilateral_triangle(100)
equilateral_triangle(200)
equilateral_triangle(300)

turtle.mainloop()
```

It can be done also with for-loop

```import turtle

def equilateral_triangle(side)
for _ in range(3):
turtle.forward(side)
turtle.left(120)

for x in range(1, 4):
equilateral_triangle(x*100)

turtle.mainloop()
```

or we can use range(begin, end, step). We have to remember that end is not part of range so to get 300 we have to use 301 as the end

```import turtle

def equilateral_triangle(side)
for _ in range(3):
turtle.forward(side)
turtle.left(120)

for bok in range(100, 301, 100):
equilateral_triangle(side)

turtle.mainloop()
```

We can also use list (or tuple) instead range() and then we can put more random values

```import turtle

def equilateral_triangle(side)
for _ in range(3):
turtle.forward(side)
turtle.left(120)

for bok in [100, 234, 300]:
equilateral_triangle(side)

turtle.mainloop()
```

Using for we can easily change number of triangles.

---

Instead of changing size od triangle we can also rotate turtle before drawing every figure.

```import turtle

def equilateral_triangle(side)
for _ in range(3):
turtle.forward(side)
turtle.left(120)

equilateral_triangle(100)
turtle.left(120)
equilateral_triangle(100)
turtle.left(120)
equilateral_triangle(100)
turtle.left(120)

turtle.mainloop()
```

Here you can see again that code is repeating so we can use for-loop

```import turtle

def equilateral_triangle(side):
for _ in range(3):
turtle.forward(side)
turtle.left(120)

for _ in range(3):
equilateral_triangle(100)
turtle.left(120)

turtle.mainloop()
```

We can also change number of repeating and automatically calculate angles

```import turtle

def equilateral_triangle(side):
for _ in range(3):
turtle.forward(side)
turtle.left(120)

size = 100
number = 4
angle = 360/number

for _ in range(number):
equilateral_triangle(side)
turtle.left(angle)

turtle.mainloop()
```

Instead of changing anlge we can also move turtle before drawing next triangle.

```import turtle

def equilateral_triangle(side):
for _ in range(3):
turtle.forward(side)
turtle.left(120)

size = 100
number = 4

for _ in range(number):
equilateral_triangle(side)
turtle.forward(side)

turtle.mainloop()
```

We can also do both - moving and turning right

```import turtle

def equilateral_triangle(side):
for _ in range(3):
turtle.forward(side)
turtle.left(120)

size = 100
number = 4
angle = 360/number

for _ in range(number):
equilateral_triangle(side)
turtle.forward(side)
turtle.right(angle)

turtle.mainloop()
```

number = 4

number = 5

We can also turn left

```import turtle

def equilateral_triangle(side):
for _ in range(3):
turtle.forward(side)
turtle.left(120)

size =  100
number =  5
angle =  360/number

for _ in range(number):
equilateral_triangle(side)
turtle.forward(side)
turtle.left(angle)

turtle.mainloop()
```

number = 5

number = 10

After adding line to triangle we have flag

```import turtle

def equilateral_triangle(side):
for _ in range(3):
turtle.forward(side)
turtle.right(120)

def flaga(side, ):
turtle.forward(100)        # drawing line
equilateral_triangle(side) # drawing triangle
turtle.backward(100)       # move back to beginning position

number = 12
angle = 360/number

for _ in range(number):
flaga(50)
turtle.left(angle)

turtle.mainloop()
```

We can also change flag size

```import turtle

def equilateral_triangle(side):
for _ in range(3):
turtle.forward(side)
turtle.right(120)

def flaga(size, ):
turtle.forward(100)        # drawing line
equilateral_triangle(side) # drawing triangle
turtle.backward(100)       # move back to beginning position

number =  12
angle =  360/number

for x in range(number):
flaga(10 * x)
turtle.left(angle)

turtle.mainloop()
```

etc.

## Rectangular Triangle and other

To draw triangle with turtle you have to know length for all three sides and all three angles (between sides) - or rather 180-angle degrees.

```turtle.forward(bok_a)
turtle.left(180 - angle_a_b)

turtle.forward(bok_b)
turtle.left(180 - angle_b_c)

turtle.forward(bok_c)
turtle.left(180 - angle_c_a)
```

If we have rectangular triangle then we know only sides a i b (near angle with 90 degrees) and angle between a, b (90 degrees) and we can calculate side c using twierdzenia Pitagorasa ( a**2 + b**2 = c**2 which will be in Python c = math.sqrt(a**2 + b**2) ) and we can calculate angles between sides a and c and between sides b and c using arcus tangence <https://pl.wikipedia.org/wiki/Funkcje_cyklometryczne>'__) (reverse to tangence), which is avaliable as `math.atan.

a/b and b/a gives tangences, math.atan() changes tangences to angle in radians, and math.degrees() changes radians to degrees.

```import turtle
import math

a = 100
b = 150
angle_a_b = 90

c = math.sqrt(a**2 + b**2)

angle_b_c = math.degrees(math.atan(a/b))
angle_c_a = math.degrees(math.atan(b/a))

turtle.forward(a)
turtle.left(180 - angle_a_b)

turtle.forward(b)
turtle.left(180 - angle_b_c)

turtle.forward(c)
turtle.left(180 - angle_c_a)

turtle.mainloop()
```

and the same as function

```import turtle
import math

def rectangular_triangle(a, b, angle):

c = math.sqrt(a**2 + b**2)

angle_a_b = angle
angle_b_c = math.degrees(math.atan(a/b))
angle_c_a = math.degrees(math.atan(b/a))

turtle.forward(a)
turtle.left(180 - angle_a_b)

turtle.forward(b)
turtle.left(180 - angle_b_c)

turtle.forward(c)
turtle.left(180 - angle_c_a)

rectangular_triangle(100, 150)

turtle.mainloop()
```

We can skip all these calculations using turtle.position() remeber position of first top and turtle.goto() to go from last top directly to first top.

At the end we have to only rotate turtle's head into original direction - but this is not problem.

```import turtle

a = 100
b = 150
angle =  90

pos = turtle.position()  # save position of first top

turtle.forward(a)        # first side
turtle.left(180 - angle)
turtle.forward(b)        # second side

turtle.goto(pos)         # move to to first top and draw third side

turtle.right(180 - angle)  # rotate turtle's head to original direction

turtle.mainloop()
```

This method is good because it lets us draw every type of triangle: rectangular, rectangular, equilibrious, open, sharp.

```import turtle

def triangle(a, b, angle):
pos = turtle.position() # save position of first top

turtle.forward(a)
turtle.left(180 - angle)
turtle.forward(b)

turtle.goto(pos) # move to to first top and draw third side

turtle.right(180 - kat) # rotate turtle's head to original direction

triangle(100, 100, 0)   # line
triangle(100, 100, 30)
triangle(100, 100, 60)  # rectangular
triangle(100, 100, 90)  # rectangular
triangle(100, 100, 120)
triangle(100, 100, 150)
triangle(100, 100, 180) # line
triangle(100, 100, 210)
triangle(100, 100, 240)
triangle(100, 100, 270) # rectangular
triangle(100, 100, 300) # rectangular
triangle(100, 100, 330)
triangle(100, 100, 360) # the same as angle 0

triangle(-100, 100, 0)   # line
triangle(-100, 100, 30)
triangle(-100, 100, 60)  # rectangular
triangle(-100, 100, 90)  # rectangular
triangle(-100, 100, 120)
triangle(-100, 100, 150)
triangle(-100, 100, 180) # line
triangle(-100, 100, 210)
triangle(-100, 100, 240)
triangle(-100, 100, 270) # rectangular
triangle(-100, 100, 300) # rectangular
triangle(-100, 100, 330)
triangle(-100, 100, 360) # the same as angle 0

turtle.mainloop()
```

Angles from 0 to 180, side a positive.

Angles from 0 to 360, side a positive.

Angles from 0 to 360, side a positive i negative.

## Other ideas

```import turtle

# triangle neons

def triangle(a, b, angle):
pos = turtle.position()
turtle.forward(a)
turtle.left(180-angle)
turtle.forward(b)
turtle.goto(pos)

turtle.right(180-angle)

# triangle on left side
for b in range(50, 301, 50):
triangle(300, b, 60)

# move to right side
turtle.forward(300)
turtle.left(180)

# triangle on right side
# (angle with minus -60 to draw in reverse direction)
for b in range(50, 301, 50):
triangle(300, b, -60)

turtle.mainloop()
```
If you like it