Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            string FileName, Latitude, Longitude, count, type;

            Console.WriteLine("coordinates and size!");

            string stroka = Console.ReadLine();

            string[] items = stroka.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

            FileName  = items[0];
            Latitude  = items[1];
            Longitude = items[2];
            count     = items[3];
            type      = items[4];


            RNode rTree = FillRTree(FileName);

            Console.WriteLine();

            List <Place> nearest = RNodeSearch.FindNearestPlacesByCount(rTree, double.Parse(Latitude), double.Parse(Longitude), int.Parse(count), type);

            Output(nearest);

            Console.WriteLine();
        }
Ejemplo n.º 2
0
        public static bool OptimalInclude(RNode first, RNode second, Place place)
        {
            double lalitudeMax1  = FindBigger(first.latitudeMax, place.Latitude);
            double latitudeXMin1 = FindLower(first.latitudeMax, place.Latitude);

            double longitudeMax1 = FindBigger(first.longitudeMax, place.Longitude);
            double longitudeMin1 = FindLower(first.longitudeMax, place.Longitude);

            double tmp1 = (lalitudeMax1 - latitudeXMin1) *
                          (longitudeMax1 - longitudeMin1) +
                          (second.latitudeMax - second.latitudeMin) *
                          (second.longitudeMax - second.longitudeMin);

            double latitudeMax2 = FindBigger(second.latitudeMax, place.Latitude);
            double latitudeMin2 = FindLower(second.latitudeMax, place.Latitude);

            double longitudeMax2 = FindBigger(second.longitudeMax, place.Longitude);
            double longitudeMin2 = FindLower(second.longitudeMax, place.Longitude);

            double tmp2 = (first.latitudeMax - first.latitudeMin) *
                          (first.longitudeMax - first.longitudeMin) +
                          (latitudeMax2 - latitudeMin2) *
                          (longitudeMax2 - longitudeMin2);

            if (tmp1 != tmp2)
            {
                return(tmp1 < tmp2);
            }
            else
            {
                return(first.Size < second.Size);
            }
        }
Ejemplo n.º 3
0
        private void Divide()
        {
            IsParent = true;

            FirstChild  = new RNode();
            SecondChild = new RNode();

            if (latitudeMax - latitudeMin > longitudeMax - longitudeMin)
            {
                list.Sort(CompareByCoordinateX);
            }
            else
            {
                list.Sort(CompareByCoordinateY);
            }

            FirstChild.Add(list[0]);
            SecondChild.Add(list[Size - 1]);

            list.RemoveAt(Size - 1);
            list.RemoveAt(0);

            foreach (Place Place in list)
            {
                if (OptimalInclude(FirstChild, SecondChild, Place))
                {
                    FirstChild.Add(Place);
                }
                else
                {
                    SecondChild.Add(Place);
                }
            }
        }
Ejemplo n.º 4
0
        public static List <Place> FindNearestPlaces(RNode node, double x, double y, int radius, string type)
        {
            List <Place> Places = new List <Place>();

            if (node.IsParent)
            {
                var first = new List <Place>();
                if (IsRadiusIsEnough(node.FirstChild, x, y, radius))
                {
                    first = FindNearestPlaces(node.FirstChild, x, y, radius, type);
                }
                else
                {
                    first = null;
                }


                var second = new List <Place>();
                if (IsRadiusIsEnough(node.SecondChild, x, y, radius))
                {
                    second = FindNearestPlaces(node.SecondChild, x, y, radius, type);
                }
                else
                {
                    second = null;
                }


                if (first == null && second == null)
                {
                    return(null);
                }
                if (first == null)
                {
                    return(second);
                }
                if (second == null)
                {
                    return(first);
                }

                foreach (var item in second)
                {
                    first.Add(item);
                }

                return(first);
            }
            foreach (var Place in node.list)
            {
                if (FindDistanceToPlace(Place, x, y) <= radius && type.ToString() == Place.Type.ToString())
                {
                    Places.Add(Place);
                }
            }

            return(Places);
        }
Ejemplo n.º 5
0
        public static RNode FillRTree(string filename)
        {
            RNode         tree = new RNode();
            List <string> list = new List <string>();

            list = File.ReadAllLines(filename).ToList();
            foreach (var item in list)
            {
                string[] buf = item.Split(';');
                tree.Add(new Place(double.Parse(buf[0]), double.Parse(buf[1]), buf[2], buf[3], buf[4], buf[5]));
            }

            return(tree);
        }
Ejemplo n.º 6
0
        public static List <Place> FindNearestPlacesByCount(RNode node, double x, double y, int count, string type)
        {
            int          radius = 5;
            List <Place> places = FindNearestPlaces(node, x, y, radius, type);

            try
            {
                while ((places.Count <= count - 1))
                {
                    radius *= 2;
                    places  = FindNearestPlaces(node, x, y, radius, type);
                }
            }
            catch (Exception)
            {
                Console.WriteLine("Out of range");
            }

            places.RemoveRange(count, places.Count - count);

            return(places);
        }
Ejemplo n.º 7
0
        static bool IsRadiusIsEnough(RNode node, double x, double y, int radius)
        {
            double nearestX;

            if (x < node.latitudeMin)
            {
                nearestX = node.latitudeMin;
            }
            if (x > node.latitudeMax)
            {
                nearestX = node.latitudeMax;
            }
            nearestX = x;


            double nearestY;

            if (y < node.longitudeMin)
            {
                nearestY = node.longitudeMin;
            }
            if (x > node.longitudeMax)
            {
                nearestY = node.longitudeMax;
            }
            nearestY = y;

            double xCoef1 = 111 * Math.Cos(y);
            double xCoef2 = 111 * Math.Cos(nearestY);
            double yCoef  = 111;

            double xLength = ((nearestX - x) * xCoef1 + (nearestX - x) * xCoef2) / 2;
            double yLength = (nearestY - y) * yCoef;

            return(Math.Sqrt(Math.Pow(xLength, 2) + Math.Pow(yLength, 2)) <= radius);
        }