Browse Source

Lab 3

master
Lev 3 years ago
parent
commit
dd547ffc8e
  1. 9
      turtle_2/task_1.py
  2. 48
      turtle_2/task_2.py

9
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))

48
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)
Loading…
Cancel
Save