class DrawRoom(): def __init__(self, room = None): self.robot = Robot() self.room = room if self.room != None: while True: self.startingPosition = (round(random.uniform(self.room.minAbscissa(), self.room.maxAbscissa()), 2), round(random.uniform(self.room.minOrdinate(), self.room.maxOrdinate()), 2)) if self.room.isInside(self.startingPosition): break print "Starting from ", self.startingPosition self.positions = [] def run(self): following = False startLoopPosition = (0,0) while True: self.positions.append(self.robot.position) if self.wall_on_right(): if self.wall_forward(): self.robot.turn(90) else: following = True self.robot.forward() else: if not following: if self.wall_forward(): self.robot.turn(90) startLoopPosition = self.robot.position else: self.robot.forward() else: self.robot.turn(-90) self.robot.forward() if self.robot.position == startLoopPosition and following: self.positions.append(self.robot.position) break def plot(self, filename): plt.plot([pos[0] for pos in self.room.vertices_], [pos[1] for pos in self.room.vertices_], color="red") plt.plot([pos[0] for pos in self.positions], [pos[1] for pos in self.positions]) plt.savefig(filename) def wall_forward(self): if self.room == None: return self.robot.frontCollision() else: self.robot.forward() currentPosition = (self.startingPosition[0] + self.robot.position[0], self.startingPosition[1] + self.robot.position[1]) ret = self.room.isOutside(currentPosition) self.robot.backward() return ret def wall_on_right(self): if self.room == None: return self.robot.rightCollision() else: self.robot.turn(-90) ret = self.wall_forward() self.robot.turn(90) return ret