Esempio n. 1
0
        private KDNode <T> FindNearest(KDNode <T> node, T dearch, int depth)
        {
            int        Direction = dearch.Comparator((Axis)(depth % 3)).Compare(dearch, node.Location);
            KDNode <T> Next      = (Direction < 0) ? node.Left : node.Right;
            KDNode <T> Other     = (Direction < 0) ? node.Right : node.Left;
            KDNode <T> Best      = (Next == null) ? node : FindNearest(Next, dearch, depth + 1);

            if (node.Location.SquaredDistance(dearch) < Best.Location.SquaredDistance(dearch))
            {
                Best = node;
            }

            if (Other != null)
            {
                if (Other.Location.AxisSquaredDistance(dearch, (Axis)(depth % 3)) < Best.Location.SquaredDistance(dearch))
                {
                    KDNode <T> PossibleBest = FindNearest(Other, dearch, depth + 1);
                    if (PossibleBest.Location.SquaredDistance(dearch) < Best.Location.SquaredDistance(dearch))
                    {
                        Best = PossibleBest;
                    }
                }
            }

            return(Best);
        }
Esempio n. 2
0
 public KDTree(T[] items)
 {
     Root = CreateTree(items, 0, items.Length, 0);
 }
Esempio n. 3
0
 public KDTree(T[] Items)
 {
     Root = CreateTree(Items, 0, Items.Length, 0);
 }