Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions mesa/space.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 4 additions & 0 deletions tests/test_space.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down