veramolnar.py

Created by schraf

Created on May 08, 2021

1.78 KB

Enoncé des 4 exercices et Corrigé

Utilisation

Lancez le script puis molnar(1) pour l’exercice 1, molnar(2) pour l’exercice 2 etc.


from turtle import *
from random import *
from math import sin, cos, pi
from kandinsky import fill_rect

speed(0)
hideturtle()

def melange(v):
    return sorted(v, key=lambda _: random())

def forme(n):
 trace=[127,25,45,14,346,21,113,38]
 pensize(n)
 pendown() 
 for i in range(len(trace)/2):
  setheading(trace[2*i])
  forward(trace[2*i+1])

def molnar1():
 for c in range(15):
  l = melange(list(range(3)))
  u, v = -140 + 20 * c, -100 + randint(1,48)
  for y in range(3):
   penup()
   goto(u, v + 48 * y)
   forme(l[y]+1)

  
def case(k,n,d):
  return [d * (k % n), d * int(k / n)]  

def molnar2(N = 8):
  x, y = -130,-110
  for j in range(N*N):
   l = melange(list(range(N*N)))
   [x1,y1] = case(j,N,30)
   color((randint(0,255),randint(0,255),randint(0,255)))
   for i in range(N*N):
      [x2,y2] = case(l[i],N,5)
      penup() if i == 0 else pendown()
      goto(x + x1 + x2, y + y1 + y2)

def molnar3(d = 6):
  pensize(2)
  for y in range(16):
    penup()
    goto(-160, -110 + 15 * y)
    pendown()
    while position()[0] < 160:
      if random() < .1:
        [x,y] = position()  
        a = randint(0,90)
        co, si = d * cos(pi * a / 180), d * sin(pi * a / 180)
        penup()
        goto(x, y - si)
        setheading(a)
        pendown()
        forward(2 * d)
        penup()
        goto(min(160,x + 2 * co), y)
        pendown()
      else: 
        setheading(0)
        forward(2 * d)

def molnar4():
  pensize(1)
  for y in range(20): 
    penup()
    goto(-150, 90 - 10 * y)
    pendown()
    while position()[0] < 130:
      [x,y] = position()
      a = randint(10,60)
      d = randint(3, 3 + int((x + 150) / 10))
      setheading(a)
      forward(d)
      goto(x + randint(3,7),y)

def molnar(n):
  fill_rect(0,0,320,222,(230,224,205))
  eval("molnar"+str(n))()

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.