diff --git a/mesa/space.py b/mesa/space.py index 4cb665f26e8..8c8957aae5d 100644 --- a/mesa/space.py +++ b/mesa/space.py @@ -562,15 +562,18 @@ def get_distance(self, pos_1, pos_2): """ Get the distance between two point, accounting for toroidal space. Args: - pos_1, pos_2: Coordinate tuples for both points. + pos_1, pos_2: Coordinate tuples for both points. """ pos_1 = np.array(pos_1) pos_2 = np.array(pos_2) + dist_1 = np.linalg.norm(pos_1 - pos_2) if self.torus: pos_1 = (pos_1 - self.center) % self.size pos_2 = (pos_2 - self.center) % self.size - return np.linalg.norm(pos_1 - pos_2) + dist_2 = np.linalg.norm(pos_1 - pos_2) + + return min(dist_1, dist_2) def torus_adj(self, pos): """ Adjust coordinates to handle torus looping. diff --git a/tests/test_space.py b/tests/test_space.py index 9622b591c52..0874e80f300 100644 --- a/tests/test_space.py +++ b/tests/test_space.py @@ -42,6 +42,10 @@ def test_distance_calculations(self): pos_3 = (-30, -20) assert self.space.get_distance(pos_1, pos_3) == 10 + pos_4 = (20, -5) + pos_5 = (20, -15) + assert self.space.get_distance(pos_4, pos_5) == 10 + def test_heading(self): pos_1 = (-30, -30) pos_2 = (70, 20)