Skip to content

Commit

Permalink
Added docstrings
Browse files Browse the repository at this point in the history
  • Loading branch information
WaterNewt committed Mar 21, 2024
1 parent 5e42f2a commit 05ce891
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
26 changes: 20 additions & 6 deletions ray.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand All @@ -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)
13 changes: 11 additions & 2 deletions walls.py
Original file line number Diff line number Diff line change
@@ -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)})"

0 comments on commit 05ce891

Please sign in to comment.