-
-
Save antiface/cea8c2003ae5d9abf05811d4abe09cde to your computer and use it in GitHub Desktop.
Generates bitmap of a spiral
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 | |
| from PIL import Image | |
| def spiral(r=20): | |
| im = Image.new("RGB", (r * 2, r * 2), (255, 255, 255)) | |
| inc = 10 | |
| inside = True | |
| angle = 0 | |
| rad = 0 | |
| while inside: | |
| angle += math.radians(inc) | |
| rad += 0.005 | |
| x = int(r + rad * math.cos(math.radians(angle))) | |
| y = int(r + rad * math.sin(math.radians(angle))) | |
| inside = r * 2 > x >= 0 and r * 2 > y >= 0 | |
| for direction in [(0, 0), (0, 1), (1, 0), (1, 1)]: | |
| xn = x + direction[0] | |
| yn = y + direction[1] | |
| inside2 = r * 2 > xn >= 0 and r * 2 > yn >= 0 | |
| if inside2: | |
| rc = round((255 / (r * 2)) * x) | |
| gc = round((255 / (r * 2)) * y) | |
| bc = abs(255 - rc - gc) | |
| im.putpixel((xn, yn), (rc, gc, bc)) | |
| im.save("spiral_" + str(r) + ".png") | |
| spiral(500) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment