Last active
February 16, 2024 08:46
-
-
Save paniq/e5d662e8a9b79a129afea811212c7ba4 to your computer and use it in GitHub Desktop.
Squarify
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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