montagne.py

Created by christian-mercat

Created on November 02, 2023

797 Bytes

Une fractale montagneuse: on remplace chaque triangle par 4 petits triangles en joignant presque le milieu des arêtes. Pour que les arêtes se recollent bien, je “triche” en tirant les mêmes nombres aléatoires de part et d’autre…


from kandinsky import*
from random import *

coef = 0.2 # De combien on perturbe, 20%
e=2.718281828459045
def ligne(u,v):
  n = round(abs(v-u))
  for k in range(n):
    w=u+k*(v-u)/n
    set_pixel(round(w.real),round(w.imag), color(0,0,0))

def montagne(u=10+190J+3*random(),v=310+190J+3*random(),w=160+10J+3*random(),n=6):
  if n==0:
    ligne(u,v)
    ligne(v,w)
    ligne(w,u)
  else:
    seed(round(10*(u+v).real))
    uv=(u+v)/2+abs(v-u)*coef*random()*pow(e,6.28J*random())
    seed(round(10*(v+w).real))
    vw=(v+w)/2+abs(w-v)*coef*random()*pow(e,6.28J*random())
    seed(round(10*(w+u).real))
    wu=(w+u)/2+abs(u-w)*coef*random()*pow(e,6.28J*random())
    montagne(u,uv,wu,n-1)
    montagne(v,vw,uv,n-1)
    montagne(w,wu,vw,n-1)
    montagne(uv,vw,wu,n-1)
    
montagne()

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 <a href="https://www.numworks.com/legal/cookies-policy/">cookies policy</a>.