From efc30142a62587cd13fe585c8fdf99bf243e2ece Mon Sep 17 00:00:00 2001 From: Chris Proctor Date: Mon, 30 Sep 2024 09:07:12 -0400 Subject: [PATCH] Add trigram function and test script --- test_trigram.py | 12 +++++++++++ trigram.py | 56 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 test_trigram.py create mode 100644 trigram.py diff --git a/test_trigram.py b/test_trigram.py new file mode 100644 index 0000000..a9be059 --- /dev/null +++ b/test_trigram.py @@ -0,0 +1,12 @@ +from turtle import left +from trigram import ( + draw_trigram, + 건, + 곤, + 리, + 감, +) + +left(90) +draw_trigram(리, 200) +input() diff --git a/trigram.py b/trigram.py new file mode 100644 index 0000000..ae4d1df --- /dev/null +++ b/trigram.py @@ -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()