Last active
August 29, 2015 14:17
-
-
Save adusak/18f97f71061600f0a13b to your computer and use it in GitHub Desktop.
Generates bitmap of equilateral triangle
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 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