Exemplo n.º 1
0
        public void RangeSearch(Vector2 house, Node rootNode, float maxDistance, int depth, Func<Vector2, float>[] coordinates)
        {
            if (rootNode == null)
            {
                return;
            }
            var rootVector = coordinates[depth % 2](rootNode.root);
            var houseVector = coordinates[depth % 2](house);

            if (rootVector < (houseVector - maxDistance))
            {
                RangeSearch(house, rootNode.right, maxDistance, depth + 1, coordinates);
            }
            else if (rootVector > (houseVector + maxDistance))
            {
                RangeSearch(house, rootNode.left, maxDistance, depth + 1, coordinates);
            }
            else if ((rootVector >= (houseVector - maxDistance)) && (rootVector <= (houseVector + maxDistance)))
            {
                PotentialBuildingsInRange.Add(rootNode.root);
                RangeSearch(house, rootNode.left, maxDistance, depth + 1, coordinates);
                RangeSearch(house, rootNode.right, maxDistance, depth + 1, coordinates);
            }
        }
Exemplo n.º 2
0
 private static Node InsertTree(Node root, Node newNode, bool isDimensionX)
 {
     if (root == null)
     {
         root = newNode;
         return root;
     }
     else if (isDimensionX && newNode.value.X < root.value.X)
     {
         root.left = InsertTree(root.left, newNode, false);
     }else if (isDimensionX && newNode.value.X >= root.value.X)
     {
         root.right = InsertTree(root.right, newNode, false);
     }else if (!isDimensionX && newNode.value.Y < root.value.Y)
     {
         root.left = InsertTree(root.left, newNode, true);
     }
     else if (!isDimensionX && newNode.value.Y >= root.value.Y)
     {
         root.right = InsertTree(root.right, newNode, true);
     }
     return root;
 }
Exemplo n.º 3
0
        private static IEnumerable<IEnumerable<Vector2>> FindSpecialBuildingsWithinDistanceFromHouse(
      IEnumerable<Vector2> specialBuildings, 
      IEnumerable<Tuple<Vector2, float>> housesAndDistances)
    {
            //Dictionary<Vector2, List<Node>> housedAndSpecialBuildings = new Dictionary<Vector2, List<Node>>();
            List<List<Vector2>> housesAndSpecialBuildings = new List<List<Vector2>>();
            List<Node> nodes = new List<Node>();
            //fill node list with nodes
            foreach (var specialBuilding in specialBuildings)
            {
                Node newNode = new Node(specialBuilding);
                nodes.Add(newNode);
            }

            //make first root and create tree
            //Node root = nodes[0];

            Node root = nodes[nodes.Count / 2];
            nodes.Remove(root);

            for (int i = 0; i < nodes.Count; i++)
            {
                root = InsertTree(root, nodes[i], true);
            }

            //search per house
            foreach (var houseItem in housesAndDistances)
            {
                List<Vector2> specialBuildingsPerHouse = new List<Vector2>();

                Queue<Node> q = new Queue<Node>();
                q.Enqueue(root);
                while (q.Count > 0)
                {
                    Console.WriteLine();
                    Node current = q.Dequeue();
                    if (current == null)
                    {
                        continue;
                    }
                    q.Enqueue(current.left);
                    q.Enqueue(current.right);


                    if (Vector2.Distance(houseItem.Item1, current.value) < houseItem.Item2)
                    {
                        specialBuildingsPerHouse.Add(current.value);
                    }
                    else
                    {
                        Console.WriteLine("not in range");
                    }
                }
                housesAndSpecialBuildings.Add(specialBuildingsPerHouse);
            }

            return housesAndSpecialBuildings;

          //  return
          //from h in housesAndDistances
          //select
          //  from s in specialBuildings
          //  where Vector2.Distance(h.Item1, s) <= h.Item2
          //  select s;
    }