bezier_curve.py

Created by pikube

Created on November 08, 2019

1.02 KB

Courbe de bézier de avec n points de contrôle (le premier est à l’origine, en haut à gauche): essayez render(5)


from math import *
from cmath import *
import kandinsky as k
import random as ran
dark=k.color(0,0,0)
red=k.color(255,0,0)
def factorial(x):
  x=int(x)
  if x>=0:
    s=1
    for loop in range(x):
      s*=(loop+1)
    return s

def binomial(n,k):
  return(factorial(n)/(factorial(k)*factorial(n-k)))     

def bernstein(m,i,u):
  return(binomial(m,i)*(u**i)*((1-u)**(m-i)))

def setpoint(x,y):
  for cx in range(7):
    k.set_pixel((x-3+cx),y,dark)
  for cy in range(7):
    k.set_pixel(x,y-3+cy,dark)

def color(x,y):
  k.set_pixel(x,y,red)

def P(t,list):
  s=0
  for loop in range(len(list)):
    s+=bernstein(len(list),loop,t)*list[loop]
  return s

def render(n):
  n-=1
  ptsx=[]
  ptsy=[]
  setpoint(0,0)
  for loop in range(n):
    ptsx.append(ran.randrange(0,320))
    ptsy.append(ran.randrange(0,222))
    setpoint(ptsx[loop],ptsy[loop])
  step=0
  while 1:
    t=(1/(2**step))
    for loop in range(1+2**step):
      k.fill_rect(int(P(t*loop,ptsx)),int(P(t*loop,ptsy)),1,1,(0,int(255-loop*t*255),0))
    step+=1

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.