gpt4 book ai didi

c++ - 用特定分布的非均匀屏幕点填充 vector

转载 作者:行者123 更新时间:2023-12-04 12:31:42 27 4
gpt4 key购买 nike

我正在尝试用特定分布的非均匀屏幕点填充 vector 。这些点代表屏幕上的一些 x 和 y 位置。在某些时候,我将在屏幕上绘制所有这些点,它们应该不均匀地分布在中心。基本上,当您靠近中心时,点的频率应该增加,屏幕的一侧是另一侧的反射(可以“镜像屏幕中心”)

我正在考虑使用某种公式(例如 y=cos(x) 在 -pi/2 和 pi/2 之间),其中生成的 y 将等于屏幕该区域中点的频率(其中 -pi/2将是屏幕的最左侧,反之亦然),但我一直在思考如何在创建要放在 vector 上的点时甚至能够应用这样的东西。注意:必须生成特定数量的点

如果上面的假设不成立,也许实现这一点的一种作弊方式是不断减少每个点之间的一些步长,但我不知道如何才能确保具体数量点到达中心。

例如

// this is a member function inside a class PointList
// where we fill a member variable list(vector) with nonuniform data
void PointList::FillListNonUniform(const int numPoints, const int numPerPoint)
{
double step = 2;
double decelerator = 0.01;

// Do half the screen then duplicate and reverse the sign
// so both sides of the screen mirror eachother
for (int i = 0; i < numPoints / 2; i++)
{
Eigen::Vector2d newData(step, 0);
for (int j = 0; j < numPerPoint; j++)
{
list.push_back(newData);
}
decelerator += 0.01f;
step -= 0.05f + decelerator;
}

// Do whatever I need to, to mirror the points ...
}

从字面上看,我们将不胜感激。我简要地研究了 std::normal_distribution,但在我看来它依赖于随机性,所以我不确定这是否是我正在尝试做的事情的好选择。

最佳答案

您可以使用一种称为拒绝抽样的方法。这个想法是你有一些参数的函数(在你的例子中有 2 个参数 xy),它代表概率密度函数。在您的 2D 情况下,您可以生成 xy 对以及表示概率 p 的变量。如果概率密度函数在坐标(即 f(x, y) > p)处较大,则添加样本,否则生成新的对。您可以像这样实现:

#include <functional>
#include <vector>
#include <utility>
#include <random>

std::vector<std::pair<double,double>> getDist(int num){

std::random_device rd{};
std::mt19937 gen{rd()};

auto pdf = [] (double x, double y) {
return /* Some probability density function */;
};

std::vector<std::pair<double,double>> ret;

double x,y,p;

while(ret.size() <= num){
x = (double)gen()/SOME_CONST_FOR_X;
y = (double)gen()/SOME_CONST_FOR_Y;
p = (double)gen()/SOME_CONST_FOR_P;

if(pdf(x,y) > p) ret.push_back({x,y});
}
return ret;
}

这是一个非常粗略的草案,但应该给出它如何工作的想法。

另一个选项(如果您想要正态分布)是 std::normal_distribution。引用页面中的示例可以这样调整:

#include <random>
#include <vector>
#include <utility>

std::vector<std::pair<double,double>> getDist(int num){

std::random_device rd{};
std::mt19937 gen{rd()};

std::normal_distribution<> d_x{x_center,x_std};
std::normal_distribution<> d_y{y_center,y_std};

while(ret.size() <= num){
ret.push_back({d_x(gen),d_y(gen)});
}

}

关于c++ - 用特定分布的非均匀屏幕点填充 vector ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68604894/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com