Created
July 23, 2023 17:38
-
-
Save oxalica/6df229266ccd9603b88e491a2041733a to your computer and use it in GitHub Desktop.
polycircles.py
This file contains hidden or 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
import math | |
import cmath | |
def main(): | |
end = 500 | |
size = 800.0 | |
r = size / 2 / 9 | |
dash = 5 | |
limit = r * 8.7 | |
center = (size / 2.0) * (1 + 1j) | |
fmt = lambda w: f'fill="none" stroke="black" stroke-width="{w}"' | |
fmt_limit = f'fill="none" stroke="green" stroke-width="1" stroke-dasharray="{dash} {dash}"' | |
yield f'<svg version="1.1" width="{size}" height="{size}" xmlns="http://www.w3.org/2000/svg">' | |
yield '<rect x="0" y="0" width="100%" height="100%" fill="white" />' | |
yield f'<polyline points="{center.real} {center.imag - r} {center.real} {center.imag + r}" {fmt(1)}/>' | |
yield f'<circle cx="{center.real}" cy="{center.imag}" r="{r}" {fmt(1)}/>' | |
for n in range(3, end): | |
r /= math.cos(math.pi / n) | |
w = 1 / (1 + n ** 1.5 / 100) | |
pts = [center + cmath.rect(r, math.tau * i / n - math.pi / 2) for i in range(n)] | |
pts = ' '.join(str(p) for pt in pts for p in (pt.real, pt.imag)) | |
yield f'<polygon points="{pts}" {fmt(w)}/>' | |
yield f'<circle cx="{center.real}" cy="{center.imag}" r="{r}" {fmt(w)}/>' | |
yield f'<circle cx="{center.real}" cy="{center.imag}" r="{limit}" {fmt_limit}/>' | |
yield '</svg>' | |
print('\n'.join(main())) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment