for (int i = 0; i < agvs.size(); i++) { if (! paths[i].empty()) { painter.setPen(QPen(Qt::red, 2)); for (int j = 0; j < paths[i].size() - 1; j++) { painter.drawLine(paths[i][j]->x, paths[i][j]->y, paths[i][j+1]->x, paths[i][j+1]->y); } } },修改爲黨path為空時,則跳出來不運行
时间: 2023-11-28 14:47:41 浏览: 72
你可以使用 `continue` 语句来跳过当前循环迭代,直接进入下一次循环迭代。那么对于这段代码,你可以在 `if (!paths[i].empty())` 的后面添加一个 `else` 语句,并在 `else` 语句块中使用 `continue` 语句,代码如下:
```
for (int i = 0; i < agvs.size(); i++) {
if (!paths[i].empty()) {
painter.setPen(QPen(Qt::red, 2));
for (int j = 0; j < paths[i].size() - 1; j++) {
painter.drawLine(paths[i][j]->x, paths[i][j]->y, paths[i][j+1]->x, paths[i][j+1]->y);
}
} else {
continue;
}
}
```
这样,当 `paths[i]` 为空时,就会跳过内部的循环迭代。
相关问题
//輸出path for (int i = 0; i < agvs.size(); i++) { for (int j = 0; j < paths[i].size(); j++) { _MAP[paths[i][j]->x][ paths[i][j]->y] = 5; } } //模擬小車行駛 for (int i = 0; i < agvs.size(); i++) { if (! paths[i].empty()) { Node* next_node = paths[i][0]; float speed = agvs[i].getSpeed(); float distance = sqrt(pow(next_node->x - agvs[i].getCurrentX(), 2) + pow(next_node->y - agvs[i].getCurrentY(), 2)); float time = distance / speed * 1000; //node_Value[next_node->x][next_node->y] = 10; QTimer::singleShot(time, this, [&, i, next_node]() { agvs[i].setCurrentX(next_node->x); agvs[i].setCurrentY(next_node->y); this->update(); // 在窗口中重绘 }); } } //如果任務完成停止定時器 for (int i = 0; i < tasks.size(); i++) { if (tasks[i].completed != 2) { allTasksCompleted = false; break; } } if (allTasksCompleted) { timer->stop(); update(); } } },添加黨agv的current坐標輸入_MAP中,值為0
可以在第一个 for 循环中添加以下代码来实现:
```
for (int i = 0; i < agvs.size(); i++) {
_MAP[agvs[i].getCurrentX()][agvs[i].getCurrentY()] = 0;
}
```
这样就可以将所有 AGV 的当前位置对应的地图值设为 0。完整的代码如下:
```
for (int i = 0; i < agvs.size(); i++) {
for (int j = 0; j < paths[i].size(); j++) {
_MAP[paths[i][j]->x][paths[i][j]->y] = 5;
}
}
for (int i = 0; i < agvs.size(); i++) {
_MAP[agvs[i].getCurrentX()][agvs[i].getCurrentY()] = 0;
}
for (int i = 0; i < agvs.size(); i++) {
if (!paths[i].empty()) {
Node* next_node = paths[i][0];
float speed = agvs[i].getSpeed();
float distance = sqrt(pow(next_node->x - agvs[i].getCurrentX(), 2) + pow(next_node->y - agvs[i].getCurrentY(), 2));
float time = distance / speed * 1000;
QTimer::singleShot(time, this, [&, i, next_node]() {
agvs[i].setCurrentX(next_node->x);
agvs[i].setCurrentY(next_node->y);
this->update();
});
}
}
for (int i = 0; i < tasks.size(); i++) {
if (tasks[i].completed != 2) {
allTasksCompleted = false;
break;
}
}
if (allTasksCompleted) {
timer->stop();
update();
}
```
for (int i = 0; i < agvs.size(); i++) { if (!paths[i].empty()) { Node* prev_node = nullptr; for (Node* next_node : paths[i]) { if (prev_node) { painter.drawLine(prev_node->x, prev_node->y, next_node->x, next_node->y); } prev_node = next_node; } },一運行就卡死,修改一下
可以尝试加上一些调试语句来确定问题出在哪里,比如在循环中加上打印语句:
```
for (int i = 0; i < agvs.size(); i++) {
if (!paths[i].empty()) {
Node* prev_node = nullptr;
for (Node* next_node : paths[i]) {
if (prev_node) {
painter.drawLine(prev_node->x, prev_node->y, next_node->x, next_node->y);
}
prev_node = next_node;
qDebug() << "Drawing line from (" << prev_node->x << "," << prev_node->y << ") to (" << next_node->x << "," << next_node->y << ")";
}
}
}
```
这样可以看到哪一步出现了问题,进而进行调整。另外,如果是卡死的话,也可以考虑加上一些限制条件,比如最多只画几百个线段,或者加上一个定时器,每隔一段时间才画一条线段,防止一次性画太多导致卡死。
阅读全文