creatures.py

Created by schraf

Created on April 24, 2023

481 Bytes

Représentation binaire des coefficients binomiaux de 0 à 33. D’étranges créatures à imaginer (singe, guerrier, extra-terrestre, oiseau…)

Visitez ma chaine YouTube

Version avec la bibliothèque PIL :

from PIL import Image, ImageDraw
from math import comb

source = Image.new("RGB", (1300, 50), color = "white")
draw = ImageDraw.Draw(source)

x = 0
for n in range(50):
 for i in range(n + 1):     
  r = comb(n, i)
  j = 0
  while r > 0: 
   if 1 & r:
    y = 50 - j 
    draw.point((x, y), fill = 'black')
   j += 1
   r >>= 1
  x += 1

source.show()


from kandinsky import *

def comb(n, k):
 if k > n // 2: k = n - k
 x = 1
 y = 1
 i = n - k + 1
 while i <= n:
  x = (x * i) // y
  y += 1
  i += 1
 return x

x = 0
dy = 0
maxi = 0
for n in range(34):
 if x + 2 * n > 320:
  x = 0
  dy -= 2 * maxi + 10
  maxi = 0
 for i in range(n + 1):     
  r = comb(n, i)
  j = 0
  while r > 0: 
   if 1 & r:
    y = 218 - 2 * j + dy 
    if j > maxi: maxi = j
    fill_rect(x, y, 2, 2, (0, 0, 0))
   j += 1
   r >>= 1
  x += 2