I am a new coder and want to write some code to plot various rectangles in matplotlib, taking the animation from this link to demonstrate continued fractions. Namely, I am trying to write a function that takes the width and height of a rectangle and plots the rectangle along with lines to demonstrate this euclidean tiling process (starting with squares equal in side length to the width of the original rectangle, and then doing the same process recursively with the smaller rectangle that's left over).
I tried writing the following code:
import matplotlib.pyplot as plt
def draw_squares_on_rectangle(width, height, ax=None, leftover_x=0, leftover_y=0, leftover_width=None, leftover_height=None):
if ax is None:
fig, ax = plt.subplots()
ax.set_aspect('equal', adjustable='box')
plt.xlim([0, width])
plt.ylim([0, height])
if leftover_width is None:
leftover_width = width
if leftover_height is None:
leftover_height = height
square_size = min(width, height)
if square_size >= 1:
x = 0
y = 0
while x + square_size <= width and y + square_size <= height:
rect = plt.Rectangle((x, y), square_size, square_size, fill=False)
ax.add_patch(rect)
if x + square_size == width:
x = 0
y += square_size
else:
x += square_size
leftover_width = width - x
leftover_height = height - y
if leftover_width > 0 and leftover_height > 0:
if leftover_width > leftover_height:
draw_squares_on_rectangle(leftover_width, square_size, ax, leftover_x+x, leftover_y, leftover_width, square_size)
draw_squares_on_rectangle(width - leftover_width, leftover_height, ax, leftover_x, leftover_y+y, width - leftover_width, leftover_height)
else:
draw_squares_on_rectangle(square_size, leftover_height, ax, leftover_x, leftover_y+y, square_size, leftover_height)
draw_squares_on_rectangle(leftover_width, height - leftover_height, ax, leftover_x+x, leftover_y, leftover_width, height - leftover_height)
if leftover_width == leftover_height == 1:
return
if leftover_width == 1 and leftover_height > 1:
draw_squares_on_rectangle(1, leftover_height, ax, leftover_x, leftover_y, 1, leftover_height)
elif leftover_height == 1 and leftover_width > 1:
draw_squares_on_rectangle(leftover_width, 1, ax, leftover_x, leftover_y, leftover_width, 1)
if ax is None:
plt.show()
However, this seems only to perform the first 'square-tiling' step without the recursion. Can anyone help me out? Thank you
Here is an example of a possible direction you could take to solve that problem. The idea is to keep tiling the rectangle until the remaining area is smaller than a certain percentage of the original area (variable
threshin my code). This leftover area being smaller than the threshold will be your stop criteria for the recursion. The idea then is to tile the rectangle until the threshold is reach and add the patches in a list of patches. You can then design an animation withFuncAnimation(doc here), by adding each patch to the figure at each iteration. See code below: