From dd547ffc8e18046c6e3b111434d78bb415fd7126 Mon Sep 17 00:00:00 2001 From: ennucore Date: Mon, 20 Sep 2021 09:55:06 +0300 Subject: [PATCH] Lab 3 --- turtle_2/task_1.py | 9 +++++++++ turtle_2/task_2.py | 48 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 turtle_2/task_1.py create mode 100644 turtle_2/task_2.py diff --git a/turtle_2/task_1.py b/turtle_2/task_1.py new file mode 100644 index 0000000..af6c7b4 --- /dev/null +++ b/turtle_2/task_1.py @@ -0,0 +1,9 @@ +from random import randint, random, uniform +import turtle as t + + +t.shape('turtle') +while True: + t.right(uniform(-120, 120)) + t.forward(uniform(0, 50)) + diff --git a/turtle_2/task_2.py b/turtle_2/task_2.py new file mode 100644 index 0000000..aa1da69 --- /dev/null +++ b/turtle_2/task_2.py @@ -0,0 +1,48 @@ +import turtle as t +import typing +import math + + +DATA: typing.Dict[str, typing.List[typing.Tuple[int, int]]] = { + '0': [(0, 0), (1, 0), (1, 1), (1, 2), (0, 2), (0, 1), (0, 0)], + '1': [(0, 1), (1, 0), (1, 1), (1, 2)], + '2': [(0, 0), (1, 0), (1, 1), (0, 2), (1, 2)], + '3': [(0, 0), (1, 0), (0, 1), (1, 1), (0, 2)], + '4': [(0, 0), (0, 1), (1, 1), (1, 0), (1, 2)], + '5': [(1, 0), (0, 0), (0, 1), (1, 1), (1, 2), (0, 2)], + '6': [(1, 0), (0, 1), (1, 1), (1, 2), (0, 2), (0, 1)], + '7': [(0, 0), (1, 0), (0, 1), (0, 2)], + '8': [(0, 0), (1, 0), (1, 1), (0, 1), (0, 2), (1, 2), (1, 1), (0, 1), (0, 0)], + '9': [(0, 0), (1, 0), (1, 1), (0, 2), (1, 1), (0, 1), (0, 0)] + } +DIGIT_SIZE = 25 + + +def go_delta(delta_x: int, delta_y: int, coef=DIGIT_SIZE): + t.seth(math.degrees(math.atan2(-delta_y, delta_x))) + t.forward(math.hypot(delta_x, delta_y) * coef) + + +def write_coordinates(data: typing.List[typing.Tuple[int, int]]): + t.penup() + go_delta(*data[0]) + t.pendown() + for previous_point, next_point in zip(data, data[1:]): + go_delta(next_point[0] - previous_point[0], next_point[1] - previous_point[1]) + t.penup() + go_delta(-data[-1][0], -data[-1][1]) + + +def write_digit(num: typing.Union[int, str]): + num: str = str(num) + data: typing.List[typing.Tuple[int, int]] = DATA.get(num) + write_coordinates(data) + go_delta(1.5, 0) + + +if __name__ == '__main__': + numbers = input('Numbers: ') + t.shape('turtle') + for digit in numbers: + write_digit(digit) +