简体   繁体   English

生成某些形式的两个值(x,y)的矢量的函数

[英]Function to generate some vectors of two values (x,y) of some forms

EDIT: I reformulate the question simply: How do I generate in C++ or Python, random points (x,y) following: a circular distribution, a square distribution, and a triangular distribution. 编辑:我简单地重新提出问题:如何在C ++或Python中生成以下随机点(x,y):圆形分布,正方形分布和三角形分布。

This is a simple code for square for example: 例如,这是一个简单的正方形代码:

def generateSquare(min, max, size):
    data = []

    for i in range(size):
        x = randint(min, max)
        y = randint(min, max)
        data += [[x, y]]

    return data

First of all, instead of storing your coordinates in a vector, you would be better off using std::pair or a custom class: 首先,最好不要使用std::pair或自定义类来将坐标存储在向量中:

struct Point
{
    int x;
    int y;
};

Then you just need to have a way of generating random points, such as 然后,您只需要一种生成随机点的方法,例如

Point randomPoint(Point const & min, Point const & max)
{
    static std::mt19937 gen;
    std::uniform_int_distribution<> distribX(min.x, max.x);
    std::uniform_int_distribution<> distribY(min.y, max.y);

    return Point{distribX(gen), distribY(gen)};
}

You can then use this generation function to fill your vector, for instance with generate_n : 然后,您可以使用此生成函数来填充矢量,例如,使用generate_n

unsigned int const nbPoints = 100;

std::vector<Point> points;

std::generate_n(back_inserter(points), nbPoints, 
    std::bind(randomPoint, Point{0, 0}, Point{1000, 1000}));

Note that this will generate random points, so you are not guaranteed to end up with a square, a triangle, etc. If you want to generate a could, you could either use a non-uniform distribution (if you know what distribution your coordinates follow) to generate your numbers, or use rejection sampling to discard points that are not in the area you want them to be. 请注意,这将生成随机点,因此不能保证以正方形,三角形等结尾。如果要生成罐,则可以使用非均匀分布(如果您知道坐标的分布是什么)跟随)生成您的数字,或使用拒绝抽样来丢弃不在您希望的区域中的点。

Generating a triangle boils down to drawing three random points. 生成三角形归结为绘制三个随机点。

To generate a square, you can draw two points, corresponding to two opposite corners of the square. 要生成一个正方形,可以绘制两个点,分别对应于正方形的两个相对角。

And so on... I don't think there is a "universal" solution that would work for any shapes. 依此类推...我认为没有适用于任何形状的“通用”解决方案。

As supplement to Luc Touraille's post. 作为Luc Touraille职位的补充。

For a square find two random points and let these two points be the two furhest apart corners of the square. 对于一个正方形,找到两个随机点,并将这两个点作为正方形的两个相距最远的角。

For a triangle find three random points and let the triangle be triangle these three points make. 对于三角形,找到三个随机点,并让三角形成为这三个点组成的三角形。

For a circle find a random point as a center for the circle and another random point, and let the distance between the two be the radius of the circle. 对于一个圆,找到一个随机点作为圆的中心,然后找到另一个随机点,并将两者之间的距离作为圆的半径。

A more general approach could be to find the center point of the figures and let the parameters (scale, rotation, etc.) be found by further randomly generated numbers. 一种更通用的方法是找到图形的中心点,然后通过进一步随机生成的数字来找到参数(比例,旋转度等)。 (I guess a bit like Rook suggests). (我想有点像Rook所说的)。

Your problem is underspecified. 您的问题未指定。

There is no such thing as a "circular distribution" or "triangular distribution". 没有“圆形分布”或“三角形分布”之类的东西。

You probably meant: a uniform distribution in the shape of a circle, rectangle, triangle. 您可能是说:圆形,矩形,三角形的均匀分布。 There even is no uniquely specified triangle... 甚至没有唯一指定的三角形...

The key point is uniform . 关键是统一

Eg a standard normal distribution in 2D may appear to be somewhat circular, but it is not exactly the shape of a circle. 例如,二维的标准正态分布可能看起来有些圆形,但它并不完全是圆形。

There is no random generator that directly produces a circle with uniform density; 没有随机生成器可以直接生成具有均匀密度的圆; at least not that I know of. 至少不是我所知道的。 The most common way is just to generate a square, and reject those points that you do not want to have . 最常见的方法是生成一个正方形,并拒绝那些您不想拥有的点

Eg generate (x,y) pairs on [0:1]x[0:1] and reject those with distance from .5,.5 larger than .5 - then you get the circle. 例如,在[0:1] x [0:1]上生成(x,y)对,并拒绝距离.5,.5大于.5的对-然后得到了圆。

If you - as other users suggested - generate a radius and a distance, then the generated points will not be uniformly distributed on the circle. 如果您(如其他用户所建议的那样)生成半径和距离,则生成的点将不会均匀分布在圆上。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 是否有一些STL函数可以获得两个C ++向量的笛卡尔积? - Is there some STL function to get cartesian product of two C++ vectors? 使用两个向量,一个像x,y,z这样的变量,一个像1,2,3这样的变量,得到像x = 1,y = 2,z = 3的等式? - Using two vectors, one with variables like x, y, z, one with values like 1, 2, 3, to make an equation like x=1, y=2, z=3? 使用提示用户输入笛卡尔平面中某些点的 xy 坐标(浮点类型)的函数 - Use function that prompts he user to input x-y coordinates (float types) of some points in a Cartesian plane Eigen 将向量与一些 NaN 值进行比较而不会丢失 NaN 值 - Eigen compare vectors with some NaN values WITHOUT losing the NaN values 大小为 3 的向量返回一些奇数的奇怪问题 - Weird issue where vectors of size 3 returns some odd values 如何用编程语言生成一些固定长度为r的相同分布的伪随机向量? - How to generate some identically distributed pseudo-random vectors with a fixed length r in a programming language? 是否所有x元素都存在于y中(排序的向量)? - Are all elements of `x` present in `y` (sorted vectors)? 在函数中编码两个向量 - Encoding two vectors in a function “ 3n”个不同的元素并找到两个值“ x &lt;y”? - `3n` distinct elements and finding two values, `x < y`? 我想通过使用 rand 和 function 生成一些数学问题 - I want to generate some mathematical questions by using rand and function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM