AS3:不规则形状上的随机点
我有一个不规则形状的 MovieClip,如下所示:
我需要在此生成一个随机点形状。
我可以通过在边界框内生成点来使用强力,然后进行测试以查看它们是否位于不规则形状上。但是,我确信有一种更有效的方法来解决这个问题。
在不规则形状上生成随机点的最有效方法是什么?
I have a MovieClip holding an irregular shape such as this one:
I need to generate a random point on this shape.
I can use brute force by generating points within the bounding box and then hitTesting to see if they reside on the irregular shape. However, I'm sure there's a more efficient way to tackle this problem.
What is the most efficient way to generate a random point on an irregular shape?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您提到了 hitTest,但我认为您的意思是 hitTestPoint()。
如果是这样,一个函数会获取您提到的随机点,看起来有点像这样:
我在评论中暗示的另一个涉及循环遍历对象的位图数据中的非透明像素。与之前的方法相反,此方法可以确保您不会有太多重复项,但这也意味着您对创建的点数量的控制较少,并且需要额外的内存来创建位图。尽管如此,出于文档目的,这里是函数:
这是一个非常基本的测试:
HTH
You mentioned hitTest, but I assume you meant hitTestPoint().
If so, a function go get the random points you mention, would look a bit like this:
The other I hinted at in my comment involves looping through non transparent pixels in a bitmap data of your object. This method would insure you don't have many duplicates, as opposed to the previous method, but it also means, you have less control over the number of points created and there's extra memory used for creating the bitmap. Still, for documentation purposes, here is the function:
And here'a very basic test:
HTH
如果您想到一些非斑点状的形状,很明显,检查随机像素,然后重试方法并不是一个真正的好方法。与形状区域相比,边界框区域可能会很大。
您可以采取的措施来提高效率获取形状的 BitmapData 的向量。它应该包含边界框的所有像素。 更新 - 现在如果我们可以选择一个随机点,如果它不在形状内,则将其从向量中删除,那就太好了。不幸的是,向量只包含像素的颜色,而不包含隐式的位置,并且只有在我们不改变向量的长度的情况下才是正确的。由于我们不需要知道实际颜色,因此可以省略所有透明像素并将内部像素的位置存储为向量中的值。这样我们就不需要为形状的每个像素创建一个新对象(这将非常昂贵!)。
If you think of some non-blob like shapes, it's clear the check random pixel, try again method isn't really a good way. The bounding box area could be huge compared to the shape area.
What you could do to improve the effectiveness is getting a vector of the BitmapData of the shape. It should contain all pixels of the bounding box. Update - it would be nice now if we could pick a random point, and remove it from the vector if it isn't inside the shape. Unfortunately the vector only contains the pixels' colour, not the position which is implicit and only correct if we don't change the vector's length. Since we don't need to know the actual colour, we can omit all transparent pixels and store an inside pixel's position as it's value in the vector. This way we don't need to create a new object for each pixel of the shape (that would be quite expensive!).