C++ 用于散点打印的排序浮点向量(C+;+;/QCustomPlot)
问题: 1-按相同顺序对多个浮点向量排序(保持对应) 2-QCustomPlot(QCP)仅绘制散点图的外边界 (回答这两个问题中的任何一个都可以解决我的问题) 情况: 我有3个用于绘制的向量:C++ 用于散点打印的排序浮点向量(C+;+;/QCustomPlot),c++,qt,qcustomplot,C++,Qt,Qcustomplot,问题: 1-按相同顺序对多个浮点向量排序(保持对应) 2-QCustomPlot(QCP)仅绘制散点图的外边界 (回答这两个问题中的任何一个都可以解决我的问题) 情况: 我有3个用于绘制的向量: std::vector<float> x, y; std::vector<int> hits; 命中率基于射程和速度(比如说小鸟)是否成功命中(比如说弓箭手的箭) 由此产生的情节是以鸟为中心参照的点击的外部边界 数据向量可能非常大 方法1:我可以计算距离和角度。然后对浮点向量进
std::vector<float> x, y;
std::vector<int> hits;
命中率
基于射程和速度(比如说小鸟)是否成功命中(比如说弓箭手的箭)
由此产生的情节是以鸟为中心参照的点击的外部边界
数据向量可能非常大
方法1:我可以计算距离和角度。然后对浮点向量进行排序:按顺序对角度进行排序,以便当QCustomPlot绘制外部边界时,内部没有“涂鸦”。但是,我需要知道如何根据对角度的排序将相应的x
和y
值保存在一起
// 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
//生成用于排序的范围和角度向量
std::矢量范围,角度;
对于(int i=0;i添加数据(绘图x,绘图y);//已排序的矢量
方法2:获取QCustomPlot
曲线->添加数据(x,y)
成员,仅绘制散点图的点击数的“周界线”
。我尝试使用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();
}
你的问题的背景非常特殊,这无助于社区帮助你,请记住,我们不知道你在做什么。在你的第一个问题中,我直觉地认为你想使用一个共同的标准对n向量进行排序,例如,n向量的位置索引对应。这是否正确?如果o、 标准是什么,除此之外,我看到你们展示了向量x,e的例子,以及hit会发生什么?根据我看到的每一对x,可以与一个角度相关联,关于这个角度,你们想对x和y进行排序。我是对的。好吧,制作一个索引数组,用[0…N]填充它,制作自定义比较器,以获取浮点数组,使用比较器对索引进行排序,运行浮点数组并交换值。对于第二个问题,外边界是什么意思?是的。我想使用一个通用标准对n向量进行排序。n向量相互对应。谢谢你,eyllanesc。这正是我要找的。很抱歉,之前没有弄清楚。我应该d将其表述为一个问题。问题2本应是一个解决方案,让QCP采用1和0的网格,并在所有1周围绘制一条线。就像周长一样。排序线返回错误:预期的主表达式在['token.expected unqualified id before'bool'。另外,std::iota是什么库。我找不到它,所以它不会编译。:use
#include
,您使用的是什么IDE?您的编译器是什么?@JohnShearer如果您使用的是qtcreator enable c++11:CONFIG+=c++11
#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)