def get_ground_direction(self): ''' In future classes, this function can be altered to return a preferred direction, but currently it only returns a random feasable direction if no direction is assigned for the robot (self.ground_direction) ''' def random_direction(): ''' Returns a random, new location (direction) ''' # obtain a random direction direction = (random.uniform(-1 * self.step, self.step), random.uniform( -1 * self.step, self.step), 0) # The they can't all be zero! if helpers.compare(helpers.length(direction),0): return random_direction() else: step = helpers.scale(self.step,helpers.make_unit(direction)) predicted_location = helpers.sum_vectors(step, self.location) # Check the location if helpers.check_location(predicted_location): return direction else: return random_direction() # If we have a currently set direction, check to see if we will go out of # bounds. if self.ground_direction != None: step = helpers.scale(self.step,helpers.make_unit(self.ground_direction)) predicted_location = helpers.sum_vectors(step, self.location) # We are going out of bounds, so set the direction to none and call # yourself again (to find a new location) if not helpers.check_location(predicted_location): self.ground_direction = None return self.get_ground_direction() # Here, we return the right direction else: assert self.ground_direction != None return self.ground_direction # We don't have a direction, so pick a random one (it is checked when we # pick it) else: self.ground_direction = random_direction() return self.ground_direction
def random_direction(): ''' Returns a random, new location (direction) ''' # obtain a random direction direction = (random.uniform(-1 * self.step, self.step), random.uniform( -1 * self.step, self.step), 0) # The they can't all be zero! if helpers.compare(helpers.length(direction),0): return random_direction() else: step = helpers.scale(self.step,helpers.make_unit(direction)) predicted_location = helpers.sum_vectors(step, self.location) # Check the location if helpers.check_location(predicted_location): return direction else: return random_direction()