Skip to content

Instantly share code, notes, and snippets.

@adusak
Last active August 29, 2015 14:17
Show Gist options
  • Select an option

  • Save adusak/18f97f71061600f0a13b to your computer and use it in GitHub Desktop.

Select an option

Save adusak/18f97f71061600f0a13b to your computer and use it in GitHub Desktop.
Generates bitmap of equilateral triangle
import math
from PIL import Image
def triangle(length=20):
im = Image.new("RGB", (length, length), (255, 255, 255))
for x in range(length):
for y in range(length):
x_n, y_n = x - length / 2, length - y
if (y_n <= 2 / math.sqrt(2) * x_n + length / math.sqrt(2) and
-2 / math.sqrt(2) * x_n + length / math.sqrt(2) >= y_n >= 0):
r = round(255 / length * (length - x))
b = round(255 / length * (length - y))
g = abs(255 - r - b)
im.putpixel((x, y), (r, g, b))
im.save("triangle_" + str(length) + ".png")
# triangle(500)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment