-
Notifications
You must be signed in to change notification settings - Fork 1
/
node.cpp
56 lines (47 loc) · 1.69 KB
/
node.cpp
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
#include <climits>
#include <iostream>
#include <cmath>
#include "node.h"
using namespace std;
Node::Node(int x, int y) : Fl_Box(20 * x, 20 * y, 20, 20), x(x), y(y),
moveCost(0), totalCost(0), isObstacle(false), parent(nullptr)
{
this->color(FL_BLACK);
this->box(FL_BORDER_FRAME);
//~ cout << "node created at " << this->Fl_Widget::x() << " " << this->Fl_Widget::y() << endl;
}
int Node::getX() { return this->x; }
int Node::getY() { return this->y; }
int Node::getMoveCost() { return this->moveCost; }
int Node::getTotalCost() { return this->totalCost; }
bool Node::checkObstacle() { return this->isObstacle; }
Node* Node::getParent() { return this->parent; }
void Node::setObstacle(bool obstacle) { this->isObstacle = obstacle; }
void Node::setMoveCost(int moveCost) { this->moveCost = moveCost; }
void Node::setTotalCost(int totalCost) { this->totalCost = totalCost; }
void Node::setParent(Node* parent) { this->parent = parent; }
void Node::reset()
{
this->color(FL_BLACK);
this->parent = nullptr;
this->moveCost = INT_MAX;
this->totalCost = INT_MAX;
this->isObstacle = false;
//~ this->redraw();
}
//calculate the cost to move from current node to goal (Manhattan distance)
int Node::heuristic(int xGoal, int yGoal)
{
int manhattanDist = (abs(this->x - xGoal) + abs(this->y - yGoal));
return manhattanDist;
//~ int h1 = abs(this->x - xGoal) + abs(this->y - yGoal);
//~ int dx = abs(this->x - xGoal);
//~ int dy = abs(this->y - yGoal);
//~ int h2 = sqrt(pow(dx, 2) + pow(dy, 2));
//~ return sqrt(pow(dx, 2) + pow(dy, 2));
//~ return (h1 + h2) / 2;
}
int Node::updateTotalCost(int xGoal, int yGoal)
{
this->totalCost = this->moveCost + this->heuristic(xGoal, yGoal);
}