简体   繁体   English

在C#中编码距离和角度

[英]Coding for Distance and angle in c#

I am working on my first program in C# and I'm having problems getting my equations for distance and angle to calculate. 我正在用C#开发第一个程序,但在获取距离和角度方程式时遇到了问题。 My purpose is to prompt for an X1,Y1 value, an X2, Y2 value, and calculate the distance and angle between the two points. 我的目的是提示输入X1,Y1值,X2,Y2值,并计算两点之间的距离和角度。 I will paste my (very horrible) code below. 我将在下面粘贴我的代码(非常糟糕)。 Thanks in advance for any help! 在此先感谢您的帮助!

        //prompt and store the x and y for two points
        Console.WriteLine("Please enter X1:");
        float point1X = float.Parse(Console.ReadLine());
        Console.WriteLine("Please enter Y1:");
        float point1Y = float.Parse(Console.ReadLine());
        Console.WriteLine("Please enter X2:");
        float point2X = float.Parse(Console.ReadLine());
        Console.WriteLine("Please enter Y2:");
        float point2Y = float.Parse(Console.ReadLine());
        Console.WriteLine("");

        //calculate the distance
        double deltaX= Math.Pow(point2X - point1X, 2);
        double deltaY= Math.Pow(point2Y - point1Y, 2);
        Console.WriteLine("double deltaX, double deltaY");
        Console.WriteLine("");

        //calculate the angle
        double angle = Math.Atan2(deltaX, deltaY);
        Console.WriteLine("double angle");
        Console.WriteLine("");

If this is something like this you searched for, this is Mathematics, not C# ;-) : 如果这是您要搜索的内容,则为数学,而不是C#;-):

        //calculate the distance
        ...
        Console.WriteLine("Distance : " + Math.Sqrt(deltaX + deltaY));

        //calculate the angle
        double angle = Math.Atan2(point2Y - point1Y, point2X - point1X);
        ...
        Console.WriteLine(angle + " Rad");
        Console.WriteLine(angle * 180 / Math.PI + " Deg");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM