pentagone.py

Created by schraf

Created on September 27, 2023

1.64 KB


from math import *
from turtle import *

def dist(a, b):
 return sqrt((a[0] - b[0]) ** 2 + (a[1] - b[1]) ** 2)

def angle(a, b):
 return degrees(atan2(b[1] - a[1], b[0] - a[0]))

def point(a):
 penup()
 goto(a)
 pendown()
 pensize(4)
 goto(position())
 pensize(1)

def segment(a, b, coul, dash=True):
 color(coul)
 pensize(1)
 r = dist(a, b)
 penup()
 point(a)
 setheading(angle(a, b))
 for i in range(r):
  penup() if i % 5 and dash else pendown()
  fd(1)
 point(b)

def arc(a, b, c, w, coul):
 color(coul)
 pensize(w)
 bc = dist(b, c)
 ac = dist(a, c)
 ab = dist(a, b)
 u = angle(a, b)
 t = degrees(acos((ac * ac + ab * ab - bc * bc) / (2 * ac * ab)))
 penup()
 goto(b)
 setheading(u + 90)
 pendown()
 circle(int(ab), t)

def avance(a, b, d):
 penup()
 goto(a)
 setheading(angle(a, b))
 fd(d)
 pendown()
 pensize(4)
 goto(position())
 pensize(1)

def cercle(a, b, r, coul):
 color(coul)
 avance(a, b, r)
 b = position()
 segment(a, b, coul)
 p = position()
 point(p)
 setheading(heading() + 90)
 circle(int(r))

speed(1)
cercle((0, 0), (100, 0), 100, "grey")
segment((100, 0), (-100, 0), "grey")
cercle((-100, 0), (0, 0), 100, "purple")
a, b = (-50, sqrt(3) * 50), (-50, -sqrt(3) * 50)
segment(a, b, "blue")
point((-50, 0))
segment((0, 100), (0, -100), "blue")
point((0, 0))
setheading(90)
circle(50)
cercle((0, -100), (-50, 0), (sqrt(5) + 1) * 50, "green")
cercle((0, -100), (-50, 0), (sqrt(5) - 1) * 50, "green")
p = pi / 2
for i in range(5):
 cs1, ss1 = 100 * cos(2 * i * pi / 5 + p), 100 * sin(2 * i * pi / 5 + p)
 cs2, ss2 = 100 * cos(2 * (i + 1) * pi / 5 + p), 100 * sin(2 * (i + 1) * pi / 5 + p)
 segment((cs1, ss1), (cs2, ss2), "red", False)
hideturtle()