numpy 在三角域内生成随机位置

i34xakig  于 2023-10-19  发布在  其他
关注(0)|答案(3)|浏览(120)

我想生成具有均匀分布的xy,并受到[xmin,xmax][ymin,ymax]的限制
点(x,y)应该在三角形内。
我该如何解决这样的问题?

3gtaxfhh

3gtaxfhh1#

下面是一些代码,可以在平面上的任意三角形上均匀地生成点。

import random
    
def point_on_triangle(pt1, pt2, pt3):
    """
    Random point on the triangle with vertices pt1, pt2 and pt3.
    """
    x, y = sorted([random.random(), random.random()])
    s, t, u = x, y - x, 1 - y
    return (s * pt1[0] + t * pt2[0] + u * pt3[0],
            s * pt1[1] + t * pt2[1] + u * pt3[1])

我们的想法是计算三个顶点的加权平均值,权重由单位区间[0, 1]随机分为三部分(在所有这些中断上均匀分布)。这里,xy表示单位区间的中断点,stu表示中断点之后的片段长度。然后我们使用stu作为三角形中点的重心坐标。
下面是上面的一个变体,它避免了排序的需要,而是使用绝对值调用:

def point_on_triangle2(pt1, pt2, pt3):
    """
    Random point on the triangle with vertices pt1, pt2 and pt3.
    """
    x, y = random.random(), random.random()
    q = abs(x - y)
    s, t, u = q, 0.5 * (x + y - q), 1 - 0.5 * (q + x + y)
    return (
        s * pt1[0] + t * pt2[0] + u * pt3[0],
        s * pt1[1] + t * pt2[1] + u * pt3[1],
    )

下面是一个在三角形中生成10000个点的示例用法:

pt1 = (1, 1)
pt2 = (2, 4)
pt3 = (5, 2)
points = [point_on_triangle(pt1, pt2, pt3) for _ in range(10000)]

以及从上面得到的曲线图,证明了均匀性。该图由以下代码生成:

import matplotlib.pyplot as plt
x, y = zip(*points)
plt.scatter(x, y, s=0.1)
plt.show()

下面是图片:

既然你用“numpy”标记了这个问题,这里有一个NumPy版本,可以一次生成多个样本。请注意,它使用了Python 3.5中引入的矩阵乘法运算符@,并且在NumPy >= 1.10中支持。您需要在旧版本的Python或NumPy上调用np.dot来替换它。

import numpy as np

def points_on_triangle(v, n):
    """
    Give n random points uniformly on a triangle.

    The vertices of the triangle are given by the shape
    (2, 3) array *v*: one vertex per row.
    """
    x = np.sort(np.random.rand(2, n), axis=0)
    return np.column_stack([x[0], x[1]-x[0], 1.0-x[1]]) @ v

# Example usage
v = np.array([(1, 1), (2, 4), (5, 2)])
points = points_on_triangle(v, 10000)
soat7uwm

soat7uwm2#

好吧,是时候添加另一个版本了,我想。已知的算法是在三角形中均匀采样,详见the paper by Osada et al. (2002),第4.2章。
Python代码:

import math
import random

import matplotlib.pyplot as plt

def trisample(A, B, C):
    """
    Given three vertices A, B, C, 
    sample point uniformly in the triangle
    """
    r1 = random.random()
    r2 = random.random()

    s1 = math.sqrt(r1)

    x = A[0] * (1.0 - s1) + B[0] * (1.0 - r2) * s1 + C[0] * r2 * s1
    y = A[1] * (1.0 - s1) + B[1] * (1.0 - r2) * s1 + C[1] * r2 * s1

    return (x, y)

random.seed(312345)
A = (1, 1)
B = (2, 4)
C = (5, 2)
points = [trisample(A, B, C) for _ in range(10000)]

xx, yy = zip(*points)
plt.scatter(xx, yy, s=0.2)
plt.show()

结果看起来

2006,J. H.,J. H.,& J. B.,& J. H.(2002). Shape distributions. ACM Transactions on Graphics(TOG),21(4),807-832.

2sbarzqh

2sbarzqh3#

三角区的制服?

import numpy as np

N = 10 # number of points to create in one go

rvs = np.random.random((N, 2)) # uniform on the unit square
# Now use the fact that the unit square is tiled by the two triangles
# 0 <= y <= x <= 1 and 0 <= x < y <= 1
# which are mapped onto each other (except for the diagonal which has
# probability 0) by swapping x and y.
# We use this map to send all points of the square to the same of the
# two triangles. Because the map preserves areas this will yield 
# uniformly distributed points.
rvs = np.where(rvs[:, 0, None]>rvs[:, 1, None], rvs, rvs[:, ::-1])

Finally, transform the coordinates
xmin, ymin, xmax, ymax = -0.1, 1.1, 2.0, 3.3
rvs = np.array((ymin, xmin)) + rvs*(ymax-ymin, xmax-xmin)

统一的边际?最简单的解决方案是将质量均匀地集中在(ymin,xmin)-(ymax,xmax)线上

rvs = np.random.random((N,))
rvs = np.c_[ymin + (ymax-ymin)*rvs, xmin + (xmax-xmin)*rvs]

但这不是很有趣,不是吗

相关问题