generated from mwc/lab_turtle
I made a cardioid by making use of the goto func.
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.
This commit is contained in:
36
drawing.py
36
drawing.py
@@ -1,22 +1,28 @@
|
|||||||
from turtle import (
|
from turtle import (
|
||||||
forward,
|
|
||||||
back,
|
|
||||||
left,
|
|
||||||
right,
|
right,
|
||||||
penup,
|
|
||||||
pendown,
|
|
||||||
color,
|
color,
|
||||||
|
goto,
|
||||||
|
pensize
|
||||||
)
|
)
|
||||||
|
from math import sin, cos, tau, pi
|
||||||
|
|
||||||
forward(100)
|
def cardioid(theta, size=100):
|
||||||
right(360 * 2 / 5)
|
return size * (1 - sin(theta))
|
||||||
forward(100)
|
|
||||||
right(360 * 2 / 5)
|
|
||||||
forward(100)
|
|
||||||
right(360 * 2 / 5)
|
|
||||||
forward(100)
|
|
||||||
right(360 * 2 / 5)
|
|
||||||
forward(100)
|
|
||||||
right(360 * 2 / 5)
|
|
||||||
|
|
||||||
|
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()
|
input()
|
||||||
|
|||||||
Reference in New Issue
Block a user