std::vector<QPointF> 怎么访问
时间: 2024-10-01 15:09:03 浏览: 38
`std::vector<QPointF>` 是 C++ 中 Qt 库中的一个动态数组,用于存储 `QPointF` 类型的对象。`QPointF` 是一个二维坐标点的结构体或类,通常用于图形处理和布局计算。
要访问这个向量中的元素,你可以使用下标操作符 `[]` 或者迭代器。以下是两种常见的访问方式:
1. **通过索引**:
```cpp
// 创建一个 QVector
std::vector<QPointF> points = {QPointF(0, 0), QPointF(10, 20), QPointF(20, 40)};
// 访问第一个元素 (位置0)
QPointF firstPoint = points[0];
// 访问最后一个元素 (如果没有指定大小,将访问最后一个已添加的元素)
QPointF lastPoint = points.back();
```
2. **使用迭代器**:
```cpp
// 迭代器遍历
for (const QPointF& point : points) {
// 操作每个点
qDebug() << "Point coordinates: (" << point.x() << ", " << point.y() << ")";
}
```
相关问题
std::vector<QPointF>list;//有一个点数组 qreal dMinX, dMinY, dMaxX, dMaxY;//计算出最大的x和y 最小的x和y
//首先需要初始化最小最大值
dMinX = std::numeric_limits<qreal>::max();
dMinY = std::numeric_limits<qreal>::max();
dMaxX = std::numeric_limits<qreal>::min();
dMaxY = std::numeric_limits<qreal>::min();
//遍历点数组,更新最小最大值
for(int i = 0; i < list.size(); i++){
QPointF point = list[i];
if(point.x() < dMinX){
dMinX = point.x();
}
if(point.y() < dMinY){
dMinY = point.y();
}
if(point.x() > dMaxX){
dMaxX = point.x();
}
if(point.y() > dMaxY){
dMaxY = point.y();
}
}
void MainWindow::moveAgvs_(){ timer =new QTimer(this); timer->start(100); connect(timer, &QTimer::timeout, this, &MainWindow::moveAgvs);} 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].getLoad()){//如果是負載的狀態,則任務的起點到任務的終點 if (agvs[i].getCurrentX() == agvs[i].getEndX() && agvs[i].getCurrentY() == agvs[i].getEndY()) { agvs[i].setState(true); } 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(); if (next_node->x == agvs[i].getEndX() && next_node->y == agvs[i].getEndY()) { task_to_agv(); } }); } } },增加函數,畫出小車形式路徑
可以在 `MainWindow` 类中增加一个函数来画出小车行驶的路径。具体实现如下:
```
void MainWindow::drawPaths() {
QPainter painter(this);
for (int i = 0; i < agvs.size(); i++) {
std::vector<Node*> path = paths[i];
if (!path.empty()) {
painter.setPen(QPen(Qt::green, 3));
for (int j = 0; j < path.size() - 1; j++) {
painter.drawLine(QPointF(path[j]->x, path[j]->y), QPointF(path[j+1]->x, path[j+1]->y));
}
}
}
}
```
在 `moveAgvs()` 函数中,调用 `drawPaths()` 函数来画出路径。同时,为了保证路径和小车的移动同步,需要在 QTimer 计时器的回调函数中调用 `drawPaths()` 函数。具体实现如下:
```
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].getLoad()) { // 如果是负载的状态,则任务的起点到任务的终点
if (agvs[i].getCurrentX() == agvs[i].getEndX() && agvs[i].getCurrentY() == agvs[i].getEndY()) {
agvs[i].setState(true);
}
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, [=, &paths] {
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;
paths[i].erase(paths[i].begin());
this->update();
if (next_node->x == agvs[i].getEndX() && next_node->y == agvs[i].getEndY()) {
task_to_agv();
}
drawPaths(); // 每次移动后都重新画出路径
});
}
}
}
```
阅读全文