Flex/Actionscript 函数计算圆内的随机点

发布于 2024-10-02 17:17:41 字数 414 浏览 1 评论 0原文

不确定我在这里做错了什么,所以我想看看是否有人有任何想法或建议。 我正在尝试创建一个返回圆内随机点的函数。 下面的代码给了我沿圆边缘的随机点。 对我在这里做错了什么有什么想法吗?谢谢!

private function getPointInCircle(tmpRadius:int):Point {
    var x:int;
    var y:int;

    var a:Number = Math.random() * 360;
    x = radius * Math.cos(a * (Math.PI / 180));
    y = radius * Math.sin(a * (Math.PI / 180));

    trace("x: " + x + "y: " + y);
    return new Point(x, y);
}

Not sure what I'm doing wrong here so I was looking to see if anyone had any thoughts or suggestions.
I'm trying to create a function that returns a random point WITHIN a circle.
The following code gives me random points along the edge of the circle.
Any thoughts on what I'm doing wrong here? Thanks!

private function getPointInCircle(tmpRadius:int):Point {
    var x:int;
    var y:int;

    var a:Number = Math.random() * 360;
    x = radius * Math.cos(a * (Math.PI / 180));
    y = radius * Math.sin(a * (Math.PI / 180));

    trace("x: " + x + "y: " + y);
    return new Point(x, y);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

慈悲佛祖 2024-10-09 17:17:41

您需要两个自由度,一个是您拥有的角度,另一个是距中心的距离:

var d:Number = Math.random()*radius
var a:Number = Math.random()*2*Math.PI;
x = d*Math.cos(a);
y = d*Math.sin(a);

如果您的圆以 (x0,y0) 为中心,而不是以 >(0,0) 您可以这样修改:

x = x0 + d*Math.cos(a);
y = y0 + d*Math.sin(a);

琐事: 只是边界线。如果您想引用边界界定的区域,请说disk

You need two degrees of freedom, one is the angle, which you had, and the other is the distance from the center:

var d:Number = Math.random()*radius
var a:Number = Math.random()*2*Math.PI;
x = d*Math.cos(a);
y = d*Math.sin(a);

If you have your circle centered in (x0,y0) and not in (0,0) you modify like this:

x = x0 + d*Math.cos(a);
y = y0 + d*Math.sin(a);

Trivia: The circle is just the border line. If you want to refer to the area delimited by the border, you say disk.

你是暖光i 2024-10-09 17:17:41

在圆形坐标系中尝试随机角度和随机半径,然后转换为笛卡尔坐标系。另外,您是否注意到您的参数是 tmpRadius ,并且在您的函数中使用的是 radius ?

var a = Math.random() * 2 * Math.PI;
var r = Math.random() * radius;
var x = r * Math.cos(a);
var y = r * Math.sin(a);

Try a random angle and random radius in a circular coordinate system, then convert to cartesian. Also, did you notice that your parameter is tmpRadius and in your function you are using radius?

var a = Math.random() * 2 * Math.PI;
var r = Math.random() * radius;
var x = r * Math.cos(a);
var y = r * Math.sin(a);
写给空气的情书 2024-10-09 17:17:41

其他答案确实提供了一种圆内的随机点,但它们不是均匀分布的。靠近中心的区域每单位面积的点集中度高于靠近边缘的区域。

要获得均匀分布,您可以:

(a) 在圆周围的轴正方形中找到一个随机点。

x = (Math.random() * 2 - 1) * radius;
y = (Math.random() * 2 - 1) * radius;

测试它是否确实在圆内 (x * x + y * y <= r * r)。重复直到找到有效点。

(b) 进行一些数学变换以获得适当分布的距离和角度。

uniform_random = P(dist < r) = (pi r^2) / (pi radius^2) = r^2 / radius^2

r = radius * sqrt(uniform_random)

var r = radius * sqrt(Math.random());
var theta = 2 * Math.PI * Math.random();
var x = r * Math.cos(theta);
var y = r * Math.sin(theta);

The other answers do provide a sort of random point inside a circle, but they are not uniformly distributed. Regions near the center will have a higher concentration of points per unit area than regions near the rim.

To get a uniform distribution, you can either:

(a) Find a random point in the axis square around the circle.

x = (Math.random() * 2 - 1) * radius;
y = (Math.random() * 2 - 1) * radius;

Test whether it's actually in the circle (x * x + y * y <= r * r). Repeat until you find a valid point.

(b) Do a little math transformation to get appropriately distributed distance and angle.

uniform_random = P(dist < r) = (pi r^2) / (pi radius^2) = r^2 / radius^2

r = radius * sqrt(uniform_random)

var r = radius * sqrt(Math.random());
var theta = 2 * Math.PI * Math.random();
var x = r * Math.cos(theta);
var y = r * Math.sin(theta);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文