Лабораторные работы по Python, ЛФИ, 1 семестр
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

130 lines
2.9 KiB

import turtle as t
import math
import time
import sys
def polygon(n=4, radius=90., offset=(0, 0), alpha_offset=0, direction=1):
t.penup()
t.goto(radius * math.cos(alpha_offset) + offset[0], radius * math.sin(alpha_offset) + offset[1])
t.pendown()
for i in range(n):
angle = direction * (i + 1) * 2 * math.pi / n + alpha_offset
t.goto(radius * math.cos(angle) + offset[0], radius * math.sin(angle) + offset[1])
def star(n=5, r=160):
# step = n // 2
for i in range(n):
t.forward(r)
t.right(180 - 180 * 1 / n)
num = int(sys.argv[1] if len(sys.argv) > 1 else input('Task: '))
t.shape('turtle')
if num == 3:
polygon()
elif num == 4:
polygon(100)
elif num == 5:
for r in range(70, 160, 10):
polygon(4, r)
elif num == 9:
for n in range(3, 10):
rad = 90 / (2 * math.sin(2 * math.pi / (2 * n)))
polygon(n, rad)
elif num == 10:
n = 6
alpha = 2 * math.pi / n
r = 90
for i in range(n):
polygon(100, r, (r * math.cos(alpha * i), r * math.sin(alpha * i)), -(math.pi - alpha * i))
elif num == 11:
for n in range(5):
r = 70 + 10 * n
polygon(100, r, (r, 0), math.pi)
polygon(100, r, (-r, 0), direction=-1)
elif num == 6:
n, r = input('n: '), 90
for i in range(n):
t.goto(r * math.cos(i * 2 * math.pi / n), r * math.sin(i * 2 * math.pi / n))
t.stamp()
t.goto(0, 0)
elif num == 7:
k = 20 / (2 * math.pi)
step = 100
n = 20
for phi_ in range(0, int(n * 2 * math.pi * step)):
phi = phi_ / step
r = k * phi
t.goto(r * math.cos(phi), r * math.sin(phi))
elif num == 8:
for n in range(1, 20):
# right
t.forward(10 * n)
t.left(90)
# up
t.forward(10 * (2 * n))
t.left(90)
# left
t.forward(10 * (2 * n))
t.left(90)
# down
t.forward(10 * (n * 2 + 1))
t.left(90)
t.forward(10 * n)
elif num == 12:
t.right(90)
for _ in range(10):
t.circle(50, 180)
t.circle(10, 180)
elif num == 13:
t.fillcolor('yellow')
t.begin_fill()
t.circle(100)
t.end_fill()
t.penup()
t.goto(-50, 110)
t.pendown()
t.fillcolor('blue')
t.begin_fill()
t.circle(20)
t.end_fill()
t.penup()
t.goto(50, 110)
t.pendown()
t.fillcolor('blue')
t.begin_fill()
t.circle(20)
t.end_fill()
t.up()
t.goto(-60, 155)
t.down()
t.width(3)
t.setheading(-10)
t.forward(30)
t.up()
t.goto(60, 155)
t.down()
t.setheading(180 + 10)
t.forward(30)
t.up()
t.goto(0, 100)
t.down()
t.width(15)
t.setheading(270)
t.forward(25)
t.up()
t.goto(-40, 30)
t.down()
t.color('red')
t.seth(0)
t.forward(80)
elif num == 14:
star()
time.sleep(float(sys.argv[2]) if len(sys.argv) > 2 else 10)