-
Notifications
You must be signed in to change notification settings - Fork 0
/
SearchNode.h
48 lines (42 loc) · 1.05 KB
/
SearchNode.h
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
#pragma once
#include <iostream>
typedef std::pair<int, int> Position;
class SearchNode
{
public:
SearchNode* prevNode; // Tell us the node, where we arrived to this node (previous/parent node)
Position pos; // x and y position in the map
float G; // Actual distance from the beginning point (A)
float H; // Estimeted heuristic distance to end point (B)
float F; // G + H
public:
SearchNode(const Position ¤tPosition, float _H, float deltaG, SearchNode *prev)
: prevNode(0), pos(currentPosition), G(0), H(_H)
{
resetPrev(prev, deltaG);
}
// Calculate total G for this node from previous node G value. Also calculates new F from new G and H.
void resetPrev(SearchNode *prev, float deltaG)
{
prevNode = prev;
if (prev == 0)
{
G = 0.0f;
}
else
{
G = deltaG + prev->G;
}
F = H + G;
}
// Returns approximation of the total cost (F)
float distance() const
{
return F;
}
// Less-than function for sorting
static bool lessThan(SearchNode *n1, SearchNode *n2)
{
return n1->distance() > n2->distance();
}
};