如何使用 Atan2() 计算角度(以度为单位)

how to calculate the angle in degrees using Atan2()

我需要你帮助我这里的这段代码:

    Console.WriteLine();
    Console.WriteLine("Enter first X value: ");
    float point1X = float.Parse(Console.ReadLine());
    Console.WriteLine("Enter first Y value: ");
    float point1Y = float.Parse(Console.ReadLine());
    Console.WriteLine("Enter second X value: ");
    float point2X = float.Parse(Console.ReadLine());
    Console.WriteLine("Enter second Y value: ");
    float point2Y = float.Parse(Console.ReadLine());
    Console.WriteLine();

        double deltaX = point2X - point1X;
        double deltaY = point2Y - point1Y;
        double distance = Math.Sqrt(Math.Pow(deltaX, 2) + Math.Pow(deltaY,2)) ;

double angleX = Math.Atan2(point1X,point1Y);

        Console.WriteLine("DeltaX value is: " + deltaX);
        Console.WriteLine("DeltaY value is: " + deltaY);
        Console.WriteLine("The distance is: " + distance);
        Console.WriteLine("The angle is: " + angle + "°");

    }
}

基本上,我需要与其他部分分开的那条线的帮助。我想计算两点之间的角度并以度数打印。 我知道这是一个非常简单的代码,但我必须将它交付给合格的工作。

注1:我必须使用Atan2()。

注2:我要计算两点向量的夹角

注意 3:这是我需要的一些糟糕的绘图

https://i.stack.imgur.com/31tvB.png

注4:用户输入2个坐标,一个用于第一个点,一个用于第二个点。我需要计算的是两个点的向量之间的角度。

角度可以通过以下方式测量

Math.Atan2(val1, val2) * (180 / Math.PI);

来源:https://www.geeksforgeeks.org/c-sharp-math-atan2-method/

主要工作在这个方法中完成:

static double Angle(double x1, double y1, double x2, double y2)
{
    double angle1 = Math.Atan2(y1, x1);
    double angle2 = Math.Atan2(y2, x2);
    return Math.Abs(angle1 - angle2) * 180 / Math.PI;
}

结合你的代码,变成:

class Program
{
    static double Angle(double x1, double y1, double x2, double y2)
    {
        double angle1 = Math.Atan2(y1, x1);
        double angle2 = Math.Atan2(y2, x2);
        double angle = Math.Abs(angle1 - angle2) * 180 / Math.PI;
        return angle;
    }

    static void Main(string[] args)
    {
        Console.WriteLine();
        Console.Write("Enter first X value: ");
        float point1X = float.Parse(Console.ReadLine());
        Console.Write("Enter first Y value: ");
        float point1Y = float.Parse(Console.ReadLine());
        Console.Write("Enter second X value: ");
        float point2X = float.Parse(Console.ReadLine());
        Console.Write("Enter second Y value: ");
        float point2Y = float.Parse(Console.ReadLine());

        Console.WriteLine($"\nAngle between these points is {Angle(point1X, point1Y, point2X, point2Y)} degrees.");
    }
}

我没弄清楚你需要什么,但我想这个可以解决你的问题:

Math.Atan2(Math.Abs(point1Y-point2Y), Math.Abs(point1X-point2X)) * 180 / Math.PI;

我把最后的代码留在这里,也许有人会发现它有用:

Console.WriteLine("Enter first X value: ");
        float point1X = float.Parse(Console.ReadLine());
        Console.WriteLine("Enter first Y value: ");
        float point1Y = float.Parse(Console.ReadLine());
        Console.WriteLine("Enter second X value: ");
        float point2X = float.Parse(Console.ReadLine());
        Console.WriteLine("Enter second Y value: ");
        float point2Y = float.Parse(Console.ReadLine());
        Console.WriteLine();

    double deltaX = point2X - point1X;
    double deltaY = point2Y - point1Y;
    double distance = Math.Sqrt(Math.Pow(deltaX, 2) + Math.Pow(deltaY,2));
    double angle1 = Math.Atan2(point1Y,point1X);
    double angle2 = Math.Atan2(point2Y,point2X);
    double angleT = Math.Abs(angle1 - angle2) * 180 / Math.PI;
    double point1 = point1X + point1Y;
    double point2 = point2X + point2Y;

    if (point1 + 2 == point2)
    {
        Console.WriteLine("DeltaX value is: " + deltaX);
        Console.WriteLine("DeltaY value is: " + deltaY);
        Console.WriteLine("The distance is: " + distance);
        Console.WriteLine("The angle is: -135°");
    }
    else if (point2 + 2 == point1)
    {
        Console.WriteLine("DeltaX value is: " + deltaX);
        Console.WriteLine("DeltaY value is: " + deltaY);
        Console.WriteLine("The distance is: " + distance);
        Console.WriteLine("The angle is: -135°");
    }
    else
    {
        Console.WriteLine("DeltaX value is: " + deltaX);
        Console.WriteLine("DeltaY value is: " + deltaY);
        Console.WriteLine("The distance is: " + distance);
        Console.WriteLine("The angle is: " + angleT + "°");

    }