Example #1
0
        static void AddPoint()
        {
            double x = CInput.ReadDouble("Введите X");
            double y = CInput.ReadDouble("Введите Y");

            Console.WriteLine("Введите имя точки");
            string name  = Console.ReadLine();
            CPoint point = new CPoint(x, y, name);

            map.AddPoint(point);
        }
Example #2
0
        public CPoint GetNearestPoint(CPoint p, List <CPoint> targets)
        {
            double[] Distances = new double[targets.Count];

            for (int i = 0; i < targets.Count; i++)
            {
                Distances[i] = GetDistance(p, targets[i]);
            }
            double minVal   = Distances.Min();
            int    indexMin = Array.IndexOf(Distances, minVal);

            return(targets[indexMin]);
        }
Example #3
0
        static void CalcRoute()
        {
            Console.WriteLine("Выберете начальную точку маршрута");
            ShowAll();
            string namePoint   = Console.ReadLine();
            CPoint startPoint  = map.points.Find(i => i.name == namePoint);
            var    optimalRout = map.GetOptimalRout(startPoint);

            Console.WriteLine("Оптимальный маршрут: ");
            foreach (var t in optimalRout)
            {
                Console.WriteLine(t.name);
            }
        }
Example #4
0
 private void LoadFromBinary()
 {
     using (BinaryReader br = new BinaryReader(File.Open(fileName + ".bin", FileMode.Open)))
     {
         while (br.BaseStream.Position != br.BaseStream.Length)
         {
             string name = br.ReadString();
             double x    = br.ReadDouble();
             double y    = br.ReadDouble();
             CPoint p    = new CPoint(x, y, name);
             points.Add(p);
         }
     }
 }
Example #5
0
        public void LoadFromTextFile()
        {
            DeleteAllPoints();
            StreamReader sr = File.OpenText(fileName + ".txt");

            while (!sr.EndOfStream)
            {
                string name = sr.ReadLine();
                double x    = Convert.ToDouble(sr.ReadLine());
                double y    = Convert.ToDouble(sr.ReadLine());
                CPoint p    = new CPoint(x, y, name);
                points.Add(p);
            }

            sr.Close();
        }
Example #6
0
        public List <CPoint> GetOptimalRout(CPoint p)
        {
            List <CPoint> data   = new List <CPoint>(points);
            List <CPoint> result = new List <CPoint>();
            CPoint        vp     = p;

            result.Add(vp);
            data.RemoveAll(tp => tp.name == vp.name);
            do
            {
                vp = GetNearestPoint(result.Last(), data);
                result.Add(vp);
                data.RemoveAll(tp => tp.name == vp.name);
            } while (data.Count != 0);

            return(result);
        }
Example #7
0
 public void AddPoint(CPoint p)
 {
     points.Add(p);
     Save();
 }
Example #8
0
 private double GetDistance(CPoint p1, CPoint p2)
 {
     return(Math.Sqrt(Math.Pow((p2.x - p1.x), 2.0) + Math.Pow((p2.y - p1.y), 2.0)));
 }