Files
problemset_typeface/typeface.py
tgaeta e67f4ad19c Used Bezier curves to make fancy typeset.
This assignment was fun to do. The implementation was way easier than I
thought it would be. I know I'm supposed to make multiple commits, but I
really only have 1 implementation. I got the constants by messing around
in Desmos.
2025-09-17 14:22:50 -04:00

142 lines
2.9 KiB
Python

# typeface.py
# By Chris Proctor and Tristan Gaeta
# Contains one function for each letter in the English alphabet.
# Each function should draw its letter at a scale of `unit`, and then
# return back to the same position and heading where it started.
# Note: The `sqrt` function from math may be helpful--if you want to
# move the turtle along the diagonal of a square with side length x,
# the turtle should move a distance of sqrt(x)
from turtle import (pos, teleport, goto)
class Point(tuple):
"Class for doing element-wise arithmetic on tuples"
def __add__(self, other):
return Point((x + y for x, y in zip(self, other)))
def __sub__(self, other):
return Point((x - y for x, y in zip(self, other)))
def __rmul__(self, other):
return Point((other * x for x in self))
def __truediv__(self, other):
return Point((x / other for x in self))
def bezier(t, *points: tuple[float, ...]) -> Point:
"Recursive definition of a Bezier curve"
assert(len(points) > 0)
if len(points) == 1:
return Point(points[0])
return (1-t) * bezier(t, *points[0:-1]) + t * bezier(t, *points[1:])
def draw_bezier(unit, segments, *points: tuple[float, ...]):
"Draws bezier and returns turtle to initial position"
segments = int(unit * (segments / 40))
origin = pos()
points = tuple(origin + unit * Point(x) for x in points)
teleport(*points[0])
for i in range(0, segments + 1):
bz = bezier(i / segments, *points)
goto(bz)
teleport(*origin)
def draw_letter_a(unit):
pass
def draw_letter_b(unit):
pass
def draw_letter_c(unit):
draw_bezier(unit, 160,
(7, 7),
(0.5, 11.88),
(0.5, -3.88),
(7, 1))
def draw_letter_d(unit):
pass
def draw_letter_e(unit):
pass
def draw_letter_f(unit):
pass
def draw_letter_g(unit):
pass
def draw_letter_h(unit):
pass
def draw_letter_i(unit):
pass
def draw_letter_j(unit):
pass
def draw_letter_k(unit):
pass
def draw_letter_l(unit):
draw_bezier(unit, 100,
(1.46, 8),
(5.27, -6.66),
(-5.94, 4.27),
(7, 0))
def draw_letter_m(unit):
pass
def draw_letter_n(unit):
pass
def draw_letter_o(unit):
pass
def draw_letter_p(unit):
pass
def draw_letter_q(unit):
pass
def draw_letter_r(unit):
pass
def draw_letter_s(unit):
pass
def draw_letter_t(unit):
pass
def draw_letter_u(unit):
draw_bezier(unit, 100,
(0.5, 8),
(0, -2.5),
(8, -2.5),
(7, 8))
def draw_letter_v(unit):
draw_bezier(unit, 80,
(1, 8),
(4, -0.33),
(3.44, -4.33),
(7, 8))
def draw_letter_w(unit):
pass
def draw_letter_x(unit):
pass
def draw_letter_y(unit):
pass
def draw_letter_z(unit):
pass