EN
C# / .NET - Math.Atan() method example
0
points
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 |
|
Parameters |
|
Result |
If value can not be calculated |
Description |
|
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
}
}