def draw_maze(img_file, no_wallss, img_size=400, animate=True, levels=4, side_length=50): """ Draws maze. """ def draw_triangle(img, x=0, y=0, side_length=50, drop_ab=False, drop_bc=False, drop_ca=False): """ Draws triangle with side @side_length. Bottom left corner is at the @x,@y coordinates. Able to not draw specific side of triangle by setting @drop_* to True. """ ax = x ay = y bx = x + side_length by = y cx = x + side_length / 2 cy = y - (math.sqrt(3) / 2) * side_length if not drop_ab: img.add_line(ax, ay, bx, by) if not drop_bc: img.add_line(bx, by, cx, cy) if not drop_ca: img.add_line(cx, cy, ax, ay) img = SVGImage(img_size, img_size, center_origin=False, show_borders_and_origin=False, animate=animate, animation_speed=0.05) x = img_size / 2 - side_length / 2 y = (math.sqrt(3) / 2) * side_length current_node_id = 1 # node id of the triangle we are drawing for level in range(levels + 1): current_x = x for triangle in range(level + 1): # draw only walls that are not in @no_walls drop_ab, drop_bc, drop_ca = False, False, False for wall in no_walls: node_id, neighbor_id = wall if node_id == current_node_id: if node_id == neighbor_id + 1: # dropping wall to the left (ac) drop_ca = True if node_id == neighbor_id - 1: # dropping wall to the right (bc) drop_bc = True if node_id + 1 < neighbor_id: # dropping wall to the bottom (ab) drop_ab = True draw_triangle(img, x=current_x, y=y, side_length=side_length, drop_ab=drop_ab, drop_bc=drop_bc, drop_ca=drop_ca) current_x += side_length if triangle < level: current_node_id += 2 x -= side_length / 2 y += (math.sqrt(3) / 2) * side_length current_node_id += 1 img.save(img_file)
def draw_triangulation(): img = SVGImage(IMG_WIDTH, IMG_HEIGHT, center_origin=False, show_borders_and_origin=True, animate=True, animation_speed=ANIMATION_SPEED) def generate_points(number): points = [] for i in range(number): x = get_random_number(BUFFER, IMG_WIDTH - BUFFER) y = get_random_number(BUFFER, IMG_HEIGHT - BUFFER) points.append((x, y)) return points points = generate_points(POINTS_NUM) #points = [(200, 200), (400, 150), (500, 400), (450, 700), (300, 600)] #points = [(200, 200), (400, 150), (500, 400), (450, 700)] #points = [(341, 518), (653, 605), (363, 476), (676, 308)] print(points) def shorten_line(line, step=10): x1, y1, x2, y2 = list(line) if x2 > x1: x = x2 - x1 x1 += x / step x2 -= x / step else: x = x1 - x2 x1 -= x / step x2 += x / step if y2 > y1: y = y2 - y1 y1 += y / step y2 -= y / step else: y = y1 - y2 y1 -= y / step y2 += y / step return (x1, y1, x2, y2) def intersect_some_collected(line, collected): for i in collected: a = shorten_line(i) b = shorten_line(line) if intersect(a, b): print("III", line, i) return True return False def get_lines(points): collected = [] for point1 in points: for point2 in points: if point1 == point2: continue if point1 + point2 in collected: continue if point2 + point1 in collected: continue line = point1 + point2 if not intersect_some_collected(line, collected): collected.append(line) print(collected) return collected lines = get_lines(points) """ lines = [ (200, 200, 450, 700)] x = (300, 600, 400, 150) print(intersect_some_collected(x, lines)) print(intersect(lines[0], x)) img.add_line(*x, width=LINE_WIDTH*4) img.add_circle(330.7692307692308, 461.53846153846155, POINTS_RADIUS, fill="red") """ ## draw points for point in points: img.add_circle(point[0], point[1], POINTS_RADIUS, color="black", fill="red") ## draw lines for line in lines: img.add_line(*line, width=LINE_WIDTH) img.save("img/triangulation_animate.svg")
def draw_rectangle(): # non-abstracted code for rectangle rect_img = SVGImage(W, H, center_origin=True, show_borders_and_origin=False, animate=False, animation_speed=0.05) x1 = -HEIGHT y2 = HEIGHT while x1 <= HEIGHT: rect_img.add_line(x1, HEIGHT, HEIGHT, y2) x1 += 8 y2 -= 8 x1 = HEIGHT y2 = HEIGHT while -x1 <= HEIGHT: rect_img.add_line(x1, HEIGHT, -HEIGHT, y2) x1 -= 13 y2 -= 13 x1 = -HEIGHT y2 = HEIGHT while x1 <= HEIGHT: rect_img.add_line(x1, -HEIGHT, -HEIGHT, y2) x1 += 21 y2 -= 21 x1 = -HEIGHT y2 = -HEIGHT while x1 <= HEIGHT: rect_img.add_line(x1, -HEIGHT, HEIGHT, y2) x1 += 34 y2 += 34 rect_img.save("img/test_svg_rect.svg")
""" Tomas Meszaros Multiple Reduction Copy Machine """ import lingebra from svglib import SVGImage from pprint import pprint as pp img = SVGImage(800, 800, center_origin=True, show_borders_and_origin=True, animate=False, animation_speed=0.01) a = {"lines": [[0,0,100,0], [100,0,100,100], [100,100,0,100], [0,100,0,0]], "attractor": [0, 0] } b = {"lines": [[100,100,200,100], [200,100,200,200], [200,200,100,200], [100,200,100,100]], "attractor": [100, 100] } group_set = [] init_group = [a, b] group_set.append(init_group) sx = 0.9 sy = 0.9 iterations = 30 pp(group_set) for _ in range(iterations): new_group_set = [] while len(group_set) > 0: