Skip to content

Commit

Permalink
fix(pointvector): Support is_equivalent for Vectors
Browse files Browse the repository at this point in the history
We might as well us inheritance here.
  • Loading branch information
chriswmackey committed Jul 6, 2021
1 parent 5ad6f4e commit 0ca574a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 33 deletions.
32 changes: 16 additions & 16 deletions ladybug_geometry/geometry2d/pointvector.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,22 @@ def is_zero(self, tolerance):
"""
return abs(self.x) <= tolerance and abs(self.y) <= tolerance

def is_equivalent(self, other, tolerance):
"""Test whether this object is equivalent to another within a certain tolerance.
Note that if you want to test whether the coordinate values are perfectly
equal to one another, the == operator can be used.
Args:
other: Another Point2D for which geometric equivalency will be tested.
tolerance: The minimum difference between the coordinate values of two
objects at which they can be considered geometrically equivalent.
Returns:
True if equivalent. False if not equivalent.
"""
return abs(self.x - other.x) <= tolerance and \
abs(self.y - other.y) <= tolerance

def normalize(self):
"""Get a copy of the vector that is a unit vector (magnitude=1)."""
d = self.magnitude
Expand Down Expand Up @@ -367,22 +383,6 @@ def distance_to_point(self, point):
vec = (self.x - point.x, self.y - point.y)
return math.sqrt(vec[0] ** 2 + vec[1] ** 2)

def is_equivalent(self, point, tolerance):
"""Test whether this point is equivalent to another within a certain tolerance.
Note that if you want to test whether the coordinate values are perfectly
equal to one another, the == operator can be used.
Args:
point: Another Point2D for which geometric equivalency will be tested.
tolerance: The minimum difference between the coordinate values of two
points at which they can be considered geometrically equivalent.
Returns:
True if equivalent. False if not equivalent.
"""
return abs(self.x - point.x) <= tolerance and \
abs(self.y - point.y) <= tolerance

def to_dict(self):
"""Get Point2D as a dictionary."""
return {'type': 'Point2D',
Expand Down
36 changes: 19 additions & 17 deletions ladybug_geometry/geometry3d/pointvector.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,25 @@ def is_zero(self, tolerance):
return abs(self.x) <= tolerance and abs(self.y) <= tolerance and \
abs(self.z) <= tolerance

def is_equivalent(self, other, tolerance):
"""Test whether this object is equivalent to another within a certain tolerance.
Note that if you want to test whether the coordinate values are perfectly
equal to one another, the == operator can be used.
Args:
other: Another Point3D or Vector3D for which geometric equivalency
will be tested.
tolerance: The minimum difference between the coordinate values of two
objects at which they can be considered geometrically equivalent.
Returns:
True if equivalent. False if not equivalent.
"""
return abs(self.x - other.x) <= tolerance and \
abs(self.y - other.y) <= tolerance and \
abs(self.z - other.z) <= tolerance

def normalize(self):
"""Get a copy of the vector that is a unit vector (magnitude=1)."""
d = self.magnitude
Expand Down Expand Up @@ -426,23 +445,6 @@ def distance_to_point(self, point):
vec = (self.x - point.x, self.y - point.y, self.z - point.z)
return math.sqrt(vec[0] ** 2 + vec[1] ** 2 + vec[2] ** 2)

def is_equivalent(self, point, tolerance):
"""Test whether this point is equivalent to another within a certain tolerance.
Note that if you want to test whether the coordinate values are perfectly
equal to one another, the == operator can be used.
Args:
point: Another Point3D for which geometric equivalency will be tested.
tolerance: The minimum difference between the coordinate values of two
points at which they can be considered geometrically equivalent.
Returns:
True if equivalent. False if not equivalent.
"""
return abs(self.x - point.x) <= tolerance and \
abs(self.y - point.y) <= tolerance and \
abs(self.z - point.z) <= tolerance

def to_dict(self):
"""Get Point3D as a dictionary."""
return {'type': 'Point3D',
Expand Down

0 comments on commit 0ca574a

Please sign in to comment.