-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNode.py
More file actions
68 lines (54 loc) · 1.98 KB
/
Copy pathNode.py
File metadata and controls
68 lines (54 loc) · 1.98 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
#1
import copy
import math
from numpy import array as vector
def Magnitude(x):
return (x**2).sum()**0.5
class Node:
#A node reprsenting any traversable (or non traversable) square
def __init__(self, Position):
self.Position = Position #of type vector from numpy
self.Traversable = True
self.GCost = math.inf
self.HCost = math.inf
self.FCost = math.inf
self.Neighbors = []
#For linked list
self.Parent = None
#For heap from open list
self.n = None #Index in heap
self.P = None #Parent in heap
self.L = None #Left child
self.R = None #Right child
def __repr__(self):
return f"Node({self.Position})"
def Clone(self):
#Clone node
return copy.copy(self)
#3
#Calculate G Cost
def CalcG(self, GCost = 0):
#If the node has a parent, assign the parent's position to ParentVector
ParentVector = not (self.Parent is None) and self.Parent.Position
if not (self.Parent is None):
#Distance from current node to its Parent node
GCost += Magnitude(self.Position-ParentVector)
#Repeat process for the parent node
return self.Parent.CalcG(GCost)
return GCost
def CalcH(self, TargetNode):
#Distance from self to target node
return Magnitude(self.Position-TargetNode.Position)
def GetNeighbors(self, maxX, maxY):
Neighbors = []
for i in range(3):
for j in range(3):
x, y = (self.Position[0] + i - 1), (self.Position[1] + j - 1)
if not (self.Position[0] == x and self.Position[1] == y) and x >= 0 and x <= maxX and y >= 0 and y <= maxY:
Neighbors.append(vector([x, y]))
return Neighbors
def Trace(self, path = []):
path.append(self.Position)
if not (self.Parent is None):
self.Parent.Trace(path)
return path