Add trigram function and test script

This commit is contained in:
Chris Proctor 2024-09-30 09:07:12 -04:00
parent cd13f96603
commit efc30142a6
2 changed files with 68 additions and 0 deletions

12
test_trigram.py Normal file
View File

@ -0,0 +1,12 @@
from turtle import left
from trigram import (
draw_trigram,
,
,
,
,
)
left(90)
draw_trigram(, 200)
input()

56
trigram.py Normal file
View File

@ -0,0 +1,56 @@
from turtle import *
= [True, True, True]
= [False, False, False]
= [True, False, True]
= [False, True, False]
def draw_trigram(pattern, width):
"""Draws a trigram from the Korean flag.
The starting and ending point is the center of the bottom of
the trigram, with the turtle facing up.
The pattern should be a list of True/False values, indicating
whether each bar should be solid, starting from the top.
"""
unit = width/12
for bar_type in pattern:
draw_trigram_bar(bar_type, width)
fly(unit * 3)
fly(unit*-9)
def draw_trigram_bar(is_solid, width):
"""Draws a single bar from a trigram, starting and ending at the center
of the bottom of the bar.
"""
unit = width/12
right(90)
fly(unit*-6)
if is_solid:
draw_black_rectangle(unit*12, unit*2)
fly(width/2)
else:
draw_black_rectangle(unit*5.5, unit*2)
fly(unit*6.5)
draw_black_rectangle(unit*5.5, unit*2)
fly(unit*-0.5)
left(90)
def draw_black_rectangle(width, height):
"""Draws a filled black rectangle, starting and ending at the bottom
left corner and turning to the left.
"""
color('black')
fillcolor('black')
begin_fill()
for half in range(2):
forward(width)
left(90)
forward(height)
left(90)
end_fill()
def fly(steps):
"Move without drawing"
penup()
forward(steps)
pendown()