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/422038ed36ce0f4ce6f6 to your computer and use it in GitHub Desktop.

Select an option

Save adusak/422038ed36ce0f4ce6f6 to your computer and use it in GitHub Desktop.
Gnerates bitmap of tilted ellipse
import math
from PIL import Image
def ellipse(major, minor, angle):
ang_r = math.radians(angle)
width = major * 2
heigth = major * 3
print((width, heigth))
im = Image.new("RGB", (width, heigth), (255, 255, 255))
for x in range(width):
for y in range(heigth):
xn = width / 2 - x
yn = heigth / 2 - y
eq1 = ((xn * math.cos(ang_r) + yn * math.sin(ang_r)) ** 2 / major ** 2)
eq2 = ((yn * math.cos(ang_r) - xn * math.sin(ang_r)) ** 2 / minor ** 2)
fin = eq1 + eq2
col = round(255 * fin)
if fin <= 1:
im.putpixel((x, y), (col, col, col))
# im.save("ellipse_"+str(major)+"_"+str(minor)+"_"+str(angle)+".png")
im.show()
# ellipse(300, 200, 45)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment