Пример #1
0
def addGrid(im, size, side=20):
    # draws the grid

    # grid
    for i in xrange(size - 1):
        A = [side * (i + 1), 0]
        B = [side * (i + 1), size * side]

        im.add( im.line(start  = A,\
                        end    = B,\
                        stroke = 'rgb(50,50,50)'))
        im.add( im.line(start  = A[::-1],\
                        end    = B[::-1],\
                        stroke = 'rgb(50,50,50)'))

    # outline
    corners = array([[0, 0], [1, 0], [1, 1], [0, 1]])
    for i in xrange(4):
        corners[i] *= size * side

    for line in connect_points(corners):
        im.add( im.line(start  = line[:2],\
                        end    = line[2:],\
                        stroke = 'black'))

    return
Пример #2
0
    def draw_maze(self):

        im = svgwrite.drawing.Drawing()

        # draw walls/lines
        linewidth = str(self.side / 12.)
        for line in self.lines:
            im.add( im.line(start = line[:2],\
                            end   = line[2:],\
                            style = 'stroke:black; stroke-width:'+ linewidth +';'))

        # draw outline
        points = [[0,0],[1,0],[1,1],[0,1]]
        for i in xrange( len(points) ):
            points[i] = array( points[i] ) * (self.size * self.side)
        lines = connect_points( points )
        for line in lines:
            im.add( im.line(start = line[:2],\
                            end   = line[2:],\
                            style = 'stroke:black; stroke-width:'+ linewidth +';'))            


        im.saveas('img/'+ self.filename +'.svg')

        return
Пример #3
0
def addGrid(im, size, side=20):
# draws the grid
    
    # grid
    for i in xrange( size - 1):
        A = [ side * (i + 1), 0]
        B = [ side * (i + 1), size * side]

        im.add( im.line(start  = A,\
                        end    = B,\
                        stroke = 'rgb(50,50,50)'))
        im.add( im.line(start  = A[::-1],\
                        end    = B[::-1],\
                        stroke = 'rgb(50,50,50)'))

    # outline
    corners = array( [[0,0], [1, 0], [1,1], [0,1]] )
    for i in xrange(4):
        corners[i] *= size * side

    for line in connect_points( corners ):
        im.add( im.line(start  = line[:2],\
                        end    = line[2:],\
                        stroke = 'black'))

    return
Пример #4
0
def Triangles(turtle, side=20, step=20, n=5):

    for i in xrange(n):
        x = side / 2 - i * step / tan(pi / 6)
        y1 = step * i
        y2 = -(side**2 - side**2 / 4)**0.5 - i * step / sin(pi / 6)

        points = [[-x, y1], [x, y1], [0, y2]]

        for line in connect_points(points):
            turtle.line(line)

    return
Пример #5
0
def Triangles(turtle, side=20, step=20, n=5):

    for i in xrange(n):
        x  = side / 2 - i * step / tan(pi / 6)
        y1 = step * i
        y2 = -(side**2 - side**2/4)**0.5 - i * step / sin(pi / 6)


        points = [[ -x, y1],
                  [  x, y1],
                  [  0, y2 ]]

        for line in connect_points( points ):
            turtle.line( line )
        

    return
Пример #6
0
    def draw_maze(self):

        im = svgwrite.drawing.Drawing()

        # draw walls/lines
        linewidth = str(self.side / 12.)
        for line in self.lines:
            im.add( im.line(start = line[:2],\
                            end   = line[2:],\
                            style = 'stroke:black; stroke-width:'+ linewidth +';'))

        # draw outline
        points = [[0, 0], [1, 0], [1, 1], [0, 1]]
        for i in xrange(len(points)):
            points[i] = array(points[i]) * (self.size * self.side)
        lines = connect_points(points)
        for line in lines:
            im.add( im.line(start = line[:2],\
                            end   = line[2:],\
                            style = 'stroke:black; stroke-width:'+ linewidth +';'))

        im.saveas('img/' + self.filename + '.svg')

        return
Пример #7
0
def color_gif_path(maze, size, path, step_dict, side=20, type_path=''):
    # draws the path given by the DFS from ColorMaze class

    print "Creating gif from: "
    for row in maze:
        print "  " + " ".join([str(x) for x in row])

    im = svgwrite.drawing.Drawing()
    # reverse x, y because of the canvas nature
    path = [x[::-1] for x in path]

    # create lines from path and scale them
    lines = connect_points(path)[:-1]
    lines = [list(side * array(x) + 4 * [side / 2]) for x in lines]
    linewidth = side / 10
    lines = expand_lines(lines, linewidth / 2)

    # draw the rest
    addColors(im, maze, size, side)
    addGrid(im, size, side)

    # reverse x, y because of the step counter
    path = [x[::-1] for x in path]

    # add the step counter
    #steps = [[(size + 1) * side, (side * 1.3) * y] for y in xrange(len(step_dict))]
    counter_corners = dict.fromkeys(step_dict.keys(), 0)
    for i, key in enumerate(counter_corners):
        counter_corners[key] = [(size + 1) * side, (side * 1.3) * i]

    print counter_corners
    for i, key in enumerate(step_dict):
        im.add( im.rect(insert = counter_corners[ key ],\
                        size   = (side, side),\
                        stroke = 'black',\
                        fill   = COLORS[ key ]))

    count_dict = dict.fromkeys(step_dict.keys(), 0)
    #for key in count_dict:
    #    count_dict[key] = 0

    #print count_dict

    for i, last in enumerate(lines):
        # draw the lines you have passed
        for passed in lines[:i + 1]:
            im.add( im.line(start  = passed[:2],\
                            end    = passed[2:],\
                            style  = 'stroke:black; stroke-width:'+ str(linewidth) +';' ))

        # increment if step is on color
        maze_value = maze[path[i + 1][0]][path[i + 1][1]]
        if maze_value in count_dict.keys():
            count_dict[maze_value] += 1
            im.add( im.rect(insert = counter_corners[ maze_value ],\
                        size   = (side, side),\
                        stroke = 'black',\
                        fill   = COLORS[ maze_value ]))
            im.add( im.text(count_dict[maze_value],\
                            insert = counter_corners[maze_value] + array([side * 0.35, side * 0.65]),\
                            font_family = 'sans-serif',\
                            font_size = side / 2,\
                            fill = 'black'))

        index = str(i + 1).zfill(len(str(len(lines))))
        filename = 'img/color_gif_' + index + '.svg'
        print "+++ {}".format(filename)
        im.saveas(filename)

    # convert svg files to gif
    print ">>> converting to gif:"
    gifname = 'color' + str(size) + put_path_to_string(path)
    build = 'convert -delay 72 img/color_gif_*.svg img/' + gifname + '.gif'
    os.system(build)

    # do the clean up
    for f in sorted(
        [fn for fn in os.listdir('img/') if fn.startswith('color_gif_')]):
        print "--- {}".format('img/' + f)
        os.remove('img/' + f)

    return
Пример #8
0
def color_gif_path(maze, size, path, step_dict, side=20, type_path=''):
# draws the path given by the DFS from ColorMaze class
        
    print "Creating gif from: "
    for row in maze:
        print "  "+ " ".join( [str(x) for x in row])

    im = svgwrite.drawing.Drawing()
    # reverse x, y because of the canvas nature
    path = [x[::-1] for x in path]

    # create lines from path and scale them
    lines = connect_points( path )[:-1]
    lines = [list( side * array(x) + 4 * [side / 2]) for x in lines]
    linewidth = side / 10
    lines = expand_lines( lines, linewidth / 2)

    # draw the rest
    addColors(im, maze, size, side)
    addGrid(im, size, side)

    # reverse x, y because of the step counter
    path = [x[::-1] for x in path]

    # add the step counter
    #steps = [[(size + 1) * side, (side * 1.3) * y] for y in xrange(len(step_dict))]
    counter_corners = dict.fromkeys( step_dict.keys(),0 )
    for i, key in enumerate(counter_corners ):
        counter_corners[ key ] = [(size + 1) * side, (side * 1.3) * i]

    print counter_corners
    for i, key in enumerate( step_dict ):
        im.add( im.rect(insert = counter_corners[ key ],\
                        size   = (side, side),\
                        stroke = 'black',\
                        fill   = COLORS[ key ]))

    count_dict = dict.fromkeys( step_dict.keys(), 0 )
    #for key in count_dict:
    #    count_dict[key] = 0

    #print count_dict

    for i, last in enumerate( lines ):
        # draw the lines you have passed
        for passed in lines[:i + 1]:
            im.add( im.line(start  = passed[:2],\
                            end    = passed[2:],\
                            style  = 'stroke:black; stroke-width:'+ str(linewidth) +';' ))

        # increment if step is on color
        maze_value = maze[path[i + 1][0]][path[i + 1][1]]
        if maze_value in count_dict.keys():
            count_dict[ maze_value ] += 1
            im.add( im.rect(insert = counter_corners[ maze_value ],\
                        size   = (side, side),\
                        stroke = 'black',\
                        fill   = COLORS[ maze_value ]))
            im.add( im.text(count_dict[maze_value],\
                            insert = counter_corners[maze_value] + array([side * 0.35, side * 0.65]),\
                            font_family = 'sans-serif',\
                            font_size = side / 2,\
                            fill = 'black'))

        index = str(i + 1).zfill( len( str( len( lines))))
        filename = 'img/color_gif_'+ index +'.svg'
        print "+++ {}".format( filename )
        im.saveas( filename )

    
    # convert svg files to gif
    print ">>> converting to gif:"
    gifname = 'color'+ str(size) + put_path_to_string( path )
    build   = 'convert -delay 72 img/color_gif_*.svg img/'+ gifname +'.gif'
    os.system( build )

    # do the clean up
    for f in sorted( [fn for fn in os.listdir('img/') if fn.startswith('color_gif_')]):  
        print "--- {}".format( 'img/'+ f )
        os.remove( 'img/'+ f )
    
    return