From ac47e008785140abe585ae1759e8ebde884f60cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Enrico=20Cirac=C3=AC?= Date: Tue, 13 Feb 2024 00:57:25 +0100 Subject: [PATCH] PtsLine.py - Refactor PtsLine class: Add error handling for zero slope, transform methods into properties, and redefine as a dataclass --- PtsLine.py | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/PtsLine.py b/PtsLine.py index 20c21aa..7de9205 100644 --- a/PtsLine.py +++ b/PtsLine.py @@ -12,12 +12,12 @@ class PtsLine: """ Class to compute the equation of a line passing through two points. - Note: this class does not check if: - - the two points are the same - - the two points are aligned [Vertical or Horizontal Line] """ def __init__(self, x_pt1: float, y_pt1: float, x_pt2: float, y_pt2: float) -> None: + if x_pt1 == x_pt2 and y_pt1 == y_pt2: + raise ValueError("The input points are the same. " + "A line cannot be defined by a single point.") self.x_1 = x_pt1 self.y_1 = y_pt1 self.x_2 = x_pt2 @@ -31,8 +31,32 @@ def y_val(self, x_pt: float | np.ndarray) -> float | np.ndarray: def x_val(self, y_pt: float | np.ndarray) -> float | np.ndarray: """Return the x coordinate of the line at the given y coordinate.""" + if self.m_val == 0: + return np.inf return (y_pt - self.q_val) / self.m_val + @property + def slope(self) -> float: + return self.m_val + + @property + def intercept(self) -> float: + return self.q_val + + @property + def distance(self) -> float: + return np.sqrt((self.x_2 - self.x_1)**2 + (self.y_2 - self.y_1)**2) + + @property + def midpoint(self) -> tuple[float, float]: + return (self.x_1 + self.x_2) / 2, (self.y_1 + self.y_2) / 2 + + def is_parallel_to(self, other_line: 'PtsLine') -> bool: + return np.isclose(self.m_val, other_line.m_val) + + def is_perpendicular_to(self, other_line: 'PtsLine') -> bool: + return np.isclose(-1, self.m_val * other_line.m_val) + class PtsLineIntersect: """