def __init__(self, poly=None): super(Room, self).__init__() self.poly = None if poly is not None: self.poly = QtGui.QPolygon() for p in poly: self.poly.append(QtCore.QPoint(p[0], p[1])) else: # Generate a room so that the robot will be inside the room, not colliding with the walls robot = getRobotPolygon() while self.poly is None: # Generate three randomised polygons p1 = translatePolygon(getRectRoom()) p2 = translatePolygon(getRectRoom()) p3 = translatePolygon(getRectRoom()) # The room is generated as: (p1 - p2) + p3 pRes = p1 if random.randint(0, 1) == 0: pRes = pRes.subtracted(p2) if random.randint(0, 1) == 0: pRes = pRes.united(p3) # Perform an additional check to verify that # the polygon is not a degenerate one error_found = False l = pRes.toList()[:-1] for e in l: if l.count(e) > 1: error_found = True # If the polygon has passed all the checks, go ahead if error_found == False: pRes = movePolygon(pRes) pRobot = pRes.united(robot) if len(pRes) == len(pRobot): self.poly = pRes
def generateRandomCoordinates(self): # Generate a room so that the robot will be inside the room, not colliding with the walls self.robot = self.getRobotPolygon() self.poly = None while self.poly is None: # Generate three randomised polygons p1 = translatePolygon(self.getRectRoom()) p2 = translatePolygon(self.getRectRoom()) p3 = translatePolygon(self.getRectRoom()) # The room is generated as: (p1 - p2) + p3 pRes = p1 if random.randint(0, 1) == 0: pRes = pRes.subtracted(p2) if random.randint(0, 1) == 0: pRes = pRes.united(p3) # Perform an additional check to verify that # the polygon is not a degenerate one error_found = False l = pRes.toList()[:-1] for e in l: if l.count(e) > 1: error_found = True # If the polygon has passed all the checks, go ahead if error_found == False: pRes = movePolygon(pRes) pRobot = pRes.united(self.robot) if len(pRes) == len(pRobot): self.poly = pRes