Flex/Actionscript 函数计算圆内的随机点
不确定我在这里做错了什么,所以我想看看是否有人有任何想法或建议。 我正在尝试创建一个返回圆内随机点的函数。 下面的代码给了我沿圆边缘的随机点。 对我在这里做错了什么有什么想法吗?谢谢!
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要两个自由度,一个是您拥有的角度,另一个是距中心的距离:
如果您的圆以
(x0,y0)
为中心,而不是以>(0,0)
您可以这样修改:琐事:圆 只是边界线。如果您想引用边界界定的区域,请说disk。
You need two degrees of freedom, one is the angle, which you had, and the other is the distance from the center:
If you have your circle centered in
(x0,y0)
and not in(0,0)
you modify like this:Trivia: The circle is just the border line. If you want to refer to the area delimited by the border, you say disk.
在圆形坐标系中尝试随机角度和随机半径,然后转换为笛卡尔坐标系。另外,您是否注意到您的参数是 tmpRadius ,并且在您的函数中使用的是 radius ?
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 usingradius
?其他答案确实提供了一种圆内的随机点,但它们不是均匀分布的。靠近中心的区域每单位面积的点集中度高于靠近边缘的区域。
要获得均匀分布,您可以:
(a) 在圆周围的轴正方形中找到一个随机点。
测试它是否确实在圆内 (
x * x + y * y <= r * r
)。重复直到找到有效点。(b) 进行一些数学变换以获得适当分布的距离和角度。
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.
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.