53 lines
2.0 KiB
Python
53 lines
2.0 KiB
Python
import matplotlib.pyplot as plt
|
|
import matplotlib.patches as patches
|
|
|
|
def draw_trapezoid(a, b, h):
|
|
fig, ax = plt.subplots()
|
|
|
|
# Рисуем трапецию
|
|
trapezoid = patches.Polygon([(0, 0), (a, 0), (b, h), (0, h)], closed=True, fill=None, edgecolor='r')
|
|
ax.add_patch(trapezoid)
|
|
|
|
# Разбиваем трапецию на квадраты
|
|
split_trapezoid(a, b, h, ax, 0, 0)
|
|
|
|
# Устанавливаем пределы осей
|
|
ax.set_xlim(-1, max(a, b) + 1)
|
|
ax.set_ylim(-1, h + 1)
|
|
ax.set_aspect('equal')
|
|
|
|
plt.show()
|
|
|
|
def split_trapezoid(a, b, h, ax, x_offset, y_offset):
|
|
if a <= 0 or b <= 0 or h <= 0:
|
|
return
|
|
|
|
# Найдите самый большой квадрат, который можно вписать в трапецию
|
|
largest_square_side = min(a, b, h)
|
|
|
|
# Поместите квадрат в трапецию
|
|
place_square(largest_square_side, x_offset, y_offset, ax)
|
|
|
|
# Разделите оставшуюся часть трапеции
|
|
remaining_a = a - largest_square_side
|
|
remaining_b = b - largest_square_side
|
|
remaining_h = h - largest_square_side
|
|
|
|
# Повторите процесс для новых трапеций
|
|
if remaining_a > 0 and remaining_h > 0:
|
|
split_trapezoid(remaining_a, b, remaining_h, ax, x_offset + largest_square_side, y_offset)
|
|
if remaining_b > 0 and remaining_h > 0:
|
|
split_trapezoid(a, remaining_b, remaining_h, ax, x_offset, y_offset + largest_square_side)
|
|
|
|
def place_square(side, x, y, ax):
|
|
# Функция для помещения квадрата в трапецию
|
|
square = patches.Rectangle((x, y), side, side, linewidth=1, edgecolor='b', facecolor='none')
|
|
ax.add_patch(square)
|
|
|
|
# Пример использования
|
|
a = 10 # Длина верхнего основания трапеции
|
|
b = 20 # Длина нижнего основания трапеции
|
|
h = 5 # Высота трапеции
|
|
|
|
draw_trapezoid(a, b, h)
|