def update_pos_by_dist_and_dir(self, distance, direction): """ params: distance - distance went through direction - angles relative to the west (clockwise) """ dirRelativeNorth = Map.get_direction_relative_north(self.building, self.level, direction) northAt = Map.get_north_at(self.building, self.level) userDir = dirRelativeNorth + northAt # relative to map userDir = userDir % 360 if not self.nextLoc: self.nextLoc = self.get_next_location(self.pos[0], self.pos[1]) movingDir = Map.get_direction(self.pos[0], self.nextLoc["x"], self.pos[1], self.nextLoc["y"]) # relative to map relativeDir = movingDir - userDir # if relativeDir > 0, it's at user's rhs, else lhs if relativeDir > 180: relativeDir -= 360 if relativeDir < -180: relativeDir += 360 (x, y, newDir) = Map.get_direction_details(self.building, self.level, distance, direction + relativeDir) vec1X = self.pos[0] - float(self.nextLoc["x"]) vec1Y = self.pos[1] - float(self.nextLoc["y"]) vec2X = self.pos[0] + x - float(self.nextLoc["x"]) vec2Y = self.pos[1] + y - float(self.nextLoc["y"]) isSameDirection = (vec1X * vec2X + vec1Y * vec2Y > 0) isReachNextLoc = self.is_reach_node(self.nextLoc, self.pos[0] + x, self.pos[1] + y) isWentPass = not isSameDirection and not isReachNextLoc if isWentPass: self.reachedLoc.append(self.nextLoc["nodeName"]) self.update_pos(x, y)
def get_next_instruction(self, direction): """ param: direction relative to the west """ dirRelativeNorth = Map.get_direction_relative_north(self.building, self.level, direction) relativeDir, dist, nextLocNode = self.get_next_location_by_direction(dirRelativeNorth) side = "right hand side" if relativeDir >= 0 else "left hand side" if self.isGivingIdInstruction: self.isGivingIdInstruction = False nextNode = nextLocNode['nodeId'] uselessNodeNamePattern = re.compile("^P\d+$") if nextLocNode['nodeName'] not in self.spokenList and not uselessNodeNamePattern.match(nextLocNode['nodeName']): nextNode += " " + nextLocNode['nodeName'] self.spokenList.append(nextLocNode['nodeName']) return Navigation.INSTRUCTION.format(nextNode) else: self.isGivingIdInstruction = True return Navigation.INSTRUCTION_ANGLE.format(side, abs(relativeDir))