From be9706fef803785011ba02274743f0fc9bfc6cbe Mon Sep 17 00:00:00 2001 From: HeshamTB Date: Sun, 30 Jan 2022 18:40:59 +0300 Subject: [PATCH] basictypes: StraightLine: - Fix return type hint for __find_line() - Rename Line to StraightLine - Getters for m and b in StraightLine - intercept in StraightLine Find the intercept point of 2 Striaght lines Signed-off-by: HeshamTB --- basictypes.py | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/basictypes.py b/basictypes.py index beea2e2..0e049e4 100644 --- a/basictypes.py +++ b/basictypes.py @@ -51,7 +51,7 @@ class PointWithID(Point): def get_wallid(self): return self._wall_id -class Line: +class StraightLine: IDX_M = 0 IDX_B = 1 @@ -63,7 +63,7 @@ class Line: if not self.__matching_wall_ids(p1, p2): raise WallIDMismatch('Wall ID for points does not match') - def __find_line(self, p1:PointWithID, p2:PointWithID) -> float: + def __find_line(self, p1:PointWithID, p2:PointWithID) -> tuple: m = (p2.get_y() - p1.get_y()) / (p2.get_x() - p1.get_x()) # Using p1 to find b b = p1.get_y() - (m*p1.get_x()) @@ -74,6 +74,28 @@ class Line: def __matching_wall_ids(self, p1, p2) -> bool: if p1.get_wallid() == p2.get_wallid(): return True return False + + def get_m(self)-> float: + return self.m + + def get_b(self) -> float: + return self.b + + def get_wall_id(self) -> str: + return self.wall_id + + def intercept(self, line) -> Point: + # Basically when the two lines have the same cords (x,y) + # As a formula x = (b2 - b1)/ (m1 + m2) to find X, then apply y = mx + b on any line + x = (line.get_b() - self.get_b()) / (line.get_m() + line.get_m()) + y = self.get_m()*x + self.get_b() # Inline to reduce overhead + return Point(x, y) + + def intercept_list(self, lines) -> list: + # May return a sorted list based on distance + # Consider vectorized (SIMD) implmentaion with numpy + pass + class WallIDMismatch (RuntimeError): ...