/// <summary>
        /// Reads a path of 3D points from the Location/fileName that cane be set through the properties PathName and FileName
        /// </summary>
        /// <returns>Collection of Point3D</returns>
        static public Path ReadPathFromFile()
        {
            Path resultPath = new Path();
            StreamReader reader = new StreamReader(Location + FileName);
            string fileContents;
            string[] splitters = { "--", "\n", "[", "]", ",", ":","Point","->","\r\n"," "};

            //reads the contents of file 
            using (reader)
            {
                fileContents = reader.ReadToEnd();
            }
            //points is an array of strings containing ine [0] - the Path name and in all next 4-s - Point3d 
            string[] points = fileContents.Split(splitters, StringSplitOptions.RemoveEmptyEntries);

            //first is the name of the whole path
            resultPath.Name = points[0];

            //for every 4 elements in the points array get the nex Point X,Y,Z and Name and put them in a new instance of point
            for (int i = 1; i < points.Length; i += 4)
            {
                Point3D nextPoint = new Point3D(int.Parse(points[i + 1]), int.Parse(points[i + 2]), int.Parse(points[i + 3]), points[i]);
                resultPath.PathAddPoint = nextPoint;
            }

            return resultPath;
        }
 public static double Calculate(Point3D pointA, Point3D pointB)
 {
     // Implemented Euclidean distance formula
     return Math.Sqrt(
         (pointA.X - pointB.X) * (pointA.X - pointB.X) +
         (pointA.Y - pointB.Y) * (pointA.Y - pointB.Y) +
         (pointA.Z - pointB.Z) * (pointA.Z - pointB.Z));
 }
        /// <summary>
        /// Instantiates an instance with and existing List<Point3D>
        /// </summary>
        /// <param name="pathList"></param>
        public Path(List<Point3D> pathList, string name)
            : this()
        {
            this.Name = name;
            foreach (var point in pathList)
            {
                this.PathAddPoint = point;
            }

        }
using System.IO;

namespace ThreeDimensionalSpace
{
    class TestPoint
    {
        static void Main()
        {
            try
            {

                Point3D point1 = new Point3D(-1, -1, 1, "K");
                Point3D point2 = new Point3D(-2, -2, -2, "M");
                Point3D point3 = new Point3D(5, 6, 7, "L");
                Point3D point4 = new Point3D(10, 10, 10, "N");
                Console.WriteLine("Center {0}\n{1}\n{2}\nDistance between point {4} and {5}:\t{3}\n", Point3D.Center, point1, point2,
                    DistanceCalculator3D.CalculateDistance3D(point1, point2), point1.Name, point2.Name);

                Path newPath = new Path(point1, "Path");
                newPath.PathAddPoint = point2;
                newPath.PathAddPoint = point1;
                newPath.PathAddPoint = point2;

                Path newPath1 = new Path(point3, "Path1");
                newPath1.PathAddPoint = point4;
                newPath1.PathAddPoint = point3;
                newPath1.PathAddPoint = point4;
                
                Console.WriteLine(newPath);
                Console.WriteLine(newPath1);

                Console.WriteLine("\nAdding Path (after) Path1");
                newPath1.PathAddPath(newPath);
                Console.WriteLine(newPath1);

                //PathStorage.FileName = newPath.Name + ".pth";
                //PathStorage.SavePath(newPath);
                Console.WriteLine("Path {0} saved in {1}{2}", newPath.Name, PathStorage.Location, PathStorage.FileName);
                PathStorage.FileName = newPath1.Name + ".pth";
                PathStorage.SavePath(newPath1);
                
                Path pathFromFIle = PathStorage.ReadPathFromFile();
                Console.WriteLine("\nPath read from file:{0}",pathFromFIle);                

                PathStorage.AddPointToFile(Point3D.Center);
                pathFromFIle = PathStorage.ReadPathFromFile();
                Console.WriteLine("\nAdded 'center' to pathfile : {0}", pathFromFIle);

            }
            catch (FileLoadException ex)
            {
                Console.WriteLine(ex.Message);
        /// <summary>
        /// Calculates distance between 2 points in the 3D space.
        /// </summary>
        /// <param name="first"></param>
        /// <param name="second"></param>
        /// <returns></returns>
        public static float CalculateDistance3D(Point3D first, Point3D second)
        {
            ////calculate distance between 2D X and Z coordinates 
            //float distanceXZ = CalculateDistance2D(first.X,first.Z, second.X, second.Z);
            //calculate distance between 2D Y and Z coordinates 
            float distanceYZ = CalculateDistance2D(first.Y, first.Z, second.Y, second.Z);
            //calculate distance between 2D X and Y cooidrinates
            float distanceXY = CalculateDistance2D(first.X, first.Y, second.X, second.Y);

            //the calculated distances create a 90-degreee triangle from which I calculate the 3D distance
            float distanceXYZ = (float)Math.Sqrt(Math.Pow(distanceXY, 2) + Math.Pow(distanceYZ, 2));
            return distanceXYZ;
        }
 public static Path LoadPath(string fileName)
 {
     Path currPath = new Path();
     using (StreamReader reader = new StreamReader(fileName))
     {
         for (string input = reader.ReadLine(); input != "end"; input = reader.ReadLine())
         {
             string[] coord = input.Split(',');
             Point3D currPoint = new Point3D(double.Parse(coord[0]), double.Parse(coord[1]), double.Parse(coord[2]));
             currPath.Add(currPoint);
         }
     }
     return currPath;
 }
 /// <summary>
 /// Adds a point to the end of the file(end of path) and saves to the same file
 /// </summary>
 /// <param name="point"></param>
 static public void AddPointToFile(Point3D point)
 {
     Path tempPath = PathStorage.ReadPathFromFile();
     File.Delete(PathStorage.Location + PathStorage.FileName);
     tempPath.PathAddPoint = point;
     PathStorage.SavePath(tempPath);
 }
 public void Add(Point3D item)
 {
     this.points.Add(item);
 }
Exemple #9
0
 // Adds an item of type Point3D to the array
 public void AddPoint(Point3D point)
 {
     this.points.Add(point);
 }
 public static double CalcDistance3D(Point3D first, Point3D second)
 {
     return Math.Sqrt(Math.Pow(first.X - second.X, 2) + Math.Pow(first.Y - second.Y, 2) + Math.Pow(first.Z - second.Z, 2));
 }
 public Path(Point3D point, string name)
     : this()
 {
     this.Name = name;
     this.PathGet.Add(point);
 }