【发布时间】:2017-08-02 03:04:24
【问题描述】:
我在Java中有以下方法:
public static Vector2d random(Circle circle) {
// this returns a random number between 0 and Math.PI * 2
double angle = MathUtils.random(0, Math.PI * 2);
// give the point inside the unit circle
// this returns a normalized vector from a given angle
Vector2d point = new Vector2d(angle);
// however, this is only along the edge
// now add a random magnitude (because this is a normalized vector, we can just multiply it by the desired magnitude)
double magnitude = Math.random();
point = point.multiply(magnitude);
// now expand this to fit the radius
point = point.multiply(circle.getRadius());
// now translate by circleCenter
return point.add(circle.getCenter());
}
这确实会在定义的圆圈中返回一个点,但是,当您多次执行此操作并绘制点时,您可以清楚地看到大多数点将朝向中心。
这是为什么?我不明白我的数学怎么能做到这一点。
如果您希望我在情节上添加点的图像,请发表评论,如果您认为这可能会有所帮助。
【问题讨论】:
-
是的,发生这种情况的原因大致相同,即两极附近的经线比赤道附近的经线更近。一个在圆中得到均匀分布的随机点的好方法是在外接正方形中得到随机点,然后拒绝那些落在圆外的点。
-
试试这个:画一个圆圈(或者想象你正在画一个圆圈)。像切披萨一样分成 16 等份。现在,对于从中心到外边缘的每个半径,将线段分成 10 个相等的部分,并沿半径放置 10 个均匀分布的点。我想你会注意到这些点在中心附近似乎更加集中。这实际上就是您的随机化方法正在做的事情。
-
@ajb 哦,谢谢,这帮助我将其可视化。现在我明白为什么会这样了
标签: java math random geometry uniform-distribution