def divide(self, cell):
        # create 4 new cells
        cell_nw = VariableSquareCell.create_initialized(self.rule,
                                                        self.neighbourhood,
                                                        self.cell_state_class)
        cell_ne = VariableSquareCell.create_initialized(self.rule,
                                                        self.neighbourhood,
                                                        self.cell_state_class)
        cell_sw = VariableSquareCell.create_initialized(self.rule,
                                                        self.neighbourhood,
                                                        self.cell_state_class)
        cell_se = VariableSquareCell.create_initialized(self.rule,
                                                        self.neighbourhood,
                                                        self.cell_state_class)

        cell_nw.size = cell.size / 4
        cell_ne.size = cell.size / 4
        cell_sw.size = cell.size / 4
        cell_se.size = cell.size / 4

        # position
        half_radius = cell.radius / 2

        cell_nw.position = (cell.x - half_radius, cell.y - half_radius)
        cell_nw.radius = half_radius
        cell_ne.position = (cell.x + half_radius, cell.y - half_radius)
        cell_ne.radius = half_radius
        cell_sw.position = (cell.x - half_radius, cell.y + half_radius)
        cell_sw.radius = half_radius
        cell_se.position = (cell.x + half_radius, cell.y + half_radius)
        cell_se.radius = half_radius

        # create neighbor connections
        cell_nw.neighs["south"] = set([cell_sw])
        cell_nw.neighs["east"] = set([cell_ne])
        cell_ne.neighs["south"] = set([cell_se])
        cell_ne.neighs["west"] = set([cell_nw])
        cell_sw.neighs["north"] = set([cell_nw])
        cell_sw.neighs["east"] = set([cell_se])
        cell_se.neighs["north"] = set([cell_ne])
        cell_se.neighs["west"] = set([cell_sw])

        new_cells = {}
        new_cells["north"] = [cell_nw, cell_ne]
        new_cells["east"] = [cell_ne, cell_se]
        new_cells["south"] = [cell_se, cell_sw]
        new_cells["west"] = [cell_nw, cell_sw]

        for direction, direction_neighs in cell.neighs.items():
            for direction_neigh in direction_neighs:
                for new_cell in new_cells[direction]:
                    if self.is_neighbor_with(direction_neigh, new_cell):
                        new_cell.neighs[direction].add(direction_neigh)

        # now we have new cells with correct neighs.
        # this time we have to update rest of the neighborhood
        for new_cell in [cell_nw, cell_ne, cell_sw, cell_se]:
            self.update_surrounding_neighbourhood(new_cell, [self])

        return [cell], [cell_nw, cell_ne, cell_sw, cell_se]
class VariableSquareCellSizeTestCase(unittest.TestCase):
    def setUp(self):
        self.rule = DummyRule()
        self.smallCell1 = VariableSquareCell(self.rule)
        self.smallCell2 = VariableSquareCell(self.rule)
        self.bigCell = VariableSquareCell(self.rule)
        self.bigCell.size = 4

    def test_fails_when_comparing_different_cell_sizes(self):
        self.assertFalse(self.smallCell1.sameSize(self.bigCell))

    def test_ok_when_comparing_same_sized_cells(self):
        self.assertTrue(self.smallCell1.sameSize(self.smallCell2))
class VariableSquareCellHelperMethodsTestCase(VSCTestCase):
    '''Unit test for variable square cell's helper methods'''

    def setUp(self):
        self.rule = DummyRule()
        self.cell = VariableSquareCell(self.rule)

    def test_ok_to_redirect_north_to_south(self):
        self.assertEqual('south', self.cell.reverseDirection('north'))

    def test_ok_to_redirect_east_to_west(self):
        self.assertEqual('west', self.cell.reverseDirection('east'))

    def test_ok_to_redirect_west_to_east(self):
        self.assertEqual('east', self.cell.reverseDirection('west'))

    def test_ok_to_redirect_south_to_north(self):
        self.assertEqual('north', self.cell.reverseDirection('south'))

    def test_ok_to_turn_north_to_east_direction(self):
        self.assertEqual('east', self.cell.turnRight('north'))

    def test_ok_to_turn_north_to_east_direction(self):
        self.assertEqual('south', self.cell.turnRight('east'))

    def test_ok_to_turn_north_to_east_direction(self):
        self.assertEqual('west', self.cell.turnRight('south'))

    def test_ok_to_turn_north_to_east_direction(self):
        self.assertEqual('north', self.cell.turnRight('west'))
 def create_new_cell(self, cells_to_merge):
     new_cell = VariableSquareCell.create_initialized(
         self.rule,
         self.neighbourhood,
         self.cell_state_class)
     new_cell.position = self.interpolate_center(cells_to_merge)
     new_cell.size = len(cells_to_merge) * cells_to_merge[0].size
     new_cell.radius = cells_to_merge[0].radius * 2
     return new_cell
    def create_cells(self, rule):
        cells = {}
        for x in range(0, self.width, self.resolution):
            for y in range(0, self.height, self.resolution):
                coordinates = (x + self.resolution / 2, y + self.resolution / 2)
                cells[coordinates] = VariableSquareCell.create_initialized(
                    rule,
                    self.neighbourhood,
                    self.cell_state_class)

                cells[coordinates].position = coordinates
                cells[coordinates].radius = self.resolution / 2
        return cells
 def setUp(self):
     self.rule = DummyRule()
     self.cell = VariableSquareCell(self.rule)
 def setUp(self):
     self.rule = DummyRule()
     self.smallCell1 = VariableSquareCell(self.rule)
     self.smallCell2 = VariableSquareCell(self.rule)
     self.bigCell = VariableSquareCell(self.rule)
     self.bigCell.size = 4