def print_maze(index, maze_size, maze_str, positions, cell_size=50): # create a list of lists from give 'maze string' maze_iter = iter(maze_str) maze_list = [[next(maze_iter) for i in range(maze_size)] for j in range(maze_size)] svg = vector.SVG(folder="cv11", name="maze%d" % index) # add lines for i in range(maze_size + 1): svg.add_line(0 + cell_size*i, 0, 0 + cell_size*i, maze_size*cell_size) svg.add_line(0, 0 + cell_size*i, maze_size*cell_size, 0 + cell_size*i) # add start points and 'walls' for i in range(maze_size): for j in range(maze_size): if maze_list[i][j] == "#": rect = vector.Rectangle(x=i*cell_size, y=j*cell_size, size=cell_size, color="black") svg.objects.append(rect) elif maze_list[i][j] == "A": point = vector.Point(x=(i*cell_size + cell_size/2), y=(j*cell_size + cell_size/2), color="green") svg.objects.append(point) elif maze_list[i][j] == "B": point = vector.Point(x=(i*cell_size + cell_size/2), y=(j*cell_size + cell_size/2), color="red") svg.objects.append(point) # print moves for i in range(len(positions) - 1): start = vector.Point(x=(positions[i][0]*cell_size + cell_size/2), y=(positions[i][1]*cell_size + cell_size/2)) end = vector.Point(x=(positions[i+1][0]*cell_size + cell_size/2), y=(positions[i+1][1]*cell_size + cell_size/2)) line = vector.Line(start=start, end=end, color="blue") svg.objects.append(line) svg.save()
def convex_hull(): svg = vector.SVG(folder="cv5", name="convex") points = [] for i in range(100): point = vector.Point(random.uniform(100, 500), random.uniform(100, 500)) points.append(point) svg.objects.append(point) x_sorted = sorted(points, key=lambda point: point.x) bottom = [] # bottom half of the hull for point in x_sorted: while len(bottom) > 1 and orientation(bottom[-2], bottom[-1], point) <= 0: bottom.pop() bottom.append(point) top = [] # top half of the hull for point in reversed(x_sorted): while len(top) > 1 and orientation(top[-2], top[-1], point) <= 0: top.pop() top.append(point) convex = bottom + top for i in range(len(convex) - 1): svg.objects.append(vector.Line(convex[i], convex[i + 1])) svg.save()
def intersections(): lines = [] svg = vector.SVG(folder="cv5", name="intersections") lenght = 500 for i in range(50): a = vector.Point(random.uniform(500, 1000), random.uniform(500, 1000)) # random start point rangle = math.radians(random.uniform(0, 360)) # random angle b = vector.Point(a.x + lenght * math.cos(rangle), a.y + lenght * math.sin(rangle)) line = vector.Line(a, b) lines.append(line) svg.objects.append(line) for line1 in lines: for line2 in lines: if line1 == line2: continue intersection = get_intersection(line1, line2) if intersection is not None: svg.objects.append(intersection) svg.save()
def __init__(self, name): self.position = _Position( 500, 500) # FIXME -- in vector fix negative coords self._write = True self.angle = 0 self.stack = [] self.svg = vector.SVG(folder="cv3", name=name)
def vector_img(size=200): svg = vector.SVG(folder="cv1", name="vector") for i in range(0, size + 10, 10): svg.add_line(size, 2 * size - i, size - i, size) svg.add_line(size, 2 * size - i, size + i, size) svg.add_line(size, 0 + i, size - i, size) svg.add_line(size, 0 + i, size + i, size) svg.save()
def example2(): svg = vector.SVG("cv8", "example2") matrix = combine([rotate(10), scale(1.1, 0.8)]) obj = square(cx=0, cy=0) svg.objects.extend(obj) for i in range(15): obj = do_it(matrix, obj) svg.objects.extend(obj) svg.save()
def example1(): svg = vector.SVG("cv8", "example1") matrix = combine([rotate(20), scale(1.1, 1.1), translate(5, 10)]) obj = square(a=100, cx=50, cy=50) svg.objects.extend(obj) for i in range(10): obj = do_it(matrix, obj) svg.objects.extend(obj) svg.save()
def example3(): svg = vector.SVG("cv8", "example3") matrix = combine([shear(1.3), rotate(10), scale(0.9, 0.9), translate(50, 50)]) obj = square() svg.objects.extend(obj) for i in range(5): obj = do_it(matrix, obj) svg.objects.extend(obj) svg.save()
def save_svg(self): svg = vector.SVG(folder="cv12", name="maze_%d" % self.size) hex_height = self.seg_size * math.sin(math.radians(60)) for cell in self.maze.values(): points = [] if cell.x % 2 == 0: for i in range(6): points.append( (self.seg_size * math.cos(2 * math.pi * i / 6) + self.seg_size + (1.5 * cell.x * self.seg_size), self.seg_size * math.sin(2 * math.pi * i / 6) + hex_height + 2 * hex_height * cell.y)) else: for i in range(6): points.append( (self.seg_size * math.cos(2 * math.pi * i / 6) + self.seg_size + (1.5 * cell.x * self.seg_size), self.seg_size * math.sin(2 * math.pi * i / 6) + hex_height + hex_height + (2 * cell.y * hex_height))) if cell.walls["right-lower"]: svg.add_line(points[0][0], points[0][1], points[1][0], points[1][1]) if cell.walls["bottom"]: svg.add_line(points[1][0], points[1][1], points[2][0], points[2][1]) if cell.walls["left-lower"]: svg.add_line(points[2][0], points[2][1], points[3][0], points[3][1]) if cell.walls["left-upper"]: svg.add_line(points[3][0], points[3][1], points[4][0], points[4][1]) if cell.walls["top"]: svg.add_line(points[4][0], points[4][1], points[5][0], points[5][1]) if cell.walls["right-upper"]: svg.add_line(points[5][0], points[5][1], points[0][0], points[0][1]) if cell.x == 0 and cell.y == 0: svg.objects.append( vector.Point(points[0][0] - self.seg_size, points[0][1], color="green")) if cell.x == self.size - 1 and cell.y == self.size - 1: svg.objects.append( vector.Point(points[0][0] - self.seg_size, points[0][1], color="red")) svg.save()
def pentagram_lines(): svg = vector.SVG(folder="cv3", name="pentagram2") points = [] # calculate point coordinates, +100 is padding to make sure there are no # negative coordinates because these doesn't work with svg for i in range(5): points.append((100 * math.cos(2 * math.pi * i / 5) + 100, 100 * math.sin(2 * math.pi * i / 5) + 100)) # connect all points for i in points: for j in points: svg.add_line(i[0], i[1], j[0], j[1]) svg.save()
def save_svg(self): svg = vector.SVG(folder="cv12", name="maze_%d" % self.size) for cell in self.maze.values(): if cell.walls["top"]: svg.add_line((cell.x * self.seg_size), (cell.y * self.seg_size), (cell.x * self.seg_size) + self.seg_size, (cell.y * self.seg_size)) if cell.walls["right"]: svg.add_line((cell.x * self.seg_size) + self.seg_size, (cell.y * self.seg_size), (cell.x * self.seg_size) + self.seg_size, (cell.y * self.seg_size) + self.seg_size) if cell.walls["bottom"]: svg.add_line((cell.x * self.seg_size) + self.seg_size, (cell.y * self.seg_size) + self.seg_size, (cell.x * self.seg_size), (cell.y * self.seg_size) + self.seg_size) if cell.walls["left"]: svg.add_line((cell.x * self.seg_size), (cell.y * self.seg_size) + self.seg_size, (cell.x * self.seg_size), (cell.y * self.seg_size)) svg.save()
def circle_lines(): svg = vector.SVG(folder="cv3", name="circle") d = 200 # "center" lines svg.add_line(100, 0, 100, 200) svg.add_line(0, 100, 200, 100) for i in range(5, 100, 5): lenght = math.sqrt(d**2 / 4 - i**2) # lenght of the line to draw svg.add_line(100 - i, d / 2 - lenght, 100 - i, d / 2 + lenght) # left part svg.add_line(100 + i, d / 2 - lenght, 100 + i, d / 2 + lenght) # right part svg.add_line(d / 2 - lenght, 100 - i, d / 2 + lenght, 100 - i) # top part svg.add_line(d / 2 - lenght, 100 + i, d / 2 + lenght, 100 + i) # bottom part svg.save()
def triangulation(): svg = vector.SVG(folder="cv5", name="triangulation") # random points points = [] for i in range(50): point = vector.Point(random.uniform(100, 500), random.uniform(100, 500)) points.append(point) svg.objects.append(point) # all lines lines = [] for point1 in points: for point2 in points: if point1 == point2: continue lines.append(vector.Line(point1, point2)) # sort lines by lenght lines = sorted(lines, key=lambda line: line.length) triang_lines = [] for candidate in lines: add = True for line in triang_lines: if get_intersection(line, candidate): add = False # intersection with some line in triangulation --> do not add break if add: triang_lines.append(candidate) for line in triang_lines: svg.objects.append(line) svg.save()