public static Path3D LoadPath(string destination) { List<Point3D> points = new List<Point3D>(); using (StreamReader reader = new StreamReader(destination)) { while (true) { string line = reader.ReadLine(); if (string.IsNullOrEmpty(line)) { break; } string[] pointCoordinates = line.Split(' '); double x = double.Parse(pointCoordinates[0]); double y = double.Parse(pointCoordinates[1]); double z = double.Parse(pointCoordinates[2]); Point3D point = new Point3D(x, y, z); points.Add(point); } Path3D path = new Path3D(points.ToArray()); return path; } }
public static void Main() { Point3D startPoint = Point3D.StartPoint; Point3D pointA = new Point3D(3.5, 4.2, 8); Point3D pointB = new Point3D(1, 17, 4.8); Point3D pointC = new Point3D(4.8, 1, 2); Path3D path = new Path3D(startPoint, pointA, pointB, pointC); try { Storage.SavePath(path, "path.txt"); } catch (IOException ex) { Console.WriteLine(ex.Message); } try { Path3D pathLoaded = Storage.LoadPath("path.txt"); Console.WriteLine(path); } catch (FileNotFoundException ex) { Console.WriteLine(ex.Message); } }
public static void SavePath(Path3D path, string destination) { using (FileStream stream = File.Open(destination, FileMode.Create)) using (StreamWriter writer = new StreamWriter(stream)) { foreach (var point in path.Path) { writer.WriteLine("{0} {1} {2}", point.X, point.Y, point.Z); } } }