在AS3中查找点数组中的最高/最低y值

时间:2012-12-18 06:30:04

标签: arrays actionscript-3 search coordinates point

如果我在AS3中有一系列的点数:

(x=584.1, y=279.4),(x=584.1, y=280.4),(x=584.1, y=281.4),(x=584.1, y=282.4),(x=584.1, y=283.4),(x=584.1, y=284.4),(x=584.1, y=285.4),(x=584.1, y=286.4),(x=584.1, y=287.4),(x=585.1, y=287.45),(x=586.1, y=287.45),(x=584.1, y=288.4),(x=585.1, y=288.45),(x=586.1, y=288.45),(x=587.1, y=288.5),(x=588.1, y=288.55),(x=584.1, y=289.4),(x=585.1, y=289.45),(x=586.1, y=289.45),(x=587.1, y=289.5),(x=588.1, y=289.55),(x=584.1, y=290.4),(x=585.1, y=290.45),(x=586.1, y=290.45),(x=587.1, y=290.5),(x=588.1, y=290.55),(x=584.1, y=291.4),(x=585.1, y=291.45),(x=586.1, y=291.45),(x=587.1, y=291.5),(x=588.1, y=291.55),(x=584.1, y=292.4),(x=585.1, y=292.45),(x=586.1, y=292.45),(x=587.1, y=292.5),(x=588.1, y=292.55),(x=584.1, y=293.4),(x=585.1, y=293.45),(x=586.1, y=293.45),(x=587.1, y=293.5),(x=588.1, y=293.55),(x=584.1, y=294.4),(x=585.1, y=294.45),(x=586.1, y=294.45),(x=587.1, y=294.5),(x=588.1, y=294.55),(x=584.1, y=295.4),(x=585.1, y=295.45),(x=586.1, y=295.45),(x=587.1, y=295.5),(x=588.1, y=295.55)

找到最高和最低y值的有效方法是什么?

3 个答案:

答案 0 :(得分:1)

[编辑]有时Array.sortOn最快。如果需要对数组进行排序,请使用native函数,否则,使用Daniel的代码。

myArray.sortOn('y', Array.NUMERIC);

我使用Daniel的代码进行基准测试和基准测试。在某些情况下,sortOn / copy比无数组副本更快。在某些情况下没有。无论哪种方式,数组复制/排序都会产生不一致的结果,因为它会消耗更多的内存。结果和基准如下。

(在调试播放器中,array.sortOn将永远丢失)

package {

    import flash.display.*;
    import flash.geom.*;
    import flash.text.*;
    import flash.utils.*;

    final public class ArraySortTest extends Sprite {

        /**
         *  @private
         */
        private const arr:Array         = [];

        private const text:TextField    = new TextField();

        public function ArraySortTest():void {

            text.width          = stage.stageWidth;
            text.height         = stage.stageHeight;
            addChild(text);

            var length:int      = 1000;
            var iterations:int  = 5000;
            while (length--) {
                arr.push(new Point(Math.random() * 500, Math.random() * 500));
            }

            var i:int, start:int, result:Point = new Point();

            start   = getTimer();
            i       = iterations;

            while (i--) {
                getMinMax(arr, 'y', result);
            }

            text.appendText('nosort: ' + String(getTimer() - start) + result + '\n');

            start   = getTimer();
            i       = iterations;
            while (i--) {
                getMinMaxSorted(arr, 'y', result);
            }
            text.appendText('sorted: ' + String(getTimer() - start) + result + '\n');


            start   = getTimer();
            i       = iterations;
            while (i--) {
                getMinMaxSortedConcat(arr, 'y', result);
            }
            text.appendText('sorted concat: ' + String(getTimer() - start) + result + '\n');

        }

        private function getMinMaxSortedConcat(input:Array, key:String, result:Point):void {

            input.concat().sortOn('y', Array.NUMERIC);

            result.x = input[0][key];
            result.y = input[int(input.length - 1)][key];
        }

        private function getMinMaxSorted(input:Array, key:String, result:Point):void {

            input.sortOn('y', Array.NUMERIC);

            result.x = input[0][key];
            result.y = input[int(input.length - 1)][key];
        }

        private function getMinMax(input:Array, key:String, result:Point):void {

            var len:Number = input.length;
            var min:Number = Number.MAX_VALUE;
            var max:Number = Number.MIN_VALUE;

            var check:Number;
            for (var i:int = 0; i < len; i++) {
                check = input[i][key];

                if (check < min) {
                    min = check;
                } else if (check > max) {
                    max = check;
                }
            }

            result.x = min;
            result.y = max;
        }
    }
}

结果:

100 elements, 5000 iterations each
nosort: 124(x=3.739513223990798, y=495.2090959995985)
sorted: 109(x=3.739513223990798, y=495.2090959995985)
sorted concat: 115(x=3.739513223990798, y=495.2090959995985)

1000 elements, 5000 iterations each
nosort: 1263(x=0.13151345774531364, y=499.65104297734797)
sorted: 1181(x=0.13151345774531364, y=499.65104297734797)
sorted concat: 1234(x=0.13151345774531364, y=499.65104297734797)

1000 elements, 10000 iterations
nosort: 2474(x=0.18377462401986122, y=499.73958847112954)
sorted: 2371(x=0.18377462401986122, y=499.73958847112954)
sorted concat: 2454(x=0.18377462401986122, y=499.73958847112954)

10000 elements, 1000 iterations
nosort: 2487(x=0.003137858584523201, y=499.816557392478)
sorted: 2961(x=0.003137858584523201, y=499.816557392478)
sorted concat: 3157(x=0.003137858584523201, y=499.816557392478)

答案 1 :(得分:1)

我很确定这是最快的,如果你只对任何随机输入数据的最高和最低感兴趣,其中n&gt; 2。

var input:Array = [ {x:584.1, y:279.4}, {x:584.1, y:280.4}, {x:584.1, y:281.4}, ...];

public function MinMaxValues() {
    var len:Number = input.length;
    var min:Number = Number.MAX_VALUE;
    var max:Number = Number.MIN_VALUE;

    var check:Number;
    for (var i:int = 0; i < len; i++) {
        check = input[i]["y"];

        if (check < min) {
            min = check;
        } 

        if (check > max) {
            max = check;
        }
    }
    trace("minimum value of [" + len + "] items is::" + min);
    trace("maximum value of [" + len + "] items is::" + max);

}

答案 2 :(得分:0)

就是这样,遍历数组,抓住第一个点的Y值作为当前的最小值和最大值,并比较所有其他值。我认为没有更好的方法,除非你提供有关如何在该数组中组织数据的其他信息。