Point: add constructor ability to init from polar or cart

Signed-off-by: HeshamTB <hishaminv@gmail.com>
This commit is contained in:
HeshamTB 2022-02-14 16:14:52 +03:00
parent dc42010f0b
commit 6b3b957257
Signed by: Hesham
GPG Key ID: 74876157D199B09E
2 changed files with 18 additions and 7 deletions

View File

@ -6,10 +6,20 @@ class Point:
IDX_X = 0
IDX_Y = 1
FROM_POLAR = 0
FROM_CARTESIAN = 1
def __init__(self, x, y):
self._x = x
self._y = y
def __init__(self, cord1, cord2, mkfrom:int):
if mkfrom == self.FROM_CARTESIAN:
self._x = cord1
self._y = cord2
elif mkfrom == self.FROM_POLAR:
self._x = cord1 * math.cos(deg_to_rad(cord2))
self._y = cord1 * math.sin(deg_to_rad(cord2))
self._r = cord1
self._theta = cord2
else:
raise IncorrectCordFormat(f"mkfrom option \"%d\" is unknown" % mkfrom)
def get_cartesian(self) -> tuple:
return self.get_x(), self.get_y()
@ -44,8 +54,8 @@ class Point:
class PointWithID(Point):
IDX_WALLID = 3
def __init__(self, x, y, wall_id):
super().__init__(x, y)
def __init__(self, x, y, wall_id, mkfrom:int):
super().__init__(x, y, mkfrom)
self._wall_id = wall_id
def get_wallid(self):
@ -95,7 +105,7 @@ class StraightLine:
# 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() + self.get_m())
y = self.get_m()*x + self.get_b() # Inline to reduce overhead
return Point(x, y)
return Point(x, y, Point.FROM_CARTESIAN)
def intercept_list(self, lines) -> list:
# May return a sorted list based on distance
@ -104,6 +114,7 @@ class StraightLine:
class WallIDMismatch (RuntimeError): ...
class IncorrectCordFormat (RuntimeError): ...
# Helper methods
def polar_to_cart(r, theta_deg) -> tuple:

View File

@ -48,7 +48,7 @@ def read_csv(filename: str, remove_header=False) -> list[PointWithID]:
for i, row in enumerate(reader):
if remove_header and i == 0: continue # Skip header
# Assumes wall_id,r,theta
point_tmp = point_from_polar(float(row[1]), float(row[2]), row[0])
point_tmp = PointWithID(float(row[1]), float(row[2]), row[0], PointWithID.FROM_POLAR)
tmp_data.append(point_tmp)
return tmp_data