void MainWindow::moveAgvs() { Astar astar; std::vector<std::vector<Node*>> paths(agvs.size()); // 得到agv的路綫 for (int i = 0; i < agvs.size(); i++) { if (agvs[i].getState() == false) { if (agvs[i].getLoad()){//如果是負載的狀態,則任務的起點到任務的終點 if (agvs[i].getCurrentX() == agvs[i].getEndX() && agvs[i].getCurrentY() == agvs[i].getEndY()) { agvs[i].setState(true); agvs[i].setLoad(false); tasks[i].setCompleted(2); task_to_agv(); } Node* start_node = new Node(agvs[i].getCurrentX(), agvs[i].getCurrentY()); Node* end_node1 = new Node(agvs[i].getEndX(), agvs[i].getEndY()); std::vector<Node*> path_to_end = astar.getPath(start_node, end_node1); path_to_end.erase(path_to_end.begin()); std::vector<Node*> path; path.insert(path.end(), path_to_end.begin(), path_to_end.end()); paths[i] = path;} else { //如果是空載的狀態,則行駛到任務的起點 //如果agv已經到達任務起點,變爲負載狀態 if (agvs[i].getCurrentX() == agvs[i].getStartX() && agvs[i].getCurrentY() == agvs[i].getStartY()) { agvs[i].setLoad(true); } Node* start_node = new Node(agvs[i].getCurrentX(), agvs[i].getCurrentY()); Node* end_node = new Node(agvs[i].getStartX(), agvs[i].getStartY()); std::vector<Node*> path_to_start = astar.getPath(start_node, end_node); std::vector<Node*> path; path.insert(path.end(), path_to_start.begin() + 1, path_to_start.end()); paths[i] = path;} } //模擬小車行駛 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); //std::cout << "AGV " << agvs[i].getid() << " current_x: " << agvs[i].getCurrentX() << " current_y: " << agvs[i].getCurrentY() <<std::endl; this->update(); }); } } } },for(int i=0;i<31;i++) for(int j=0;j<31;j++){ if (i<30) { // Create the nodes painter.drawLine(i*25+200+nodeSize,(j+1)*25+50+nodeSize/2,(i+1)*25+200,(j+1)25+50+nodeSize/2); } if (j <30) { painter.drawLine(i25+200+nodeSize/2,(j+1)25+50+nodeSize,i25+200+nodeSize/2,(j+2)*25+50); } },將path的路徑坐標兩點之間的綫變成黃色
时间: 2023-11-27 17:53:58 浏览: 61
Qt5主窗体程序:文本编辑器的实现(Qt5开发及实例)
您可以在 `MainWindow::paintEvent()` 函数中,遍历 `paths` 中每个 `AGV` 的路径,对于每个路径中相邻的节点,都画一条黄色的线段:
```cpp
void MainWindow::paintEvent(QPaintEvent *event) {
// ...
// 画地图边界
// ...
// 画 AGV
for (int i = 0; i < agvs.size(); i++) {
// ...
// 画 AGV 的路径
QPainterPath path;
for (int j = 0; j < paths[i].size() - 1; j++) {
Node* current_node = paths[i][j];
Node* next_node = paths[i][j+1];
path.moveTo(current_node->x*25+200+nodeSize/2, current_node->y*25+50+nodeSize/2);
path.lineTo(next_node->x*25+200+nodeSize/2, next_node->y*25+50+nodeSize/2);
}
painter.setPen(QPen(Qt::yellow, 3));
painter.drawPath(path);
}
}
```
这里使用了 `QPainterPath` 来绘制路径,其中 `moveTo()` 函数用于移动画笔到指定位置,`lineTo()` 函数用于画一条从当前位置到指定位置的线段。使用 `QPen` 来设置画笔的颜色和线宽。
阅读全文