From 05ce89115668218d5bf4f9ca01c0b6386a88e35b Mon Sep 17 00:00:00 2001 From: WaterNewt <134024849+WaterNewt@users.noreply.github.com> Date: Thu, 21 Mar 2024 22:12:14 +0500 Subject: [PATCH] Added docstrings --- ray.py | 26 ++++++++++++++++++++------ walls.py | 13 +++++++++++-- 2 files changed, 31 insertions(+), 8 deletions(-) diff --git a/ray.py b/ray.py index ec2ec58..569e377 100644 --- a/ray.py +++ b/ray.py @@ -2,12 +2,22 @@ class Ray: - def __init__(self, start, end): + def __init__(self, start, end) -> None: + """ + Initialize the ray line + :param start: The start position of the ray line + :param end: The end position of the ray line + """ self.start = start self.end = end self.intersecting_walls = [] - def cast(self, walls): + def cast(self, walls) -> tuple[float, float] | None: + """ + Cast the rays to the walls and check for intersection + :param walls: The walls for the ray to cast + :return: Returns position of the closest intersection. Returns None, if no intersection + """ record = float('inf') closest = None @@ -21,9 +31,7 @@ def cast(self, walls): den = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4) - if den == 0: - intersecting = False - else: + if den != 0: try: t = ((x1 - x3) * (y3 - y4) - (y1 - y3) * (x3 - x4)) / den u = -((x1 - x2) * (y1 - y3) - (y1 - y2) * (x1 - x3)) / den @@ -44,5 +52,11 @@ def cast(self, walls): return closest - def point_dist(self, px, py): + def point_dist(self, px, py) -> float: + """ + Distance between two points + :param px: The x point + :param py: The y point + :return: Returns the distance + """ return math.sqrt((self.start[0] - px) ** 2 + (self.start[1] - py) ** 2) diff --git a/walls.py b/walls.py index 299bbf4..851aee0 100644 --- a/walls.py +++ b/walls.py @@ -1,8 +1,17 @@ class Wall: - def __init__(self, start, end): + def __init__(self, start, end) -> None: + """ + Initialize a wall line + :param start: The start position of the wall line + :param end: The end position of the wall line + """ self.start = start self.end = end self.intersecting = False - def __repr__(self): + def __repr__(self) -> str: + """ + Representation magic method + :return: Returns the representation string + """ return f"{str(self.__class__.__name__)}(start={str(self.start)}, end={str(self.end)}, intersecting={str(self.intersecting)})"