Languages
[Edit]
EN

C# / .NET - Math.Atan() method example

0 points
Created by:
Dirask Admin - Marwin
1460

The Math.Atan function returns number in radians in the range -Math.PI/2 to +Math.PI/2.Ā The function calculates the inverted tangent function value.

using System;

public class Program
{
    static double CalculateAngle(double a, double b)
    {
        return Math.Atan(a / b);
    }

    public static void Main(string[] args)
    {
        /*
          |\\\\
          | \\\\ h
        a |  \\\\
          |__*\\\\ <- angle
            b
        */

        double a, b;

        // a an b build isosceles right triangle
        a = 3;
        b = a;
        Console.WriteLine(  CalculateAngle(a, b)); // 0.7853981633974483 <- ~45 degrees

        // a and b build half of equilateral triangle
        a = 3;
        b = a * Math.Sqrt(3);
        Console.WriteLine(  CalculateAngle(a, b)); // 0.5235987755982988 <- ~30 degrees

        // a and b build very high (+Inf) and slim (~0) triangle
        a = Double.PositiveInfinity;
        b = 0;
        Console.WriteLine(  CalculateAngle(a, b)); // 1.5707963267948966 <- ~90 degrees
    }
}

1. Documentation

Syntax
namespace System
{
    public static class Math
    {
        // ...
        public static double Atan(double number) { ... }
        // ...
    }
}
Parameters

number - doubleĀ number value that represents the result of the operationĀ opposite / adjacentĀ on the right triangle (primitive value).

Result

numberĀ value in radians in the range -Math.PI/2Ā to +Math.PI/2Ā (primitive value).

If value can not be calculatedĀ NaNĀ is returned.

Description

AtanĀ is a static method that takes only one parameter and returns an approximation of the result of the mathematical function arctangent(x).

2. Working with degrees

using System;

public class Program
{
    static double CalculateAngle(double a, double b)
    {
        double angle = Math.Atan(a / b);

        return (180 / Math.PI) * angle; // rad to deg conversion
    }

    public static void Main(string[] args)
    {
        /*
          |\\\\
          | \\\\ h
        a |  \\\\
          |__*\\\\ <- angle
            b
        */

        double a, b;

        // a an b build isosceles right triangle
        a = 3;
        b = a;
        Console.WriteLine(CalculateAngle(a, b)); // ~45 degrees

        // a and b build half of equilateral triangle
        a = 3;
        b = a * Math.Sqrt(3);
        Console.WriteLine(CalculateAngle(a, b)); // ~30 degrees

        // a and b build very high (+Inf) and slim (~0) triangle
        a = Double.PositiveInfinity;
        b = 0;
        Console.WriteLine(CalculateAngle(a, b)); // ~90 degrees
    }
}

References

  1. Inverse trigonometric functions - Wikipedia

C# / .NET - Math object

Native Advertising
šŸš€
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

ā¤ļøšŸ’» šŸ™‚

Join