carres_emboites.py

Created by telpe51

Created on July 06, 2018

1.17 KB

Trace une succession de carrés emboîtés : emboite((A,c), n, r) avec A couple de coordonnées du sommet en haut à gauche, c longueur du coté du premier carré, n nombre de carrés tracés, r coefficient de réduction des carrés successifs. Ce script reprend la fonction seg du flocon dans laquelle on peut changer la couleur.


from kandinsky import *
A=(10,10)
c=200
r=0.05
def carre(A,c):
    B=(A[0]+c,A[1])
    C=(B[0],B[1]+c)
    D=(A[0],C[1])
    return (A,B,C,D)
def vect(A,B):
    return (B[0]-A[0],B[1]-A[1])
def somme(poin,vec):
    return (poin[0]+vec[0],poin[1]+vec[1])
def mult(reel,vec):
    return (reel*vec[0],reel*vec[1])
def seg(xa,ya,xb,yb,r=0,v=150,b=150):
  col=color(r,v,b)
  if xa==xb :
    if ya<yb:
      y=ya
      bs=yb
    else :
      y=yb
      bs=ya
    while y<=bs:
      set_pixel(int(xa),int(y),col)
      y=y+1
  else:
    m=(yb-ya)/(xb-xa)
    p=ya-m*xa
    if xa<xb:
      x=xa
      bs=xb
    else :
      x=xb
      bs=xa
    while x<=bs:
      y=m*x+p
      set_pixel(int(x),int(y),col)
      x=x+1
def quadri(A,B,C,D): 
    quadr=[A,B,C,D,A]
    for i in range(4):
        seg(quadr[i][0],quadr[i][1],quadr[i+1][0],quadr[i+1][1])
def emboite(qua,n=50,redu=0.1):
    A=qua[0]
    B=qua[1]
    C=qua[2]
    D=qua[3]
    r=redu
    for k in range(n):
        quadri(A,B,C,D)
        v1=vect(A,B)
        v2=vect(B, C)
        A=somme(A,mult(r,v1))
        B=somme(B, mult(r,v2))
        C=somme(C,mult(-r,v1))
        D=somme(D,mult(-r,v2))
emboite(carre(A,c),100,0.05)

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.