-
Notifications
You must be signed in to change notification settings - Fork 0
/
node.cpp
120 lines (100 loc) · 2.63 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
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include "node.h"
#include "edge.h"
#include <vector>
#include <math.h>
#include <iostream>
/*Node::Node(){
Node(0,0,0.0f);
}*/
Node::Node(Node* n){
_x=n->_x;
_y=n->_y;
_error=n->_error;
}
Node::Node(int x, int y, float error){
_x=x;
_y=y;
_error=error;
}
Node::~Node(){
}
Node* Node::maxErrorNeighbour(){
if(_edges.empty()) return (this);
Node* _maxErrorNeighbour= getNeighbourNode(_edges[0]);
for(int i=1; i < _edges.size(); i++){
if(getNeighbourNode(_edges[i])->getError() > _maxErrorNeighbour->getError()){
_maxErrorNeighbour = getNeighbourNode(_edges[i]);
}
}
return _maxErrorNeighbour;
}
Node* Node::getNeighbourNode(Edge* e){
if(this->getX()==e->getNode(0)->getX() && this->getY()==e->getNode(0)->getY()) return e->getNode(1);
else return e->getNode(0);
}
void Node::addEdge(Edge* e){
this->_edges.push_back(e);
}
float Node::getX(){
return this->_x;
}
float Node::getY(){
return this->_y;
}
float Node::getError(){
return this->_error;
}
Node & Node::operator= (const Node & other){
if (this != &other) // protect against invalid self-assignment
{
this->_x=other._x;
this->_y=other._y;
this->_error=other._error;
}
// by convention, always return *this
return *this;
}
Node* between(Node* n1, Node* n2){
return new Node((n1->getX()+n2->getX())/2,
(n1->getY()+n2->getY())/2,
0);
}
float Node::distanceWith(Node* n){
int d = sqrt(pow(this->getX()-n->getX(),2) + pow(this->getY()-n->getY(),2));
return d;
}
void Node::setClosests(vector<Node*> nodes){
//if(this->firstClosest==NULL)
this->firstClosest = new Node();
//if(this->secondClosest==NULL)
this->secondClosest = new Node();
//cout<<"nodes size = " << nodes.size() << endl;
for(int i=0; i<nodes.size();i++){
if(this->distanceWith(nodes[i])==0) continue;
if(this->distanceWith(this->firstClosest) > this->distanceWith(nodes[i])){
this->secondClosest=this->firstClosest;
this->firstClosest=nodes[i];
}else if(this->distanceWith(this->secondClosest) > this->distanceWith(nodes[i])){
this->secondClosest = nodes[i];
}
}
}
void Node::moveForward(Node* n, float coef){
this->_x += (n->getX()-this->_x)*coef;
this->_y += (n->getY()-this->_y)*coef;
}
Node* Node::getClosest(int index){
if(index == 0) return firstClosest;
else return secondClosest;
}
void Node::addError(int d){
this->_error += d;
if(this->_error < 0) this->_error = 0;
}
Node* findNode(vector<Node*> nodes, int x, int y){
if(nodes.empty())return (new Node());
for(int i =0; i<nodes.size();i++){
if(round(nodes[i]->getX()) == x && round(nodes[i]->getY()) == y)return nodes[i];
}
return (new Node(x,y));
}