def chaos_game(n, iterations, r, name, random=False): zofka = SvgTurtle(500, 800) points = n_square_points(n) if random: numbers = [x-100 for x in range(200)] for point in points: point[0] += choice(numbers) point[1] += choice(numbers) for point in range(len(points)): zofka.line(points[point][0], points[point][1], points[point-1][0], points[point-1][1]) for x in range(iterations): point = choice(points) t = SvgTurtle(point[0], point[1]) position = zofka.position() if position[0] > point[0]: posx = point[0] + abs(position[0] - point[0]) * r else: posx = position[0] + abs(position[0] - point[0]) * (1-r) if position[1] > point[1]: posy = point[1] + abs(position[1] - point[1]) * r else: posy = position[1] + abs(position[1] - point[1]) * (1-r) zofka.set_pos(posx, posy) zofka.self_point() zofka.save(name+".svg")
def ring_with_lines(): zofka = SvgTurtle(500, 500) for x in range(-200, 201, 25): value = sqrt(200**2 - x**2) y = x + 500 zofka.line(y, 500 + value, y, 500 - value) zofka.line(500 + value, y, 500 - value, y) zofka.save("ring_lines.svg")
def pentagram_absolute(): zofka = SvgTurtle(500, 500) angle = (1 - 2 / 5) * 180 positions = [] for x in range(5): zofka.forward(200) zofka.left(180-108) positions.append(zofka.position()) for x in range(5): zofka.line(positions[x][0], positions[x][1], positions[x-1][0], positions[x-1][1]) for x in range(5): zofka.line(positions[x][0], positions[x][1], positions[x - 2][0], positions[x - 2][1]) zofka.save("absolute_pentragram.svg")
def create_maze(n): walls = [] points = [] for x in range(1, n): for y in range(1, n): points.append((x, y)) for x in range(n): walls.append(((0, x), (0, x + 1))) walls.append(((x, n), (x + 1, n))) walls.append(((n, x), (n, x + 1))) walls.append(((x, 0), (x + 1, 0))) while len(points): point = choice(points) x = point[0] y = point[1] c = 0 while point in points: wall = create_wall(x, y) if in_walls(wall, walls): continue if is_still_braid(walls, wall): walls.append(wall) points.remove(point) try: points.remove(wall[1]) except: pass c = c + 1 if c > n * 3: return False last_len = len(walls) while True: for x in range(n): for y in range(n): wall = create_wall(x, y) if in_walls(wall, walls): continue if is_still_braid(walls, wall): walls.append(wall) if last_len == len(walls): break last_len = len(walls) zofka = SvgTurtle(0, 0) for d in walls: zofka.line(d[0][0] * 100, d[0][1] * 100, d[1][0] * 100, d[1][1] * 100) zofka.save("p.svg") return True
def triangulation(n): points = generate_points(n) lines = [] for ia, a in enumerate(points): for ib, b in enumerate(points): if ib > ia: lines.append((a, b, calculate_length(a, b))) lines = sorted(lines, key=itemgetter(2)) zofka = SvgTurtle(1000, 1000) printed_lines = [] for line in lines: if not is_there_intersection( (line[0][0], line[0][1], line[1][0], line[1][1]), printed_lines): zofka.line(line[0][0], line[0][1], line[1][0], line[1][1]) printed_lines.append( (line[0][0], line[0][1], line[1][0], line[1][1])) zofka.save("triangulation.svg")
def triangles(): zofka = SvgTurtle(500, 500) length = 30 zofka.left(90) for x in range(15): positions = [] for x in range(3): zofka.forward(length, write=False) positions.append(zofka.position()) zofka.left(180) zofka.forward(length, write=False) zofka.right(60) positions.append(positions[0]) for x in range(3): zofka.line(positions[x][0], positions[x][1], positions[x+1][0], positions[x+1][1]) length += 30 zofka.save("triangles.svg")
def convex_hull(points): points = sorted(points, key=itemgetter(0)) angles = [] for point in points[1:]: c = calculate_length(points[0], point) b = points[0][1] - point[1] angle = degrees(acos(b / c)) angles.append(angle) points_angles = [] for x in range(len(angles)): points_angles.append((points[x + 1], angles[x])) points_angles = [[points[0], 0]] + sorted(points_angles, key=itemgetter(1)) points = [x[0] for x in points_angles] zofka = SvgTurtle(1000, 1000) for x, y in points: zofka.point(x, y) count = 1 while count != 0: count = 0 lines = [points[0] + points[1]] for x in range(1, len(points)): try: delete = True while delete: if not is_convex(points[x] + points[x + 1], lines): print(x) points.remove(points[x]) delete = True count += 1 else: delete = False lines.append(points[x - 1] + points[x]) except IndexError: break for x in range(len(points)): zofka.line(points[x][0], points[x][1], points[x - 1][0], points[x - 1][1]) zofka.save("convex_hull.svg")