def __init__(self, building, level, start, end): self.building = building self.level = level self.start = start self.end = end startPoint = Map.get_node_by_location_name(building, level, start) self.pos = [float(startPoint["x"]), float(startPoint["y"])] self.nextLoc = {} self.prevLoc = startPoint self.isGivingIdInstruction = True self.reachedLoc = [] self.reachedLoc.append(start) self.spokenList = [] self.spokenList.append(start)
def is_reach_location(self, location_name, x, y): """current x, and current y""" node = Map.get_node_by_location_name(self.building, self.level, location_name) return self.is_reach_node(node, x, y)
def get_next_location(self, x, y): """ params: current x, and current y get the nearest node, and based on it, decide the next location """ route = self.get_route() routeLength = len(route) # base cases: if routeLength == 1: return Map.get_node_by_location_name(self.building, self.level, route[0]) elif routeLength == 2: return Map.get_node_by_location_name(self.building, self.level, route[1]) minDist = sys.maxint minDistNode = {} routeIdxOfMinDistNode = -1 # get the nearest node idx = 0 for location_name in route: node = Map.get_node_by_location_name(self.building, self.level, location_name) distance = Map.get_distance(node["x"], x, node["y"], y) if distance < minDist: minDist = distance minDistNode = node routeIdxOfMinDistNode = idx idx += 1 # decide the next location to return if routeIdxOfMinDistNode == 0: # if the nearest node is at the start point # return next node return Map.get_node_by_location_name(self.building, self.level, route[1]) elif routeIdxOfMinDistNode == routeLength - 1: # if is at the end point # return end node return minDistNode else: # is at point between start & end # prevNode ----- minDistNode (nearest node) ----- nextNode prevNode = Map.get_node_by_location_name(self.building, self.level, route[routeIdxOfMinDistNode - 1]) nextNode = Map.get_node_by_location_name(self.building, self.level, route[routeIdxOfMinDistNode + 1]) prevDist = Map.get_distance(prevNode["x"], x, prevNode["y"], y) nextDist = Map.get_distance(nextNode["x"], x, nextNode["y"], y) if nextDist <= prevDist: # if nearer to the next node if minDistNode["nodeName"] in self.reachedLoc: return nextNode else: return minDistNode else: # nextDist > prevDist -- nearer to the previous node if self.is_reach_node(minDistNode, x, y): # already reached the nearest node # return next node return nextNode else: # else return the nearest node if minDistNode["nodeName"] in self.reachedLoc: return nextNode else: return minDistNode