C++ Qt:在QMap中查找最接近的QVector3D
我有这样一个QMap:C++ Qt:在QMap中查找最接近的QVector3D,c++,qt,qmap,C++,Qt,Qmap,我有这样一个QMap: "1" (0.183,-0.232,0.747) "2" (1.232, 1.322,-0.123) etc. 我需要一个输入是QVector3D,输出是最接近的函数 输入向量的键 例如: InputVector(0.189,-0.234,0.755) -> Output: "1" 有什么办法解决这个问题吗?只需在地图上迭代并检查距离: int getClosestKey(const QVector3D & ref, const QMap<int,
"1" (0.183,-0.232,0.747)
"2" (1.232, 1.322,-0.123) etc.
我需要一个输入是QVector3D,输出是最接近的函数
输入向量的键
例如:
InputVector(0.189,-0.234,0.755) -> Output: "1"
有什么办法解决这个问题吗?只需在地图上迭代并检查距离:
int getClosestKey(const QVector3D & ref, const QMap<int, QVector3D> & map)
{
int closestKey = -1;
double minDistance = std::numeric_limits<double>::max();
for (auto itr = map.constBegin(); itr != map.constEnd(); ++itr)
{
double d = ref.distanceToPoint(itr.value());
if (d > minDistance)
continue;
closestKey = itr.key();
minDistance = d;
}
return closestKey;
}
int getClosestKey(常量QVector3D&ref,常量QMap&map)
{
int closestKey=-1;
double minDistance=标准::数值限制::最大值();
对于(auto-itr=map.constBegin();itr!=map.constEnd();++itr)
{
双d=参考距离点(itr.value());
如果(d>思维距离)
持续
closestKey=itr.key();
思维距离=d;
}
返回关闭键;
}