Фракталы

  • Дерево
  • from turtle import*
    
    lt(90)
    def fr(w):
        if w >= 1:
            pensize(w)
            fd(w * 10)
            rt(30)
            fr(w * 0.75)
            lt(60)
            fr(w * 0.75)
            rt(30)
            bk(w * 10)
            pensize(w)
    speed(10)
    hideturtle()
    penup()
    goto(0, -200)
    pendown()
    fr(10)
    

    derevo

  • Веточка 1
  • from turtle import*
    from random import*
    
    def drawFractalTree(t, depth, maxdepth):
        if depth > maxdepth:
            return
        else:
            for i in range(2):
                rand = randrange(-30, 30)
                t.left(rand)
                t.forward(50*(0.8)**depth)
                another = t.clone()
                drawFractalTree(another, depth+1, maxdepth)
            return
    
    def draw_picture():
        window = Screen()
        tree = Turtle()
        tree.pensize(2)
        tree.penup()
        tree.goto(0, -200)
        tree.left(90)
        tree.pendown()
        tree.hideturtle()
        tree.speed(10)
        drawFractalTree(tree, 0, 12)
        window.exitonclick()
    
    draw_picture()
    

    vetka

  • Веточка 2
  • from turtle import*
    
    def draw_partial_fern(t, size, angle, c1, c2):
        t.left(angle)
        draw_fern(t, size * c1)
        t.right(angle)
        t.backward(size * c2)
    
    def draw_fern(t, size):
        if size > 1:
            t.forward(size)
            draw_partial_fern(t, size, 5, 0.8, 0.05)
            draw_partial_fern(t, size, -40, 0.45, 0.2)
            draw_partial_fern(t, size, 35, 0.4, 0.75)
    
    def draw_art():
        window = Screen()
        fr = Turtle()
        fr.color("blue")
        fr.speed(0)
        fr.hideturtle()
        fr.left(90)
        draw_fern(fr, 60)
        window.exitonclick()
    
    draw_art()
    

    vetka2

  • Треугольник Серпинского
  • from turtle import*
    
    def draw_sierpinski(length,depth):
        if depth==0:
            for i in range(0,3):
                t.fd(length)
                t.left(120)
        else:
            draw_sierpinski(length/2,depth-1)
            t.fd(length/2)
            draw_sierpinski(length/2,depth-1)
            t.bk(length/2)
            t.left(60)
            t.fd(length/2)
            t.right(60)
            draw_sierpinski(length/2,depth-1)
            t.left(60)
            t.bk(length/2)
            t.right(60)
    
    window = Screen()
    t = Turtle()
    t.speed(10)
    t.pensize(2)
    t.penup()
    t.goto(-200, -200)
    t.pendown()
    t.hideturtle()
    draw_sierpinski(500,7)
    window.exitonclick()
    

    serp

  • Фрактал Коха
  • from turtle import*
    def koch(t, order, size):
        if order == 0:                 
            t.forward(size)
        else:
            koch(t, order-1, size/2.7)  
            t.left(60)
            koch(t, order-1, size/2.7)
            t.right(120)
            koch(t, order-1, size/2.7)
            t.left(60)
            koch(t, order-1, size/2.7)
    
    fr = Turtle()
    wn = Screen()
    fr.speed(10)
    fr.penup()
    fr.backward(150)
    fr.pendown()
    fr.hideturtle()
    fr.pensize(2)
    fr.penup()
    fr.goto(-400, -100)
    fr.pendown()
    
    koch(fr, 5, 500)
    wn.exitonclick()
    

    koh

  • Дерево Пифагора
  • from turtle import*
    from math import*  
    def fractal(aturt, depth, maxdepth):  
       if depth > maxdepth:  
         return  
       length = 180*((sqrt(2)/2)**depth)  
       anotherturt = aturt.clone()  
       aturt.forward(length)  
       aturt.left(45)  
       fractal(aturt, depth+1, maxdepth)  
       anotherturt.right(90)  
       anotherturt.forward(length)  
       anotherturt.left(90)  
       anotherturt.forward(length)  
       if depth != maxdepth:  
         turt3 = anotherturt.clone()  
         turt3.left(45)  
         turt3.forward(180*((sqrt(2)/2)**(1+depth)))  
         turt3.right(90)  
         fractal(turt3, depth+1, maxdepth)  
       anotherturt.left(90)  
       anotherturt.forward(length)  
    
    def draw_fractal():  
       window = Screen()  
       t = Turtle()  
       t.hideturtle()  
       t.penup()  
       t.goto(-75, -225)  
       t.pendown()  
       t.speed(10)  
       t.left(90)  
       fractal(t, 1, 12)  
       window.exitonclick()  
    
    draw_fractal()  
    

    pifo

  • Cсылки
  • 1. Веточка1, Веточка2, Треугольник Серпинского
    2. Фрактал Коха
    3. Дерево
    4. Дерево Пифагора

Добавить комментарий