【问题标题】:Find top 3 closest targets in Actionscript 3在 Actionscript 3 中查找前 3 个最接近的目标
【发布时间】:2010-09-14 14:36:06
【问题描述】:

我有一个点字符数组,我想获取任何字符并能够遍历该数组并找到前 3 个最近的(使用 Point.distance)邻居。谁能告诉我如何做到这一点?

【问题讨论】:

    标签: actionscript-3 actionscript


    【解决方案1】:

    这是我昨晚发布的代码的新改进版本。它由两个类组成,PointTester 和 TestCase。这一次我也可以测试它了!

    我们从 TestCase.as 开始

    package  {
    
        import flash.geom.Point;
        import flash.display.Sprite;
    
        public class TestCase extends Sprite {
    
            public function TestCase() {
                // some data to test with
                var pointList:Array = new Array();
                pointList.push(new Point(0, 0));
                pointList.push(new Point(0, 0));
                pointList.push(new Point(0, 0));
                pointList.push(new Point(1, 2));
                pointList.push(new Point(9, 9));
    
                // the point we want to test against
                var referencePoint:Point = new Point(10, 10);
    
                var resultPoints:Array = PointTester.findClosest(referencePoint, pointList, 3);
    
                trace("referencePoint is at", referencePoint.x, referencePoint.y);
                for each(var result:Object in resultPoints) {
                    trace("Point is at:", result.point.x, ", ", result.point.y, " that's ", result.distance, " units away");
                }
            }
    
        }
    
    }
    

    这就是 PointTester.as

    package  {
    
        import flash.geom.Point;
    
        public class PointTester {
    
            public static function findClosest(referencePoint:Point, pointList:Array, maxCount:uint = 3):Array{
    
                // this array will hold the results
                var resultList:Array = new Array();
    
                // loop over each point in the test data
                for each (var testPoint:Point in pointList) {
    
                    // we store the distance between the two in a temporary variable
                    var tempDistance:Number = getDistance(testPoint, referencePoint);
    
                    // if the list is shorter than the maximum length we don't need to do any distance checking
                    // if it's longer we compare the distance to the last point in the list, if it's closer we add it
                    if (resultList.length <= maxCount || tempDistance < resultList[resultList.length - 1].distance) {
    
                        // we store the testing point and it's distance to the reference point in an object
                        var tmpObject:Object = { distance : tempDistance, point : testPoint };
                        // and push that onto the array
                        resultList.push(tmpObject);
    
                        // then we sort the array, this way we don't need to compare the distance to any other point than
                        // the last one in the list
                        resultList.sortOn("distance", Array.NUMERIC );
    
                        // and we make sure the list is kept at at the proper number of entries
                        while (resultList.length > maxCount) resultList.pop();
                    }
                }
    
                return resultList;
            }
    
            public static function getDistance(point1:Point, point2:Point):Number {
                var x:Number = point1.x - point2.x;
                var y:Number = point1.y - point2.y;
                return Math.sqrt(x * x + y * y);
            }
        }
    }
    

    【讨论】:

    • 您将原始点存储在 tmpObject 中,但以后不再使用它,这会是一个问题吗?
    • 其实我只是用它来获取两点之间的距离,新版本有它自己的功能。
    【解决方案2】:

    可能值得一提的是,如果点的数量足够大以使性能变得重要,则可以通过保留两个点列表来更快地实现目标,一个按 X 排序,另一个按 Y 排序。一个然后可以通过遍历每个点在 O(logn) 时间而不是 O(n) 时间内找到最近的 3 个点。

    【讨论】:

      【解决方案3】:

      如果您使用grapefrukt的解决方案,您可以将getDistance方法更改为return x*x + y*y;而不是return Math.sqrt( x * x + y * y );

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-06-18
        • 2012-11-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-09-21
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多