Files
project_drawing/shapes.py
ilmabura f41cffdf23 Created a static image of a penguin on an iceberg
I was struggling how to draw an oval for my penguin. I needed to look
up videos/read up how to do it. I also had to look up how to set the size of the image.
I found it really difficult to just start the penguin, but once I got the shapes
I felt more comfortable.
2025-09-29 20:57:37 -04:00

50 lines
1.5 KiB
Python

import turtle
from math import cos, sin, pi, radians
def move_to(x, y):
t.penup(); t.goto(x, y); t.pendown()
def filled_rect(x1, y1, x2, y2, fill, outline="black"):
t.pencolor(outline)
t.fillcolor(fill)
move_to(x1, y1)
t.begin_fill()
t.goto(x2, y1)
t.goto(x2, y2)
t.goto(x1, y2)
t.goto(x1, y1)
t.end_fill()
def draw_oval(cx, cy, rx, ry, tilt=0, fill=None, steps=180):
"""Parametric ellipse; steps controls smoothness."""
ang = radians(tilt)
def R(x, y): # rotate (x,y) by tilt
return (x*cos(ang) - y*sin(ang), x*sin(ang) + y*cos(ang))
t.fillcolor(fill if fill else "")
move_to(cx + rx, cy) # start at angle 0
if fill:
t.begin_fill()
for i in range(1, steps + 1):
theta = 2*pi*i/steps
x, y = rx*cos(theta), ry*sin(theta)
xr, yr = R(x, y)
t.goto(cx + xr, cy + yr)
if fill:
t.end_fill()
def poly(points, fill=None):
if fill: t.fillcolor(fill); t.begin_fill()
move_to(*points[0])
for p in points[1:]:
t.goto(*p)
t.goto(*points[0])
if fill: t.end_fill()
def right_triangle(ax, ay, leg1, leg2, angle=0, fill=None):
"""Right angle at (ax, ay). leg1 along 'angle' degrees, leg2 perpendicular."""
a = radians(angle)
B = (ax + leg1*cos(a), ay + leg1*sin(a)) # along angle
C = (ax - leg2*sin(a), ay + leg2*cos(a)) # 90° to the left
poly([(ax, ay), B, C], fill=fill)