Skip to content

Instantly share code, notes, and snippets.

@adusak
Last active August 6, 2016 14:00
Show Gist options
  • Save adusak/c625a4ca644ad7179ca8 to your computer and use it in GitHub Desktop.
Save adusak/c625a4ca644ad7179ca8 to your computer and use it in GitHub Desktop.
Generates bitmap of a spiral
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