Calculating the angle based on point coordinates provided in the console

Viewed 73
using System;

namespace ProgrammingAssignment2
{
    /// <summary>
    /// Programming Assignment 2
    /// </summary>
    class Program
    {
        // x and y coordinates for points
        static float point1X;
        static float point1Y;
        static float point2X;
        static float point2Y;

        /// <summary>
        /// Programming Assignment 2
        /// </summary>
        /// <param name="args">command-line args</param>
        static void Main(string[] args)
        {
            // loop while there's more input
            string input = Console.ReadLine();

            while (input[0] != 'q')
            {
                // extract point coordinates from string
                GetInputValuesFromString(input);

                // Add your code between this comment
                // and the comment below. You can of
                // course add more space between the
                // comments as needed
                float deltaX = point1X - point2X;
                float deltaY = point1Y - point2Y;

                float hypotenuse = (float)Math.Sqrt(Math.Pow(deltaX, 2) + Math.Pow(deltaY, 2));
                float angleInRadians = (float)Math.Atan2(deltaY , deltaX);

                var angleInDegrees = ((angleInRadians * (180 /(float)Math.PI) + 360) % 360)-180;

                Console.WriteLine(hypotenuse + " " + angleInDegrees);

                // Don't add or modify any code below
                // this comment
                input = Console.ReadLine();
            }
        }

        /// <summary>
        /// Extracts point coordinates from the given input string
        /// </summary>
        /// <param name="input">input string</param>
        static void GetInputValuesFromString(string input)
        {
            // extract point 1 x
            int spaceIndex = input.IndexOf(' ');

            point1X = float.Parse(input.Substring(0, spaceIndex));

            // move along string and extract point 1 y
            input = input.Substring(spaceIndex + 1);
            spaceIndex = input.IndexOf(' ');
            point1Y = float.Parse(input.Substring(0, spaceIndex));

            // move along string and extract point 2 x
            input = input.Substring(spaceIndex + 1);
            spaceIndex = input.IndexOf(' ');
            point2X = float.Parse(input.Substring(0, spaceIndex));

            // point 2 y is the rest of the string
            input = input.Substring(spaceIndex + 1);
            point2Y = float.Parse(input);

            #region Unfortunately, Mono doesn't have a Split method!

            //string[] values = input.Split(' ');

            //point1X = float.Parse(values[0]);
            //point1Y = float.Parse(values[1]);
            //point2X = float.Parse(values[2]);
            //point2Y = float.Parse(values[3]);

            #endregion
        }
    }
}

List of passed and failed test cases

I can't find the answers of the parameters in these failed test cases:

Test case input:
0 0 -1 0 with expected result: 1 180, your result: 1 -180
Test case input:
2 2 -4 4 with expected result: 6.324555 161.565, your result: 6.324555 161.5651
Test case input:
2 2 4 -4 with expected result: 6.324555 -71.56505, your result: 6.324555 -71.56506

I did it for other parameters that shows true results review the code and please try to solved it

1 Answers

To calculate the angle we have to use Math.Atan2 method which returns the angle whose tangent is the quotient of two specified numbers.

using System;
using System.Runtime.ConstrainedExecution;

namespace GameDevPythagoras
{
    /// <summary>
    /// Pythagoras Implementation for position calculation using C#
    /// </summary>
    class Program
    {
        // declaring x and y coordinates for player positions
        static float point1X;
        static float point1Y;
        static float point2X;
        static float point2Y;


        /// <param name="args">command-line args</param>
        static void Main(string[] args)
        {
            // loop while there's more input
            string input = Console.ReadLine();
            while (input[0] != 'q')
            {
                // extract point coordinates from string
                GetInputValuesFromString(input);

                // calculate distance between points 1 and 2
                float distance = (float) Math.Sqrt((Math.Pow(point1X - point2X, 2) + Math.Pow(point1Y - point2Y, 2)));

                double angle = (float)Math.Atan2(point2Y - point1Y, point2X - point1X);

                angle *= (float)180/Math.PI;

                Console.WriteLine("The Distance Between Player A and B is " + (float) Math.Round(distance, 6));
                Console.WriteLine("The Angle from Player A to Player B is " + (float)Math.Round(angle, 5) + " degrees");
                input = Console.ReadLine();


            }
        }

        /// <summary>
        /// Extracts point coordinates from the given input string
        /// </summary>
        /// <param name="input">input string</param>
        static void GetInputValuesFromString(string input)
        {
            // extract point 1 x
            int spaceIndex = input.IndexOf(' ');
            point1X = float.Parse(input.Substring(0, spaceIndex));

            // move along string and extract point 1 y
            input = input.Substring(spaceIndex + 1);
            spaceIndex = input.IndexOf(' ');
            point1Y = float.Parse(input.Substring(0, spaceIndex));

            // move along string and extract point 2 x
            input = input.Substring(spaceIndex + 1);
            spaceIndex = input.IndexOf(' ');
            point2X = float.Parse(input.Substring(0, spaceIndex));

            // point 2 y is the rest of the string
            input = input.Substring(spaceIndex + 1);
            point2Y = float.Parse(input);
        }
    }
}

Source Here

Related