예제 #1
0
class GridWorld(object):
    """Describes a GridWorld (the build-plate-world that the printer exists in.
    
    A gridworld can function as simply a drawing canvas. We can also set an ``ideal_grid'', which is an aditional Grid object (often constructed from a given gridfile path) which specifies a ``goal'' for the world (whatever that means for the particular implemention)."""

    def __init__(self,wid,hi,scale):
        """Construct a blank GridWorld with the given dimensions and the given pixel scale for the cells"""
        self.grid = Grid(wid, hi,scale)

    def set_ideal_grid(self, grid):
        """Set the ideal grid for the GridWorld. Allows a user to keep track of the ideal and actual gridworld and query for the difference between them"""
        self.ideal_grid = grid

    def get_starting_position(self):
        if self.ideal_grid.starting_point:
            return self.ideal_grid.starting_point
        else:
            assert(1 == 2) #no ideal grid defined. an ideal grid can be defined for the gridworld with set_ideal_grid

    def inbounds(self,p):
        """Checks if the given pixel coordinates are in bounds for the gridworld"""
        return self.grid.inbounds(p)

    def width(self):
        return self.grid.width

    def height(self):
        return self.grid.height

    def gridsize(self):
        """Returns the cell scale for the GridWorld (the number of pixels making up each cell)"""
        return self.grid.gridsize

    def PenDraw(self,p):
        """Draw with the pen in the closest grid location to the pixel location (a tuple)"""
        myx,myy = self.grid.find_closest_gridloc(p)
        self.grid.set_loc_val(myx, myy, 1)

    #shouldn't need these for now -- might be useful later?
    def distance(self,startp,endp):
            (endx,endy) = endp
            (startx,starty) = startp
            return math.sqrt(pow(endx - startx,2) + pow(endy - starty,2))
                              
    def manhattan(self,startp,endp):
            (endx,endy) = endp
            (startx,starty) = startp
            '''given two points, return manhattan distance of two points'''
            return (abs(endx - startx) + abs(endy - starty))
    
    def estimate_distance(self,start,end):
             #return self.manhattan(start,end)
             return self.distance(start,end)
예제 #2
0
class GridTests(unittest.TestCase):
    def setUp(self):
        self.grid = Grid(10, 10, 5)

    def test_init_from_file(self):
        grid = Grid(scale=7, path='test/resources/example')
        self.assertEqual(grid.grid, [ [0,1,0,1,0,1,0,1,0,1], [1,0,1,0,1,0,1,0,1,0], [1,1,1,1,1,1,1,1,1,1], [0,1,0,1,0,1,0,1,0,1] ])

    def test_value_changes(self):
        self.grid.set_loc_val(3, 3, 1)
        assert self.grid.val_at(3, 3) == 1

        self.grid.set_loc_val(1, 5, 3)
        assert self.grid.val_at(1, 5) == 3
예제 #3
0
파일: gridworld.py 프로젝트: fcjr/evofab
class GridWorld(object):

        filled = 1
        both_empty = 0
        need_fill = 1
        both_full = 2
        wrong_fill = 3

        def __init__(self,wid,hi,scale):
            self.grid = Grid(wid, hi,scale)

        def set_ideal_grid(self, grid):
            self.ideal_grid = grid

        def inbounds(self,p):
            return self.grid.inbounds(p)

        def width(self):
            return self.grid.width

        def height(self):
            return self.grid.height

        def gridsize(self):
            return self.grid.gridsize

        def PenDraw(self,p):
                myx,myy = self.grid.find_closest_gridloc(p)
                self.grid.set_loc_val(myx, myy, self.filled)

        #shouldn't need these for now -- might be useful later?
        def distance(self,startp,endp):
                (endx,endy) = endp
                (startx,starty) = startp
                return math.sqrt(pow(endx - startx,2) + pow(endy - starty,2))
                                  
        def manhattan(self,startp,endp):
                (endx,endy) = endp
                (startx,starty) = startp
                '''given two points, return manhattan distance of two points'''
                return (abs(endx - startx) + abs(endy - starty))
        
        def estimate_distance(self,start,end):
                 #return self.manhattan(start,end)
                 return self.distance(start,end)