开发者

Help me optimise this method please

开发者 https://www.devze.com 2023-02-11 00:43 出处:网络
It\'s a fairly straightforward operation - I\'m placing \'handles\' at regular points of a DisplayObjectContainer. These handles will be used to scale, rotate a la Illustrator, etc.

It's a fairly straightforward operation - I'm placing 'handles' at regular points of a DisplayObjectContainer. These handles will be used to scale, rotate a la Illustrator, etc.

I know this can be done with some fairly simple maths but I spent all my maths lessons drawing stick-men dying in increasingly creative ways.

TargetW/targetH are the original width/height of the object I'm adding the handles to. Obviously, adding the handles increases width/height so they are stored beforehand.

protected function positionHandles():void {

        handles[0].x = 0;
        handles[0].y = 0;

        handles[1].x = targetW / 2;
        handles[1].y = 0;

        handles[2].x = targetW;
        handles[2].y = 0;

        handles[3].x = 0;
        handles[3].y = targetH / 2;

        handles[4].x = targetW / 2;
        handles[4].y = targetH / 2;

        handles[5].x = targetW;
        handles[5].y = targetH / 2;

        handles[6].x = 0;
        handles[6].y = targetH;

        handles[7].x = targetW / 2;
        handles[7开发者_开发知识库].y = targetH;

        handles[8].x = targetW;
        handles[8].y = targetH;
    }


protected function positionHandles():void {

    for(var n:int = 0; n < 9; n ++) {
       handles[n].x = [0, targetW / 2, targetW][n % 3];
       handles[n].y = [0, targetH / 2, targetH][int(n / 3)];
    }

}


How about something like this.

var targetWidth:Number = 100;
var targetHeight:Number = 50;
var widthHandleDistance:Number = targetWidth/2;
var heightHandleDistance:Number = targetHeight/2;
var handles:Vector.<Point> = new Vector.<Point>();

function positionHandles():void
{
    for (var i:int = 0; i<=targetWidth; i=i+widthHandleDistance)
    {
        for (var k:int = 0; k<=targetHeight; k=k+heightHandleDistance)
        {
            var handle:Point = new Point(i,k);
            handles.push(handle);
        }
    }
}

This way you can also easily adjust the amount of handles.

0

精彩评论

暂无评论...
验证码 换一张
取 消