def __init__(self, name="turtle"): self.pen = True self.heading = 0 self.x = 0 self.y = 0 self.im = Svg(name + ".svg") self.color = "black" self.stroke = 1
class MyTurtle(): def __init__(self, name="turtle"): self.pen = True self.heading = 0 self.x = 0 self.y = 0 self.im = Svg(name + ".svg") self.color = "black" self.stroke = 1 def pd(self): self.pen = True def pu(self): self.pen = False def head(self, n): self.heading = n % 360 def right(self, n): self.heading = (self.heading - n) % 360 def left(self, n): self.heading = (self.heading + n) % 360 def forward(self, n): new_x = self.x + cos(radians(self.heading)) * n new_y = self.y + sin(radians(self.heading)) * n if self.pen: self.im.line(self.x, self.y, new_x, new_y, self.color, self.stroke) self.x, self.y = new_x, new_y def backward(self, n): self.right(180) self.forward(n) self.right(180) def begin_fill(self): pass def end_fill(self): pass def speed(self, n): pass def color(self, color): self.color = color def stroke(self, stroke): self.stroke = stroke def save(self): self.im.close() def clear(self): self.__init__()
def star(length, n, line_count, color_f=cf_black): im = Svg("star.svg") for i in range(n): x2 = cos(radians(360 / n * i)) * length y2 = sin(radians(360 / n * i)) * length x1 = cos(radians(360 / n * (i + 1))) * length y1 = sin(radians(360 / n * (i + 1))) * length star_quadrant(im, 0, (0, 0, x2, y2), (x1, y1, 0, 0), line_count, color_f) im.close()
def print_hexa(matrix, n): im = Svg("labirinth.svg") L = 20 D = sqrt(L**2 - (L / 2)**2) for i in range(n): for j in range(n): midx, midy = i * D * 2, j * 1.5 * L if j % 2 == 1: midx -= D if j == 0: print_side(midx, midy, L, D, 0, im) print_side(midx, midy, L, D, 5, im) if j == n - 1: print_side(midx, midy, L, D, 2, im) print_side(midx, midy, L, D, 3, im) if i == 0: if j % 2 == 0: print_side(midx, midy, L, D, 4, im) else: print_side(midx, midy, L, D, 3, im) print_side(midx, midy, L, D, 4, im) print_side(midx, midy, L, D, 5, im) if i == n - 1: if j % 2 == 1: print_side(midx, midy, L, D, 1, im) else: print_side(midx, midy, L, D, 0, im) print_side(midx, midy, L, D, 1, im) print_side(midx, midy, L, D, 2, im) if i < n - 1: if matrix[i + 1][j] not in matrix[i][j].ways: print_side(midx, midy, L, D, 1, im) if j < n - 1: if j % 2 == 0: if matrix[i][j + 1] not in matrix[i][j].ways: print_side(midx, midy, L, D, 3, im) if i + 1 < n and matrix[i + 1][j + 1] not in matrix[i][j].ways: print_side(midx, midy, L, D, 2, im) else: if matrix[i][j + 1] not in matrix[i][j].ways: print_side(midx, midy, L, D, 2, im) if matrix[i - 1][j + 1] not in matrix[i][j].ways: print_side(midx, midy, L, D, 3, im) im.close()
def solve(maze, start, end): matrix = generate_matrix(maze, len(maze[0]), len(maze)) result = [(None, inf)] dfs_rec(matrix[start[0]][start[1]], [], matrix[end[0]][end[1]], result, 0) LEN = 40 im = Svg("maze.svg") for x in range(len(maze[0])): for y in range(len(maze)): color = "white" if maze[x][y] == 1: color = "gray" if (x, y) == start or (x, y) == end: color = "blue" im.rectangle(x * LEN, y * LEN, LEN, LEN, fill=color) prew = matrix[start[0]][start[1]] for point in result[0][0][1:]: x1, y1 = prew.position x2, y2 = point.position im.line(x1 * LEN + LEN // 2, y1 * LEN - LEN // 2, x2 * LEN + LEN // 2, y2 * LEN - LEN // 2, "red") prew = point im.close()
def convex_wrap(n, count, points_f=random_points): im = Svg("convex_wrap.svg") points = points_f(n, count) lines = [] for (x, y) in points: im.line(x - 1.5, y - 1.5, x + 1.5, y + 1.5, "red", 4) start_point = min(points, key=lambda p: p[1]) actual_point = start_point previous_point = (actual_point[0] - 100, actual_point[1]) next_point = None while next_point != start_point: next_point = min(points, key=lambda p: angle(actual_point, p, previous_point)) lines.append((actual_point, next_point)) previous_point = actual_point actual_point = next_point for line in lines: ((xa, ya), (xb, yb)) = line im.line(xa, ya, xb, yb) im.close()
def pentagram_absolute(l): im = Svg("penta_" + str(l) + ".svg") points = [] for i in range(5): points.append((cos(radians(72 * i)) * l, sin(radians(72 * i)) * l)) for i in range(5): im.line(points[i][0], points[i][1], points[(i + 1) % 5][0], points[(i + 1) % 5][1]) im.line(points[i][0], points[i][1], points[(i + 2) % 5][0], points[(i + 2) % 5][1]) im.close()
def triangulation_heuristic(n, count, points_f=random_points): """ Triangulation using rule shortest lines first. Bad time complexity, nice results. """ im = Svg("triang.svg") points = points_f(n, count) lines = [] triang_lines = [] #line generation and sorting for i in range(len(points)): for j in range(i + 1, len(points)): lines.append((points[i], points[j])) lines.sort(reverse=True, key=line_length) while len(lines) > 0: new_line = lines.pop() crossing = False #check for crossing for line in triang_lines: x, y, cross = cross_point(line, new_line, -0.00001) if cross: crossing = True if not crossing: triang_lines.append(new_line) #printing for line in triang_lines: ((xa, ya), (xb, yb)) = line im.line(xa, ya, xb, yb) im.close()
def triangulation_random(n, count): """ Random triangulation, ugly code I won't use, just for refference, gives awful results """ im = Svg("triang.svg") points = random_points(n, count) lines = [] triang_lines = [] for i in range(len(points)): for j in range(i + 1, len(points)): lines.append((points[i], points[j])) while len(lines) > 0: new_line = choice(lines) crossing = False for line in triang_lines: x, y, cross = cross_point(line, new_line, -0.001) if cross: crossing = True if not crossing: triang_lines.append(new_line) lines.remove(new_line) for line in triang_lines: ((xa, ya), (xb, yb)) = line im.line(xa, ya, xb, yb) im.close()
def circle_lines(r, count): im = Svg("circle_lies_" + str(r) + "_" + str(count) + ".svg") for i in range(count): x = -r + r * 2 / count * i y = sin(acos(x / r)) * r im.line(x, y, x, -y) im.line(y, x, -y, x) im.close()
def test(): im = Svg("test.svg") sq = deepcopy(SQUARE_2) for line in sq: im.line(line[0][0], line[0][1], line[1][0], line[1][1]) trans = [shear(1.3), rotation(-10), scaling(0.9, 0.9), translation(50, 50)] for i in range(15): for i in range(4): p1, p2 = sq[i][0], sq[i][1] for tran in trans: p1, p2 = apply(p1, tran), apply(p2, tran) sq[i] = (p1, p2) for line in sq: im.line(line[0][0], line[0][1], line[1][0], line[1][1]) im.close()
def crosses(n, count, l): """ Genrates lines using random_lines and computes their crossing points. Prints lines and crossing points (in red) to .svg file """ im = Svg("crosses.svg") lines = random_lines(n, count, l) for line in lines: ((xa, ya), (xb, yb)) = line im.line(xa, ya, xb, yb) for i in range(count): for j in range(i + 1, count): (xp, yp, on_line) = cross_point(lines[i], lines[j]) if on_line: im.line(xp - 1.5, yp - 1.5, xp + 1.5, yp + 1.5, "red", 4) im.close()
def print_pascal(p, d, size, color_f): size = 10 im = Svg("pascal.svg") for line in p: count = len(line) left_offset = count * size * -0.5 for i in range(count): im.rectangle(left_offset + i * size, -(count - 1) * size, size, size, fill=color_f(d, line[i])) im.close()
def nested_polygons(poly=3, size=200, count=10): im = Svg("poly.svg") points = [] for i in range(count): r = size / count * (i + 1) points = [] for j in range(poly): points.append((cos(radians(360 / poly * j)) * r, sin(radians(360 / poly * j)) * r)) for i in range(poly): im.line(points[i][0], points[i][1], points[(i + 1) % poly][0], points[(i + 1) % poly][1]) im.close()
def mcrm(base, trans, iters): im = Svg("test.svg") for i in range(iters): new_base = [] for line in base: for tran in trans: p1, p2 = line for t in tran: p1, p2 = apply(p1, t), apply(p2, t) new_base.append((p1, p2)) base = new_base for line in base: im.line(line[0][0], line[0][1], line[1][0], line[1][1]) im.close()
def print_square(matrix, n): im = Svg("labirinth.svg") LEN = 20 im.line(0, 0, LEN * n, 0) im.line(0, 0, 0, LEN * n) im.line(LEN * n, 0, LEN * n, LEN * n) im.line(0, LEN * n, LEN * n, LEN * n) for i in range(n): for j in range(n): if i < n - 1: if matrix[i + 1][j] not in matrix[i][j].ways: im.line(LEN * (i + 1), LEN * j, LEN * (i + 1), LEN * (j + 1)) if j < n - 1: if matrix[i][j + 1] not in matrix[i][j].ways: im.line(LEN * i, LEN * (j + 1), LEN * (i + 1), LEN * (j + 1)) im.close()