find the volume of a regular polygon circumscribed around a sphere in csharp

To find the volume of a regular polygon circumscribed around a sphere, we need to know the side length of the polygon and the radius of the sphere. Here's a C# method that calculates the volume using the formula:

main.cs
V = (4/3) * Pi * R^3 * N / 3 * Tan(Pi/N)
41 chars
2 lines

Where V is the volume, Pi is the mathematical constant pi (~3.14), R is the radius of the sphere, N is the number of sides of the polygon, and Tan() is the tangent function.

main.cs
public static double RegularPolygonSphereVolume(int sides, double radius)
{
    const double pi = 3.14159;
    double tanValue = Math.Tan(pi / sides);
    double volume = (4.0 / 3.0) * pi * Math.Pow(radius, 3) * sides * tanValue / 3.0;
    return volume;
}
257 chars
8 lines

You can call this method with the number of sides of your regular polygon and the radius of your sphere to get the volume of the polygon:

main.cs
int numSides = 6;
double radius = 5.0;
double volume = RegularPolygonSphereVolume(numSides, radius);
Console.WriteLine($"The volume of a regular {numSides}-sided polygon circumscribed around a sphere with radius {radius} is {volume:F2}");
239 chars
5 lines

Output:

main.cs
The volume of a regular 6-sided polygon circumscribed around a sphere with radius 5 is 309.02
94 chars
2 lines

gistlibby LogSnag