用于散点图的排序浮点向量(C++ / QCustomPlot)

Sorted Float Vectors for Scatter Plotting (C++ / QCustomPlot)

本文关键字:C++ QCustomPlot 向量 散点图 排序 用于      更新时间:2023-10-16

>问题:

1 - 以相同的顺序对多个浮点向量进行排序(保持对应)

2 - QCustomPlot (QCP) 图仅散点图的外边界。

(回答这两个问题中的任何一个都可以解决我的问题)

情况:

我有 3 个用于绘图的向量:

std::vector<float> x, y;
std::vector<int> hits;

生成的图是命中或未命中散点图。生成的图由 QCustomPlot 的曲线使用,最终成为圆形"涂鸦"。它只需要看起来类似于一个内部没有"涂鸦"的圆圈。我需要这个情节覆盖在另一个情节上。

我对xyhits的初始顺序没有太多控制权。

xy在传统的网格索引中排序:

x = -8, -8, -8, -8, -8,  -4, -4, -4, -4, -4, ... 8
y = -8, -4,  0,  4,  8,  -8, -4,  0,  4,  8, ... 8

hits是基于快速目标(让我们说鸟)的射程和速度,命中是否成功(让我们说鸟)。

生成的图是基于鸟作为中心参考的命中外层。

数据向量可能非常大。

方法1:我可以计算范围和角度。然后对浮点向量进行排序:按顺序对角度进行排序,以便在 QCustomPlot 绘制外部边界时没有内部"涂鸦"。但是,我需要知道如何根据对angle进行排序将相应的xy值保持在一起。

// Make range and angle vectors for sorting
std::vector<float> range, angle;
for(int i = 0; i < x.size(); ++i {
float r = sqrt(x[i]*x[i] + y[i]*y[i]);
range.push_back(r);
float a = 0;
if(y < 0)
a = -acos(x[i]/r);
else
a = acos(x[i]/r);
angle.push_back(a);
}
// Sort all vectors by ascending angle vector.
/* Do stuff here! */
// Set up boundary plot data
QVector<float> plot_x, plot_y;
for(int i = 0; i < x.size(); ++i {
if(hits[i]) {
plot_x.push_back(x[i]);
plot_y.push_back(y[i]);
}
}
// curve is a QCPCurve object already existing.
curve->addData(plot_x, plot_y); // Already sorted QVectors

方法 2:QCustomPlotcurve->addData(x, y)成员仅绘制散点图hits的"周长线"。我尝试使用QCPScatterStyle.setCustomPath,但没有成功。

提前谢谢你! -John

如果要使用某些条件对多个向量进行排序,并且所有索引都对应,请创建一个作为索引的新向量,并对其进行排序,然后使用这些索引创建新向量:

#include <cmath>
#include <QDebug>
static float calc_angle(float x, float y){
float r = sqrt(x*x + y*y);
float angle = acos(x/r);
return  y<0 ? -angle : angle;
}
int main(int argc, char *argv[])
{
std::vector<int> hits{0, 1, 2, 1, 0, 1, 2, 1, 0, 1};
std::vector<float> x{-8, -8, -8, -8, -8,  -4, -4, -4, -4, -4};
std::vector<float> y{-8, -4,  0,  4,  8,  -8, -4,  0,  4,  8};
Q_ASSERT(x.size() == y.size() && y.size() == hits.size());
std::vector<int> indexes(x.size());
std::iota(indexes.begin(), indexes.end(), 0);
std::sort(indexes.begin(), indexes.end(), [&](const int & i, const int & j) -> bool{
return calc_angle(x[i], y[i]) < calc_angle(x[j], y[i]);
});
QVector<float> plot_x, plot_y;
QVector<int> new_hits;
for(const int & index : indexes){
plot_x<<x[index];
plot_y<<y[index];
new_hits<<hits[index];
}
qDebug()<<indexes;
qDebug()<< plot_x;
qDebug()<<plot_y;
qDebug()<<new_hits;
return 0;//a.exec();
}

输出:

std::vector(8, 0, 1, 2, 3, 4, 5, 6, 7, 9)
QVector(-4, -8, -8, -8, -8, -8, -4, -4, -4, -4)
QVector(4, -8, -4, 0, 4, 8, -8, -4, 0, 8)
QVector(0, 0, 1, 2, 1, 0, 1, 2, 1, 1)