-
Notifications
You must be signed in to change notification settings - Fork 0
/
point.py
87 lines (75 loc) · 2.64 KB
/
point.py
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
79
80
81
82
83
84
85
86
87
class point:
"K-D POINT CLASS"
def __init__(self,coordinate, name=None, dim=None):
""" name, dimension and coordinates """
self.name = name
if type(dim) == type(None):
self.dim = len(coordinate)
else:
self.dim = dim
if len(coordinate) == self.dim:
self.coordinate = coordinate
else:
raise Exception("INVALID DIMENSIONAL INFORMATION WHILE CREATING A POINT")
def getx(self):
if self.dim >= 1:
return self.coordinate[0]
else:
return None
def gety(self):
if self.dim >= 2:
return self.coordinate[1]
else:
return None
def getz(self):
if self.dim >= 3:
return self.coordinate[2]
else:
return None
def get(self,k):
if self.dim >= k:
return self.coordinate[k]
else:
return None
def print(self):
print("Name:",self.name,"Dim:",self.dim,"Coordinates:",self.coordinate)
return
def __eq__(self, other):
if (type(self) != type(None) and type(other) == type(None)) or (type(self) == type(None) and type(other) != type(None)):
return False
if self.dim == other.dim and self.coordinate == other.coordinate:
return True
return False
def __ne__(self, other):
if (type(self) != type(None) and type(other) == type(None)) or (type(self) == type(None) and type(other) != type(None)):
return False
if self.dim != other.dim or self.coordinate != other.coordinate:
return True
return False
def __lt__(self, other):
'''overload lesser than operator mainly for heapq'''
t = self.coordinate < other.coordinate
return t
def __gt__(self, other):
'''overload greater than operator mainly for heapq'''
t = self.coordinate > other.coordinate
return t
def copy(self):
k = point(self.coordinate, self.name, self.dim)
return k
def distance(self, other):
if self.dim != other.dim:
return None
dist = 0
for i in range(self.dim):
dist += (self.coordinate[i] - other.coordinate[i])**2
dist = dist**0.5
return dist
def in_range(self, bounds):
"""return true if a point lies within given bounds"""
if len(bounds) != self.dim:
raise Exception("DIMENSIONAL INCONSISTENCY WHILE CALLING IN_RANGE")
for i in range(self.dim):
if(not(bounds[i][0] <= self.coordinate[i] <= bounds[i][1])):
return False
return True