if (! paths[i].empty()) { double cur_x = agvs[i].getCurrentX(); double cur_y = agvs[i].getCurrentY(); Node* next_node = paths[i][0]; float speed = agvs[i].getSpeed(); float distance = sqrt(pow(next_node->x - cur_x, 2) + pow(next_node->y - cur_y, 2)); int steps = ceil(distance / (speed * 0.02)); // 计算需要移动的步数 float power_consumption = distance / 20; //_MAP[cur_x][cur_y] = 1; for (int j = 1; j <= steps; j++) { float time = j *100 / speed; // 每步所需时间 QTimer::singleShot(time, this, [&, i, next_node, cur_x, cur_y, power_consumption, j, steps]() { if (j == 1) { // 离开当前位置时将标记设为0 //_MAP[cur_x][cur_y] = 0; } float ratio = static_cast<float>(j) / steps; double new_x = cur_x + ratio * (next_node->x - cur_x); double new_y = cur_y + ratio * (next_node->y - cur_y); agvs[i].setCurrentX(new_x); agvs[i].setCurrentY(new_y); std:: cout <<"AGV"<<"("<< new_x << "," << new_y << ")" << endl; // 更新电量 agvs[i].setpower(agvs[i].power_ - power_consumption / steps); this->update(); // 在窗口中重绘 }); } },爲什麽輸出的currentx和y還會有整數
时间: 2024-01-27 20:05:23 浏览: 55
org.Hs.eg.db_3.5.0.tar.gz下载一直报错
这是因为在计算新的坐标时,使用了浮点数运算,但是最终的坐标仍然是以double类型的形式存储的。而在输出时,cout默认只保留小数点后6位,因此看起来像是整数。如果你想输出更多的小数位数,可以使用cout的setprecision函数来设置精度,例如:
```
std::cout << std::setprecision(10) << "AGV" << "(" << new_x << "," << new_y << ")" << std::endl;
```
这将输出10位小数的坐标值。
阅读全文