-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathpoint.py
More file actions
78 lines (63 loc) · 2.39 KB
/
point.py
File metadata and controls
78 lines (63 loc) · 2.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
from typing import Optional, Union
import bresenhams
from numba import njit
class point2d:
def __init__(self, x: float, y: float):
self.x: float = x
self.y: float = y
def distance(self, other: Union['point2d', tuple, list]) -> float:
return ((self[0] - other[0]) ** 2 + (
self[1] - other[1]) ** 2) ** 0.5
def as_tuple(self) -> tuple:
return (self.x, self.y)
def line_of_sight(self, other: 'point2d', grid) -> bool:
"""Returns true if there is a line of sight between self and other.
"""
if self == other:
return True
if self is None or other is None:
return False
if self.x < 0 or self.y < 0 or self.x >= grid.shape[0] or self.y >= grid.shape[1]:
return False
if other.x < 0 or other.y < 0 or other.x >= grid.shape[0] or other.y >= grid.shape[1]:
return False
if grid[self.x][self.y] == 1 or grid[other.x][other.y] == 1:
return False
intersected_points = [point2d(x, y) for x, y in
bresenhams.get_line(self.as_tuple(),
other.as_tuple())]
intersected_points.append(other)
intersected_points.append(self)
for point in intersected_points:
if point.x < 0 or point.y < 0 or point.x >= grid.shape[
0] or point.y >= grid.shape[1]:
return False
elif grid[point.x][point.y] == 1:
return False
return True
def __str__(self):
return "(" + str(self.x) + ", " + str(self.y) + ")"
def __repr__(self):
return "(" + str(self.x) + ", " + str(self.y) + ")"
def __eq__(self, other):
if not isinstance(other, point2d):
return False
return self.x == other.x and self.y == other.y
def __ne__(self, other):
return not self.__eq__(other)
def __hash__(self):
return hash((self.x, self.y))
def __getitem__(self, index):
if index == 0:
return self.x
elif index == 1:
return self.y
else:
raise IndexError("Index out of bounds")
def __setitem__(self, index, value):
if index == 0:
self.x = value
elif index == 1:
self.y = value
else:
raise IndexError("Index out of bounds")