TheAlgorithms-Python

Форк
0
76 строк · 2.2 Кб
1
"""Authors Bastien Capiaux & Mehdi Oudghiri
2

3
The Vicsek fractal algorithm is a recursive algorithm that creates a
4
pattern known as the Vicsek fractal or the Vicsek square.
5
It is based on the concept of self-similarity, where the pattern at each
6
level of recursion resembles the overall pattern.
7
The algorithm involves dividing a square into 9 equal smaller squares,
8
removing the center square, and then repeating this process on the remaining 8 squares.
9
This results in a pattern that exhibits self-similarity and has a
10
square-shaped outline with smaller squares within it.
11

12
Source: https://en.wikipedia.org/wiki/Vicsek_fractal
13
"""
14

15
import turtle
16

17

18
def draw_cross(x: float, y: float, length: float):
19
    """
20
    Draw a cross at the specified position and with the specified length.
21
    """
22
    turtle.up()
23
    turtle.goto(x - length / 2, y - length / 6)
24
    turtle.down()
25
    turtle.seth(0)
26
    turtle.begin_fill()
27
    for _ in range(4):
28
        turtle.fd(length / 3)
29
        turtle.right(90)
30
        turtle.fd(length / 3)
31
        turtle.left(90)
32
        turtle.fd(length / 3)
33
        turtle.left(90)
34
    turtle.end_fill()
35

36

37
def draw_fractal_recursive(x: float, y: float, length: float, depth: float):
38
    """
39
    Recursively draw the Vicsek fractal at the specified position, with the
40
    specified length and depth.
41
    """
42
    if depth == 0:
43
        draw_cross(x, y, length)
44
        return
45

46
    draw_fractal_recursive(x, y, length / 3, depth - 1)
47
    draw_fractal_recursive(x + length / 3, y, length / 3, depth - 1)
48
    draw_fractal_recursive(x - length / 3, y, length / 3, depth - 1)
49
    draw_fractal_recursive(x, y + length / 3, length / 3, depth - 1)
50
    draw_fractal_recursive(x, y - length / 3, length / 3, depth - 1)
51

52

53
def set_color(rgb: str):
54
    turtle.color(rgb)
55

56

57
def draw_vicsek_fractal(x: float, y: float, length: float, depth: float, color="blue"):
58
    """
59
    Draw the Vicsek fractal at the specified position, with the specified
60
    length and depth.
61
    """
62
    turtle.speed(0)
63
    turtle.hideturtle()
64
    set_color(color)
65
    draw_fractal_recursive(x, y, length, depth)
66
    turtle.Screen().update()
67

68

69
def main():
70
    draw_vicsek_fractal(0, 0, 800, 4)
71

72
    turtle.done()
73

74

75
if __name__ == "__main__":
76
    main()
77

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.