def sierpinski_ifs(vertices = [(0,0), (1,0), (1/2,sqrt(3)/2)], n = 10000, func=None): "Generate sierpinski points" point = oneOf(vertices) res = [] for i in range(n): if func is None: point = add_coords(point, oneOf(vertices)) point = [x / (len(vertices)-1) for x in point] #midpoint is 1/2 that is 1/(3-1) 3 represents the number of vertices... else: point = func(point, oneOf(vertices)) #if we want to force midpoint for all n. we can use any function that takes two points and return a new one res += [point] return res
def limitlessSelfAvoid3D(): def filterNeighboursVisitable(neighs): return list(set(neighs) - visited) coord = (0, 0, 0) steps = 0 visited = set() while True: # print(coord, steps, visited) steps += 1 visited.add(coord) neighs = filterNeighboursVisitable(neighbours(coord, POINTS3D)) if neighs: return steps coord = oneOf(neighs)
def selfAvoidHappened(n): "Tells us if self avoidance happened during the trial for n*n matrix" visited = [[False] * n for j in range(n)] def isVisited(coord): return visited[coord[0]][coord[1]] def setVisited(coord): visited[coord[0]][coord[1]] = True def isOutside(coord): return not (0 <= coord[0] <= (n - 1) and 0 <= coord[1] <= (n - 1)) def isDeadEnd(coord): neighs = neighbours(coord) for point in neighs: if isOutside(point) or not isVisited(point): return False return True def filterNeighboursVisitableOrOutside(neighs): res = [] for point in neighs: if isOutside(point) or not isVisited(point): res += [point] return res coord = (n // 2, n // 2) steps = 0 box = (coord, coord) while True: steps += 1 neighs = filterNeighboursVisitableOrOutside(neighbours(coord)) if isDeadEnd(coord): return (False, steps, calcArea(box)) else: coord = oneOf(neighs) box = boundingRectangle(box, coord) if isOutside(coord): return (True, steps, calcArea(box)) setVisited(coord)
import sys import math sys.path.append('../') import stddraw import random from functools import partial from randomutils import oneOf from designs import draw_design1, draw_design2, draw_design3, draw_design4 if __name__ == "__main__": n = int(sys.argv[1]) is_checkered = oneOf([True, False]) stddraw.setXscale(0, n) stddraw.setYscale(0, n) design = oneOf([draw_design1, draw_design2, draw_design3, draw_design4]) color = oneOf([stddraw.BLUE, stddraw.GRAY]) dark = oneOf([stddraw.BLACK, stddraw.RED]) dull = oneOf([stddraw.WHITE, stddraw.BLUE]) stddraw.clear(color) is_checkered = True for i in range(n): for j in range(n): if (i + j) % 2 == 0 or not is_checkered: design(i, j, 1, 1, color, dark, dull) stddraw.show()