generated from mwc/lab_turtle
I had a lot of fun with this lab. Because I have experience with math and writing code, I was able to make a fun drawing without much stress.
29 lines
652 B
Python
29 lines
652 B
Python
from turtle import (
|
|
right,
|
|
color,
|
|
goto,
|
|
pensize
|
|
)
|
|
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)
|
|
for i in range(segments + 1):
|
|
goto(coords[i])
|
|
right(90)
|
|
input()
|