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.
xxxxxxxxxx
1
using System;
2
3
public class Program
4
{
5
static double CalculateAngle(double a, double b)
6
{
7
return Math.Atan(a / b);
8
}
9
10
public static void Main(string[] args)
11
{
12
/*
13
|\
14
| \ h
15
a | \
16
|__*\ <- angle
17
b
18
*/
19
20
double a, b;
21
22
// a an b build isosceles right triangle
23
a = 3;
24
b = a;
25
Console.WriteLine( CalculateAngle(a, b)); // 0.7853981633974483 <- ~45 degrees
26
27
// a and b build half of equilateral triangle
28
a = 3;
29
b = a * Math.Sqrt(3);
30
Console.WriteLine( CalculateAngle(a, b)); // 0.5235987755982988 <- ~30 degrees
31
32
// a and b build very high (+Inf) and slim (~0) triangle
33
a = Double.PositiveInfinity;
34
b = 0;
35
Console.WriteLine( CalculateAngle(a, b)); // 1.5707963267948966 <- ~90 degrees
36
}
37
}
Syntax |
xxxxxxxxxx 1 namespace System 2 { 3 public static class Math 4 { 5 // ... 6 public static double Atan(double number) { ... } 7 // ... 8 } 9 } |
Parameters |
|
Result |
If value can not be calculated |
Description |
|
xxxxxxxxxx
1
using System;
2
3
public class Program
4
{
5
static double CalculateAngle(double a, double b)
6
{
7
double angle = Math.Atan(a / b);
8
9
return (180 / Math.PI) * angle; // rad to deg conversion
10
}
11
12
public static void Main(string[] args)
13
{
14
/*
15
|\
16
| \ h
17
a | \
18
|__*\ <- angle
19
b
20
*/
21
22
double a, b;
23
24
// a an b build isosceles right triangle
25
a = 3;
26
b = a;
27
Console.WriteLine(CalculateAngle(a, b)); // ~45 degrees
28
29
// a and b build half of equilateral triangle
30
a = 3;
31
b = a * Math.Sqrt(3);
32
Console.WriteLine(CalculateAngle(a, b)); // ~30 degrees
33
34
// a and b build very high (+Inf) and slim (~0) triangle
35
a = Double.PositiveInfinity;
36
b = 0;
37
Console.WriteLine(CalculateAngle(a, b)); // ~90 degrees
38
}
39
}