New feature: writing at an angle!

After Dr. Proctor's comment in the chat I realized my code wouldn't work
if you were trying to write at an angle so I added a rotation to the
transform. Surprisingly easy fix.
This commit is contained in:
tgaeta
2025-09-18 12:18:14 -04:00
parent e27f3424bb
commit a3c8a3f055
2 changed files with 9 additions and 2 deletions

BIN
.DS_Store vendored

Binary file not shown.

View File

@@ -9,7 +9,8 @@
# 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)
from turtle import (pos, teleport, goto, heading, setheading)
from math import sin, cos, radians
class Point(tuple):
"Class for doing element-wise arithmetic on tuples"
@@ -25,6 +26,11 @@ class Point(tuple):
def __truediv__(self, other):
return Point((x / other for x in self))
def rotate_2d_degrees(point, angle):
x, y = point
angle = radians(angle)
return Point((x * cos(angle) - y * sin(angle), x * sin(angle) + y * cos(angle)))
def bezier(t, *points: tuple[float, ...]) -> Point:
"Recursive definition of a Bezier curve"
assert(len(points) > 0)
@@ -36,7 +42,8 @@ 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)
angle = heading()
points = tuple(origin + rotate_2d_degrees(unit * Point(x), angle) for x in points)
teleport(*points[0])
for i in range(0, segments + 1):