or(int i=0;i<31;i++) for(int j=0;j<31;j++) { switch(_MAP[i][j]) { case 1://画障礙物 painter.setBrush(QBrush(Qt::gray,Qt::SolidPattern)); painter.drawRect(QRect(i*25+200-(nodeSpacing-nodeSize)/2,(j+1)25+50-(nodeSpacing-nodeSize)/2,25,25)); break; case 0://画地圖 painter.setBrush(QBrush(QColor(66, 139, 230),Qt::SolidPattern)); painter.drawRect(QRect((i25+200),(j+1)25+50,nodeSize,nodeSize)); break; case 2://路線 painter.setBrush(QBrush(Qt::yellow,Qt::SolidPattern)); painter.drawRect(QRect(i25+200,(j+1)25+50,nodeSize,nodeSize)); break; case 3://agv painter.drawPixmap(i25+200-(nodeSpacing-nodeSize)/2,(j+1)25+50-(nodeSpacing-nodeSize)/2,25,25,QPixmap(":/new/prefix1/agv1.png").scaled(25,25)); case 5://agv模擬直綫路綫 // painter.setPen(QPen(Qt::yellow, 3, Qt::SolidLine)); // 设置画笔颜色、粗细和样式 // painter.drawLine(QPointF(i25+200+nodeSize/2, (j+1)*25+50+nodeSize/2), QPointF((i+1)*25+200-nodeSize/2, (j+1)25+50+nodeSize/2)); // 绘制直线 painter.setBrush(QBrush(Qt::yellow,Qt::SolidPattern)); painter.drawRect(QRect((i25+200),(j+1)25+50,nodeSize,nodeSize)); break; } if(setstar) { painter.setBrush(QBrush(Qt::red,Qt::SolidPattern)); painter.drawRect(QRect(_starx25+200-(nodeSpacing-nodeSize)/2,(_stary+1)25+50-(nodeSpacing-nodeSize)/2,25,25)); } if(settg) { painter.setBrush(QBrush(Qt::blue,Qt::SolidPattern)); painter.drawRect(QRect(_endx25+200-(nodeSpacing-nodeSize)/2,(_endy+1)25+50-(nodeSpacing-nodeSize)/2,25,25)); } if(setagv) { painter.drawPixmap(agv_x25+200-(nodeSpacing-nodeSize)/2,( agv_y+1)25+50-(nodeSpacing-nodeSize)/2,25,25,QPixmap(":/new/prefix1/agv1.png").scaled(25,25)); } } //畫出坐標之間的綫 for(int i=0;i<31;i++) { for(int j=0;j<31;j++) { if (i<30) { painter.setPen(QColor(66, 139, 230)); // 设置线条颜色 painter.drawLine(i25+200+nodeSize,(j+1)*25+50+nodeSize/2,(i+1)*25+200,(j+1)25+50+nodeSize/2); // 绘制线条 } if (j <30) { painter.setPen(QColor(66, 139, 230)); // 设置线条颜色 painter.drawLine(i25+200+nodeSize/2,(j+1)25+50+nodeSize,i25+200+nodeSize/2,(j+2)*25+50); // 绘制线条 } } },添加代碼:使兩點之間的值為nodezhi,如果nodezhi值為0,不進行繪畫,如果nodezhi為1,則在兩點直接畫出紅綫
时间: 2024-02-01 19:14:44 浏览: 75
可以在两点之间添加一个值为 nodezhi 的参数,用来表示两点之间的距离或状态,然后根据 nodezhi 的值来绘制不同的线条或者不绘制。
具体实现方式如下:
```cpp
for(int i=0;i<31;i++) {
for(int j=0;j<31;j++) {
if (i<30 && _MAP[i][j] != 0 && _MAP[i+1][j] != 0) {
// 两点之间的距离或状态值 nodezhi
int nodezhi = _MAP[i][j] + _MAP[i+1][j];
// 如果 nodezhi 值为 1,绘制红色线条
if (nodezhi == 1) {
painter.setPen(QPen(Qt::red, 3, Qt::SolidLine));
painter.drawLine(i*25+200+nodeSize/2, (j+1)*25+50+nodeSize/2, (i+1)*25+200-nodeSize/2, (j+1)*25+50+nodeSize/2);
}
// 否则,绘制蓝色线条
else {
painter.setPen(QColor(66, 139, 230));
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 && _MAP[i][j] != 0 && _MAP[i][j+1] != 0) {
// 两点之间的距离或状态值 nodezhi
int nodezhi = _MAP[i][j] + _MAP[i][j+1];
// 如果 nodezhi 值为 1,绘制红色线条
if (nodezhi == 1) {
painter.setPen(QPen(Qt::red, 3, Qt::SolidLine));
painter.drawLine(i*25+200+nodeSize/2, (j+1)*25+50+nodeSize/2, i*25+200+nodeSize/2, (j+2)*25+50);
}
// 否则,绘制蓝色线条
else {
painter.setPen(QColor(66, 139, 230));
painter.drawLine(i*25+200+nodeSize/2,(j+1)*25+50+nodeSize,i*25+200+nodeSize/2,(j+2)*25+50);
}
}
}
}
```
其中,两点之间的距离或状态值 nodezhi 是通过 `_MAP` 数组中两个相邻节点的值相加得到的。如果 nodezhi 值为 1,表示两点之间存在障碍物或者其他特殊状态,需要绘制红色线条;否则,绘制蓝色线条。
阅读全文