Sierpinski carpet: Difference between revisions
Content added Content deleted
Line 4,977: | Line 4,977: | ||
n = 4 |
n = 4 |
||
@limit = width / 3**(n - 1) |
@limit = width / 3**(n - 1) |
||
⚫ | |||
⚫ | |||
⚫ | |||
# draw first hole |
|||
rect(width / 3, height / 3, width / 3, width / 3) |
|||
holes(width / 3, height / 3, width / 3) |
|||
end |
end |
||
def in_carpet?(x, y) |
|||
def draw |
|||
!(x == 1 && y == 1) |
|||
⚫ | |||
⚫ | |||
⚫ | |||
draw_carpet 0, 0, width |
|||
end |
end |
||
⚫ | |||
private |
|||
⚫ | |||
xc = x - s |
|||
⚫ | |||
yc = y - s |
|||
grid(3, 3) do |row, col| |
|||
rect x + len, y + len, len, len # draw hole |
|||
next unless in_carpet?(row, col) |
|||
⚫ | |||
xx = xc + row * s |
|||
draw_carpet x, y, len |
|||
yy = yc + col * s |
|||
len = s / 3 |
|||
rect(xx + len, yy + len, len, len) |
|||
holes(xx + s / 3, yy + s / 3, s / 3) |
|||
end |
|||
draw_carpet x, y + 2 * len, len |
|||
draw_carpet x + len, y + 2 * len, len |
|||
draw_carpet x + 2 * len, y + 2 * len, len |
|||
end |
end |
||
</lang> |
</lang> |
||