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 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
| #include
#include
#include
#include
#include
#include"AStar.h"
const int LEFT = 0;
const int LEFT_UP = 1;
const int UP = 2;
const int UP_RIGHT = 3;
const int RIGHT = 4;
const int RIGHT_DOWN = 5;
const int DOWN = 6;
const int LEFT_DOWN = 7;
using namespace std;
Node::Node() :x(0), y(0), g(0), h(0), f(0)
{
}
Node::Node(int x, int y) : x(x), y(y), g(0), h(0), f(0)
{
}
void Node::setX(int x)
{
this->x = x;
}
void Node::setY(int y)
{
this->y = y;
}
void Node::setG(int g)
{
this->g = g;
}
void Node::setH(Node &goal)
{
this->h = abs(goal.x - this->x) + abs(goal.y-this->y);//横坐标差的绝对值+纵坐标差的绝对值
}
void Node::setF()
{
this->f = this->g + this->h;
}
void Node::setFather(int x, int y)
{
this->father->x = x;
this->father->y = y;
}
void Node::setFather(Node &father)
{
this->father->x = father.x;
this->father->y = father.y;
this->father->g = father.g;
this->father->h = father.h;
this->father->f = father.f;
}
void Node::setFather(Node *father)
{
this->father->x = father->x;
this->father->y = father->y;
this->father->g = father->g;
this->father->h = father->h;
this->father->f = father->f;
}
int Node::getX()
{
return x;
}
int Node::getY()
{
return y;
}
int Node::getG()
{
return g;
}
int Node::getH()
{
return h;
}
int Node::getF()
{
return f;
}
Node* Node::getFather()
{
return father;
}
void Node::addBarrier(int x, int y)
{
Node node(x,y);
closed.push_back(node);
}
void Node::addBarrier(Node barrier)
{
closed.push_back(barrier);
}
Node Node::operator=(const Node& nodeOrigin)
{
this->x = nodeOrigin.x;
this->y = nodeOrigin.y;
this->g = nodeOrigin.g;
this->h = nodeOrigin.h;
this->f = nodeOrigin.f;
this->setFather(nodeOrigin.father);
return *this;
}
bool Node::operator==(const Node& node)
{
return this->x==node.x&&this->y==node.y;
}
Node Node::createSunNode(int direction, Node goal)
{
Node sun = *this;//C++在this对象指针前面加*表示对象。
//
switch (direction)
{
case LEFT:
sun.setX(sun.getX() - 1);
break;
case LEFT_UP:
sun.setX(sun.getX() - 1);
sun.setY(sun.getY() - 1);
break;
case UP:
sun.setY(sun.getY() - 1);
sun.setG(sun.getG()+STEP);
break;
case UP_RIGHT:
sun.setX(sun.getX() + 1);
sun.setY(sun.getY() - 1);
break;
case RIGHT:
sun.setX(sun.getX() + 1);
break;
case RIGHT_DOWN:
sun.setX(sun.getX() + 1);
sun.setY(sun.getY() + 1);
break;
case DOWN:
sun.setY(sun.getY() + 1);
break;
case LEFT_DOWN:
sun.setX(sun.getX() - 1);
sun.setY(sun.getY() + 1);
break;
default:
break;
}
if (direction%2==0)
{
//上下左右直走,STEP
sun.setG(sun.getG()+STEP);
}
else
{
//对角线,STEP_DIAGONAL
sun.setG(sun.getG()+STEP_DIAGONAL);
}
sun.setH(goal);
sun.setF();
//
return sun;
}
void Node::addToOpen(Node node)
{
if (isInClosed(node))
{
return;
}
//检查是否在open
bool isInOpen = false;
deque::iterator iter;//iter是Node*类型
for (iter = open.begin(); iter != open.end();iter++)
{
if (*iter==node)
{
isInOpen = true;
break;
}
}
if (isInOpen)
{
//是否G成本更低
if (node.getG()<(iter->getG()))
{
//是,改变在open表中的这个node节点的父节点
iter->setFather(node.getFather());
//重新根据F值排序
Node temp = *iter;
open.erase(iter);
//iter不能再用了,因为删除操作使iter失效了
addToOpenWithOpenNotHave(temp);
}
else
{
//否,do nothing
}
}
else
{
addToOpenWithOpenNotHave(node);
}
}
//已经确认要添加的node不在open表中
void Node::addToOpenWithOpenNotHave(Node node)
{
//不在open表中,加入open(open表按F值从大到小排列)
if (open.size()>0)
{
for (int i = open.size() - 1; i >= 0; i--)
{
if (open[i].getF() >= node.getF())
{
//这里可能有问题
open.insert(open.begin() + i, node);
break;
}
else
{
if (i == 0)
{
open.push_front(node);
}
}
}
}
else
{
open.push_back(node);
}
}
void Node::addToClosed(Node node)
{
closed.push_back(node);
}
bool Node::isInOpen(Node node)
{
bool isInOpen = false;
deque::iterator iter;//iter是Node*类型
for (iter = open.begin(); iter != open.end(); iter++)
{
if (*iter == node)
{
isInOpen = true;
break;
}
}
return isInOpen;
}
bool Node::isInClosed(Node node)
{
bool result = false;
for (int i = 0; i {
if (closed[i] == node)
{
return true;
}
}
return result;
}
void Node::printNode()
{
cout <<"x = "<x< <<"y ="<y< <<"g = "<g< <<"h = "<h< <<"f = "<f< } |