Files
lab_turtle/drawing.py
2025-09-03 10:20:50 -04:00

34 lines
724 B
Python

from turtle import (
right,
color,
goto,
pensize,
begin_fill,
end_fill,
mainloop
)
from math import sin, cos, tau, pi
def cardioid(theta, size=100):
return size * (1 - sin(theta))
def polar_to_cartesian(coord: tuple[float, float]):
r, theta = coord
return (r * cos(theta), r * sin(theta))
def get_segmented_cardioid(segments = 16):
angles = [pi / 2 + i * (tau/ segments) for i in range(segments + 1)]
radii = map(cardioid, angles)
return list(map(polar_to_cartesian, zip(radii, angles)))
segments = 50
coords = get_segmented_cardioid(segments)
color(1, 0, 0)
pensize(5)
begin_fill()
for i in range(segments + 1):
goto(coords[i])
right(90)
end_fill()
mainloop()