我有4个2D定义的4边凸多边形,而且我希望能够生成里面随机点。
I have a 4 side convex Polygon defined by 4 points in 2D, and I want to be able to generate random points inside it.
如果它真的简化了问题,我可以限制多边形的平行四边形,但更普遍的答案是prefered。
If it really simplifies the problem, I can limit the polygon to a parallelogram, but a more general answer is prefered.
生成随机点,直到有里面的多边形不会工作,因为它是真正的联合国predictable所花费的时间。
Generating random points until one is inside the polygon wouldn't work because it's really unpredictable the time it takes.
一个。如果你可以限制你输入平行四边形,这是很简单的:
A. If you can restrict your input to parallelogram, this is really simple:
在以两个随机数0和1之间,我们会打电话再 U
和 v
。
如果你的平行四边形是由点定义ABCD这样AB,BC,CD和DA是双方,然后把你的点作为是:
Take two random numbers between 0 and 1. We'll call then u
and v
.
If your parallelogram is defined by the points ABCD such that AB, BC, CD and DA are the sides, then take your point as being:
P = A + U * AB + B * AD
p = A + u*AB + b*AD
其中, AB
从载体到B 广告从A载体到D
where AB
is the vector from A to B and AD
the vector from A to D.
乙。现在,如果你不能,你仍然可以使用重心坐标。基本上,重心坐标对应,对于一个四,以4坐标(A,B,C,D)
,使得 A + B + C + D = 1
。然后,任何点 P
内四元可以由一个4 uple进行描述,使得:
B. Now, if you cannot, you can still use the barycentric coordinates. Basically, the barycentric coordinates correspond, for a quad, to 4 coordinates (a,b,c,d)
such that a+b+c+d=1
. Then, any point P
within the quad can be described by a 4-uple such that:
P = a A + b B + c C + d D
在你的情况,你可以得出4个随机数字和规范他们,使他们加起来为1。这会给你一个点。注意,点的分布将不会是均匀的在这种情况下。
In your case, you can draw 4 random numbers and normalize them so that they add up to 1. That will give you a point. Note that the distribution of points will NOT be uniform in that case.
℃。您还可以,其他地方提出,分解四成两个三角形,并使用半平行四边形的方法(即作为平行四边形,但你添加的条件 U + V = 1
)或者重心坐标三角形。然而,如果希望的均匀分布,具有在三角中的一个点的概率必须等于三角形由四边形的面积除以面积。
C. You can also, as proposed elsewhere, decompose the quad into two triangles and use the half-parallelogram method (i.e. as the parallelogram but you add the condition u+v=1
) or the barycentric coordinates for triangles. However, if you want uniform distribution, the probability of having a point in one of the triangle must be equal to the area of the triangle divided by the area of the quad.