Skip to content

Commit

Permalink
perf: ContinuousSpace: Use math instead of np for get_distance
Browse files Browse the repository at this point in the history
  • Loading branch information
rht authored and tpike3 committed Mar 27, 2022
1 parent ba1784b commit 6884c9d
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions mesa/space.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
# pylint: disable=invalid-name

import itertools
import math

import numpy as np

Expand Down Expand Up @@ -890,12 +891,12 @@ def get_distance(self, pos_1: FloatCoordinate, pos_2: FloatCoordinate) -> float:
x1, y1 = pos_1
x2, y2 = pos_2

dx = np.abs(x1 - x2)
dy = np.abs(y1 - y2)
dx = abs(x1 - x2)
dy = abs(y1 - y2)
if self.torus:
dx = min(dx, self.width - dx)
dy = min(dy, self.height - dy)
return np.sqrt(dx * dx + dy * dy)
return math.sqrt(dx * dx + dy * dy)

def torus_adj(self, pos: FloatCoordinate) -> FloatCoordinate:
"""Adjust coordinates to handle torus looping.
Expand Down

0 comments on commit 6884c9d

Please sign in to comment.