@Annvil/

FactalSquare

Python (with Turtle)

No description

fork
loading
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import turtle

def drawSquare(cx, cy, size):
  fractal.penup()
  fractal.goto(cx-size/2, cy-size/2)
  fractal.pendown()
  fractal.begin_fill()
  for i in [0,1,2,3]:
    fractal.forward(size)
    fractal.left(90)
  fractal.end_fill()
  fractal.penup

def drawCarpet(cx, cy, size, depth): 
  #print(depth, cx, cy, size)
  
  #base case
  if depth == 0:
    return
  
  #draw big square
  drawSquare(cx, cy, size)

  #draw subordinate squares
  for i in [0,1,2,3,5,6,7,8]:
    drawCarpet(cx+((i%3)-2)*size+size, cy+(i/3-1)*size, size/3, depth-1)
  
fractal = turtle.Turtle()
fractal.speed(0)
fractal.color("black", "black")
drawCarpet(0, 0, float(150), 5)
  
result
console