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 19:53:41 浏览: 64
你需要在Qt的MainWindow类中添加一个新的函数,用于绘制路径线条,并在moveAgvs函数中调用它。以下是可能的实现:
在头文件中添加:
```cpp
private:
QList<QPair<QPoint, QPoint>> paths;
QColor pathColor = Qt::yellow;
```
在构造函数中初始化:
```cpp
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
// ...
// Initialize paths list with empty lines
for (int i = 0; i < agvs.size(); ++i) {
paths.append(QPair<QPoint, QPoint>(QPoint(), QPoint()));
}
}
```
添加新函数:
```cpp
void MainWindow::paintEvent(QPaintEvent *event)
{
QMainWindow::paintEvent(event);
// Draw paths
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
painter.setPen(QPen(pathColor, 3));
for (int i = 0; i < paths.size(); ++i) {
const QPair<QPoint, QPoint>& path = paths.at(i);
if (!path.first.isNull() && !path.second.isNull()) {
painter.drawLine(path.first, path.second);
}
}
}
```
在moveAgvs函数中调用新函数:
```cpp
void MainWindow::moveAgvs()
{
Astar astar;
std::vector<std::vector<Node*>> paths(agvs.size());
// 得到agv的路綫
for (int i = 0; i < agvs.size(); i++) {
// ...
paths[i] = path;
// Update path line points
paths[i].insert(paths[i].begin(), new Node(agvs[i].getCurrentX(), agvs[i].getCurrentY()));
paths[i].erase(paths[i].end() - 1);
QPair<QPoint, QPoint>& pathLine = paths[i];
pathLine.first = QPoint(pathLine.first->x * 25 + 200 + nodeSize / 2, pathLine.first->y * 25 + 50 + nodeSize / 2);
pathLine.second = QPoint(pathLine.second->x * 25 + 200 + nodeSize / 2, pathLine.second->y * 25 + 50 + nodeSize / 2);
// ...
QTimer::singleShot(time, this, [&, i, next_node]() {
agvs[i].setCurrentX(next_node->x);
agvs[i].setCurrentY(next_node->y);
this->update();
});
}
}
```
注意,我们将每个路径线条的起点和终点存储在一个QPair<QPoint, QPoint>对象中,并将其添加到paths列表中。在每次更新时,我们还将路径的第一个和最后一个点与节点大小相乘并添加到偏移量中,以将它们转换为绘图坐标。最后,我们在paintEvent函数中使用QPainter绘制路径线条。
阅读全文