def test_add_radius(): """ Test for adding 2 radii.""" c1 = Circle(5) c2 = Circle(15) c3 = c1 + c2 assert c3.radius == 20 assert c3.radius != 5
def test__add__(): """ Test for alternative way of addition.""" c1 = Circle(10) c2 = Circle(20) c3 = c1 + c2 assert c3.radius == 30 assert c3.radius != 10
def initialPosition(self, low=2, up=12): "Returns List of Objects. This list is a position, i.e, a pattern of circles inside a container" initGrid = self.initialGrid # initial grid, only inside points #objects = [] numObj = random.randint(low, up) while (len(self.objects) < numObj) and initGrid: p = random.choice( initGrid) # select a point from grid, point is a tuple (x,y) idxp = initGrid.index( p) # index of selected point in the list initGrid r = random.choice(self.radii) # Choose randomly an object circle = Circle(*p, radius=r) # Set circle in selected point p #if len(self.objects): # if there are at least two cirlcles in the list #flag = True while any(circle.overlapping(obj) for obj in self.objects): #flag = any(circle.overlapping(obj) for obj in self.objects) #if flag: p = random.choice(initGrid) # select point circle.center = p # assign another center idxp = initGrid.index(p) # index of selected point initGrid.pop(idxp) # remove from init list self.removeOutside() #if circle.is_outside(self.grid): # test if circle is outside # circle.radius = min(self.radii) # try with minimum radius for verify. It is a point in the inside frontier self.objects.append(circle) #pts.append(initGrid.pop(idxp)) # remove from init list and put in list of points initGrid = set(initGrid) - set(self.grid.coverage( circle)) # forbidden points for next iteration initGrid = list(initGrid) # update grid points for obj, c in zip(self.objects, cycle(Circle.colors)): obj.color = c return self.objects
def __add__(self, other): 'Sum two Monkeys' sumObjects = self.objects + other.objects setObjects = {(obj.x, obj.y, obj.radius) for obj in sumObjects} # remove duplicates sumObjects = (Circle(x, y, r, color=c) for (x, y, r), c in zip(setObjects, cycle(Circle.colors))) return Monkey2(self.grid, self.radii, sumObjects)
def __init__(self, x, y): Automobile._automobileCount += 1 self._autoName = __class__.__name__ self._autoNum = Automobile._automobileCount self._components = dict(body=Rectangle(x, y, 8, 2), roof=Square(x - 0.6, y + 1, 2), wheel1=Circle(x - 3, y - 1, 0.5), wheel2=Circle(x + 3, y - 1, 0.5)) self._top = self._components[max( self._components, key=lambda i: self._components[i].top())].top() self._bottom = self._components[min( self._components, key=lambda i: self._components[i].bottom())].bottom() self._left = self._components[min( self._components, key=lambda i: self._components[i].left())].left() self._right = self._components[max( self._components, key=lambda i: self._components[i].right())].right() self._COM = (x, y)
def __init__(self, x, y): Automobile._automobileCount += 1 self._autoName = __class__.__name__ self._autoNum = Automobile._automobileCount self._components = dict(body=Rectangle(x, y, 20, 10), roof=Circle(x + 10, y, 5), hood=Square(x + 12.5, y - 2.5, 5), wheel1=Circle(x - 7, y - 5, 1), wheel2=Circle(x - 4, y - 5, 1), wheel3=Circle(x + 12.5, y - 5, 1)) self._top = self._components[max( self._components, key=lambda i: self._components[i].top())].top() self._bottom = self._components[min( self._components, key=lambda i: self._components[i].bottom())].bottom() self._left = self._components[min( self._components, key=lambda i: self._components[i].left())].left() self._right = self._components[max( self._components, key=lambda i: self._components[i].right())].right() self._COM = (x, y)
def initialGrid(self): 'Initial grid for selecting random initial points' coords_xInside = self.grid.xcoords[ 1: -1] # only coordinates inside container, for this reason remove the first and last element coords_yInside = self.grid.ycoords[ 1:-1] # only coordinates inside container Radius = min(self.radii) # choose a minimum radii x, y = coords_xInside[0], coords_yInside[ 0] # choose the very first point (corner point) circle = Circle(x, y, Radius) # set a circle in a choosen point while circle.is_outside(self.grid): # test if circle is outside coords_yInside = coords_yInside[1:-1] # update list coords_xInside = coords_xInside[1:-1] # update list x, y = coords_xInside[0], coords_yInside[ 0] # choose the first point, this is a corner point circle.center = x, y # set a circle in the new point return list(product(coords_xInside, coords_yInside))
def timerFired(data): if (data.stateGame == "splashScreen"): pass elif (data.stateGame == "playGame"): data.gameObject.frameNumber += 1 data.gameObject.getXYCoordinates() for key in data.gameObject.dictPlayersLocation: #value is going to be a tuple of x and y sidePlayer = determineSidePlayer(data, key) value = data.gameObject.dictPlayersLocation[key] xCord = value[0] yCord = value[1] jerseyNum = value[2] circleObj = Circle(xCord, yCord, jerseyNum, sidePlayer) data.listCircles.append(circleObj) currentPlayersOnCourt(data) if (data.pauseGame): data.timerDelay = 1 elif (data.pauseGame == False): data.timerDelay = 35
def setUp(self): self.c1 = Circle(0.0, 0.0, 1.0) self.c2 = Circle(1.0, 1.0, 1.0)
def listObjects(self): 'Creates a list of objects from a list of tuple solution [(x,x,x,...,x,x),(y,y,y,...,y,y),(r,r,r,...,r,r)]' list_TupleSolution = self.getSolutionsDF() return [Circle(*t) for t in list_TupleSolution]
def test_circles_sort(): """ Test sorting.""" circles = [ Circle(6), Circle(7), Circle(8), Circle(4), Circle(0), Circle(2), Circle(3), Circle(5), Circle(9), Circle(1) ] assert Circle(6) < Circle(8) assert Circle(3) < Circle(5) assert Circle(2) > Circle(1) assert Circle(9) > Circle(7)
def test_radius_reset(): c = Circle(4) c.radius = 10 assert c.radius == 10 assert c.diameter == 20
def test_diameter(): c = Circle(4) assert c.diameter == 8 assert c.diameter != 10
def test__eq__(): """ Test for 'equal to'.""" c1 = Circle(5) c2 = Circle(5) assert c1 == c2
def test_area_from_diameter(): c = Circle(7) assert c.area == pi * 7**2
def test__mul__(): """Test for increase by a factor.""" c1 = Circle(5) factor = 3 c2 = c1 * factor assert c2.radius == 15
def test_incr(): """ Test for alternative way to increase by a factor.""" c1 = Circle(12) factor = 4 c3 = c1 * factor assert c3.radius == 48
def test_area(): c = Circle(5) assert c.area == pi * 5**2 assert c.area != 80
def test__lt__(): """ Test for 'less than'.""" c1 = Circle(15) c2 = Circle(18) assert c1 < c2 assert c1 != c2
def test_init(): Circle(4)
def test_repr(): """ Test for printing formal representation of object.""" c = Circle(10.0) assert repr(c) == "Circle(10.0)"
def test_str(): """ Test for printing string, informal representation of object.""" c = Circle(5) assert str(c) == "A circle with radius: 5"
def setUp(self): #Both circles initially have the same Centre of Mass and overlap perfectly self.c1 = Circle(1.0, 1.0, 1.0) self.c2 = Circle(1.0, 1.0, 1.0)
def test__gt__(): """ Test for 'greater than'.""" c1 = Circle(10) c2 = Circle(8) assert c1 > c2 assert c1 != c2
def setUp(self): #Both shapes initially have the same Centre of Mass self.c1 = Circle(0.0, 0.0, 1.0) self.sq1 = Square(0.0, 0.0, 1.0)
def test_diameter_set(): c = Circle(10) c.diameter = 8 assert c.diameter == 8 assert c.radius == 4 assert c.radius != 10
def test_radius(): c = Circle(4) assert c.radius == 4 assert c.radius != 6
def test_area_from_diam(): c = Circle(5) assert c.area == pi * 5**2