Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Can';在C中求角时,得不到正确答案#_C#_Math - Fatal编程技术网

C# Can';在C中求角时,得不到正确答案#

C# Can';在C中求角时,得不到正确答案#,c#,math,C#,Math,我在coursera上统一入门课程。我已经完成了第一个编程任务,即求两点的斜边和角度。我几乎把代码弄对了,但有几个答案是对的,有几个是错的!我不知道怎么了 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ProgrammingAssignment1 { // IMPORTANT: On

我在coursera上统一入门课程。我已经完成了第一个编程任务,即求两点的斜边和角度。我几乎把代码弄对了,但有几个答案是对的,有几个是错的!我不知道怎么了

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ProgrammingAssignment1
{
    // IMPORTANT: Only add code in the section
    // indicated below. The code I've provided
    // makes your solution work with the 
    // automated grader on Coursera

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

        /// <summary>
        /// Calculates angle and distance between two points
        /// </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);
                float angleInDegrees = angleInRadians * 180 / (float)Math.PI - 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

        }
    }
}

这是两条求角的线。所以当我输入点5 5 4 4时,它给了我正确的角度,第二行是Math.PI-180,但同时当我输入2 2 4 4时,它给了我错误的答案

所以我玩了一会儿,加上Math.PI+180,所以这次我得到了2 2 4 4的直角和5 5 4 4的错误答案。我只是不知道我哪里出错了

float angleInRadians = (float)Math.Atan2(deltaY, deltaX);
float angleInDegrees = angleInRadians * 180 / (float)Math.PI - 180;
非常感谢您的帮助

试试这个:

//radians into degrees
var angle = (radians * (180 / Math.PI) + 360) % 360;

预期的结果是什么?您得到的实际结果是什么?这与Unity3d有什么关系?见TonyMkenu的回答。检查Math.Atan2(value)的文档,我想这会让您惊讶于它如何处理作为参数传入的正负X和Y值。通常,人们会创建可重用的函数,如degreestoradins()和degreestoradins()。拥有独立的功能也会让你更容易测试从0到360度的角度会发生什么。@derHugo从来没有说过这个程序与unity有关,只是说我在做这个course@Rethunk谢谢,我一定会小心的!感谢公式的有效性,我对它进行了一些调整,最后添加了-180,得到了正确的答案!