AS3:对异形随机点 [英] AS3: Random Point on Irregular Shape

查看:153
本文介绍了AS3:对异形随机点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个影片剪辑拿着一个不规则的形状像这样的:

I have a MovieClip holding an irregular shape such as this one:

我需要生成这个形状的随机点。

I need to generate a random point on this shape.

我可以通过边框内的生成点,然后hitTesting,看看它们所在的不规则形状用蛮力。不过,我敢肯定有解决这个问题的一个更有效的方法。

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?

推荐答案

您提到的hitTest,但我相信你的意思是<一个href="http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/DisplayObject.html?filter_flash=cs5&filter_flashplayer=10.2&filter_air=2.6#hitTestPoint%28%29"相对=nofollow> hitTestPoint()。

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:

function getRandomPointsInClip(target:MovieClip,numPoints:int):Vector.<Point>{
    var points:Vector.<Point> = new Vector.<Point>(numPoints,true);
    var width:Number = target.width,height:Number = target.height;
    for(var i:int =  0; i < numPoints ; i++){
        var point:Point = new Point(target.x+Math.random() * width,target.y+Math.random() * height);
        if(target.hitTestPoint(point.x,point.y,true)) points[i] = point;//is the random coord inside ?
        else i = i-1;//nope, go back one step - > retry above until it is inside
    }
    return points;
}

另外我在我的评论暗示,包括通过非透明像素在对象的位图数据循环。这种方法将确保没有很多重复,相对于previous方法,但它也意味着,你有超过创建的点的数量较少的控制和有用于创建位图额外存储器。不过,对于文档的目的,这里的功能是:

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:

function getGridPointsInClip(target:MovieClip,res:int,offset:Number = 3):Vector.<Point>{
    var points:Vector.<Point> = new Vector.<Point>();
    var x:int,y:int,alpha:int,w:int = int(target.width),h:int = int(target.height);
    var bmd:BitmapData = new BitmapData(w,h,true,0x00FFFFFF);bmd.draw(target);
    var pixels:Vector.<uint> = bmd.getVector(bmd.rect),numPixels:int = w*h;
    for(var i:int = 0; i < numPixels; i+=res) {
        x = i%bmd.width;
        y = int(i/bmd.width);
        alpha = pixels[i] >>> 24;
        if(alpha > 0) points.push(new Point(x+random(-offset,offset),y+random(-offset,offset)));
    }
    return points;
}
function random(from:Number,to:Number):Number {
    if (from >= to) return from;
    var diff:Number = to - from;
    return (Math.random()*diff) + from;
}

和here'a非常基本的测试:

And here'a very basic test:

var pts:Vector.<Point> = getRandomPointsInClip(mc,300);
//var pts:Vector.<Point> = getGridPointsInClip(mc,100,4);
for(var i:int = 0 ; i < pts.length; i++) drawCircle(pts[i].x,pts[i].y,3,0x009900);

function getRandomPointsInClip(target:MovieClip,numPoints:int):Vector.<Point>{
    var points:Vector.<Point> = new Vector.<Point>(numPoints,true);
    var width:Number = target.width,height:Number = target.height;
    for(var i:int =  0; i < numPoints ; i++){
        var point:Point = new Point(target.x+Math.random() * width,target.y+Math.random() * height);
        if(target.hitTestPoint(point.x,point.y,true)) points[i] = point;//is the random coord inside ?
        else i = i-1;//nope, go back one step - > retry above until it is inside
    }
    return points;
}
function getGridPointsInClip(target:MovieClip,res:int,offset:Number = 3):Vector.<Point>{
    var points:Vector.<Point> = new Vector.<Point>();
    var x:int,y:int,alpha:int,w:int = int(target.width),h:int = int(target.height);
    var bmd:BitmapData = new BitmapData(w,h,true,0x00FFFFFF);bmd.draw(target);
    var pixels:Vector.<uint> = bmd.getVector(bmd.rect),numPixels:int = w*h;
    for(var i:int = 0; i < numPixels; i+=res) {
        x = i%bmd.width;
        y = int(i/bmd.width);
        alpha = pixels[i] >>> 24;
        if(alpha > 0) points.push(new Point(x+random(-offset,offset),y+random(-offset,offset)));
    }
    return points;
}
function random(from:Number,to:Number):Number {
    if (from >= to) return from;
    var diff:Number = to - from;
    return (Math.random()*diff) + from;
}
function drawCircle(x:Number,y:Number,radius:Number,color:uint):void{
    graphics.lineStyle(1,color);
    graphics.drawCircle(x-radius,y-radius,radius);
}

心连心

这篇关于AS3:对异形随机点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆