From b786aca5171743bd517e0d8e7908af5b62bdd4da Mon Sep 17 00:00:00 2001 From: Chris Mackey Date: Tue, 6 Jul 2021 10:08:38 -0400 Subject: [PATCH] style(pointvector): Support is_equivalent for Vectors We might as well us inheritance here. --- ladybug_geometry/geometry2d/pointvector.py | 32 +++++++++---------- ladybug_geometry/geometry3d/pointvector.py | 36 ++++++++++++---------- 2 files changed, 35 insertions(+), 33 deletions(-) diff --git a/ladybug_geometry/geometry2d/pointvector.py b/ladybug_geometry/geometry2d/pointvector.py index 66b15391..e961df3b 100644 --- a/ladybug_geometry/geometry2d/pointvector.py +++ b/ladybug_geometry/geometry2d/pointvector.py @@ -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 @@ -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', diff --git a/ladybug_geometry/geometry3d/pointvector.py b/ladybug_geometry/geometry3d/pointvector.py index 97925f74..df86391a 100644 --- a/ladybug_geometry/geometry3d/pointvector.py +++ b/ladybug_geometry/geometry3d/pointvector.py @@ -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 @@ -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',