Esempio n. 1
0
        static void ClosestPlane()
        {
            //Collect data from user
            Console.Write("Enter the x coordinate of the ship (km): ");
            float x = Convert.ToSingle(Console.ReadLine());
            Console.Write("Enter the y coordinate of the ship (km): ");
            float y = Convert.ToSingle(Console.ReadLine());
            Console.Write("Enter the z coordinate of the ship (km): ");
            float z = Convert.ToSingle(Console.ReadLine());
            Vector3D ship = new Vector3D(x, y, z); //km
            Console.WriteLine("First planar point:");
            Console.Write("Enter the x coordinate (km): ");
            x = Convert.ToSingle(Console.ReadLine());
            Console.Write("Enter the y coordinate (km): ");
            y = Convert.ToSingle(Console.ReadLine());
            Console.Write("Enter the z coordinate (km): ");
            z = Convert.ToSingle(Console.ReadLine());
            Vector3D point1 = new Vector3D(x, y, z); //km
            Console.WriteLine("Second planar point:");
            Console.Write("Enter the x coordinate (km): ");
            x = Convert.ToSingle(Console.ReadLine());
            Console.Write("Enter the y coordinate (km): ");
            y = Convert.ToSingle(Console.ReadLine());
            Console.Write("Enter the z coordinate (km): ");
            z = Convert.ToSingle(Console.ReadLine());
            Vector3D point2 = new Vector3D(x, y, z); //km
            Console.WriteLine("Third planar point:");
            Console.Write("Enter the x coordinate (km): ");
            x = Convert.ToSingle(Console.ReadLine());
            Console.Write("Enter the y coordinate (km): ");
            y = Convert.ToSingle(Console.ReadLine());
            Console.Write("Enter the z coordinate (km): ");
            z = Convert.ToSingle(Console.ReadLine());
            Vector3D point3 = new Vector3D(x, y, z); //km

            if (point1 == point2 || point2 == point3 || point3 == point1) //check if points are the same
            {
                Console.WriteLine("Error: Redundant points entered.");
            }
            else
            {
                //The mathening
                Vector3D point1To2 = point2 - point1; //km, one vector within the plane
                Vector3D point1To3 = point3 - point1; //km, another vector within the plane

                if ((!point1To2) == (!point1To3) || (!point1To2) == -1 * (!point1To3)) //check if points are on a single line
                {
                    Console.WriteLine("Error: Collinear points entered.");
                }
                else
                {
                    Vector3D normal = !(point1To2 % point1To3); //the planar normal, normalized to remove units
                    Vector3D closestPoint = ship.ClosestPointPlane(point1, normal); //km, the closest point on the plane

                    //Output
                    Console.WriteLine("Closest Point is: " + closestPoint + " km");
                    Console.WriteLine("Distance of closest approach is: {0:N2} km", (closestPoint - ship).GetMagnitude());
                }
            }
        }