ifs.py

Created by nicolas-patrois

Created on August 29, 2019

747 Bytes

Draws iterated functions fractals.

nb is the number of pixels drawn.

Draw a tree: m=350 w.append([[0,0],[0,.5]]); b.append([[0],[0]]); p.append(.05) w.append([[.42,-.42],[.42,.42]]); b.append([[0],[.2]]); p.append(p[-1]+.4) w.append([[.42,.42],[-.42,.42]]); b.append([[0],[.2]]); p.append(p[-1]+.4) w.append([[.1,0],[0,.1]]); b.append([[0],[.2]]); p.append(1)


def ifs(nb):
 from random import random
 import kandinsky

 def vecprod(M,V):
  nlM=len(M)
  return [[sum([l[j]*V[j][0] for j in range(nlM)])] for l in M]

 def vecadd(v1,v2):
  return [[v1[i][0]+v2[i][0]] for i in range(len(v1))]

 w=[];b=[];p=[]

 # Fougere
 m=15
 w+=[[[0,0],[0,.16]]];b+=[[[0],[0]]]; p+=[.01]
 w+=[[[.85,.04],[-.04,.85]]]; b+=[[[0],[2]]]; p+=[p[-1]+.85]
 w+=[[[.2,-.26],[.23,.22]]]; b+=[[[0],[1]]]; p+=[p[-1]+(1-p[-1])/2]
 w+=[[[-.15,.28],[.26,.24]]]; b+=[[[0],[.9]]]; p+=[1]

 n=len(w)

 def choix(x):
  h=random()
  i=0
  while True:
   if h<=p[i]:
    return vecadd(b[i],vecprod(w[i],x))
   i+=1

 x=[[0],[0]]

 for _ in range(nb):
  x=choix(x)
  kandinsky.set_pixel(160+int(m*x[0][0]),220-int(m*x[1][0]),(0,0,0))

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.