Skip to content

Instantly share code, notes, and snippets.

@paniq
Last active February 16, 2024 08:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paniq/e5d662e8a9b79a129afea811212c7ba4 to your computer and use it in GitHub Desktop.
Save paniq/e5d662e8a9b79a129afea811212c7ba4 to your computer and use it in GitHub Desktop.
Squarify
# Squarify
# subdivide a rectangle into squares whose sum have the same area using
# the identity:
#
# w * h = min(w, h) ** 2 + (max(w,h) - min(w,h)) * min(w, h)
W,H = 1920, 1080
w,h = W, H
lengths = []
while True:
if w < h:
lengths.append(w)
h -= w
elif h < w:
lengths.append(h)
w -= h
else:
lengths.append(min(w, h))
break
# [1080, 840, 240, 240, 240, 120, 120]
print(lengths)
assert sum([x*x for x in lengths]) == W * H
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment