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); }
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]); }
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); } }
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); } } }
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(); }
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); }
public void AddPoint(CPoint p) { points.Add(p); Save(); }
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))); }