A pure C# library for working with spherical coordinates. No external dependencies except for Microsoft's unit testing, built for .NET 9.0.
This code is free to use under the MIT.
Spherical coordinates provide a way to represent points in three-dimensional space using a radius, an azimuthal angle, and a polar angle. Unlike Cartesian coordinates (x, y, z), which define a point by horizontal and vertical displacement, spherical coordinates define a point as:
- Radius (r): The distance from the origin to the point.
- Theta (θ): The azimuthal angle (in radians) measured counterclockwise from the positive x-axis in the xy-plane.
- Phi (φ): The polar angle (in radians) measured from the positive z-axis.
- Theta (θ) is measured in radians and represents the angle around the z-axis.
- Phi (φ) is measured in radians from the positive z-axis downward.
- The angle ranges are typically:
- θ in [0, 2π)
- φ in [0, π]
This Spherical
class provides methods to work with spherical coordinates, including conversions, measurements, and calculations.
public Spherical(double radius, double theta, double phi)
- Initializes a
Spherical
object with a specified radius, azimuthal angle, and polar angle.
public static Spherical FromCartesian(Vector3 point)
- Converts a Cartesian coordinate (x, y, z) into spherical coordinates.
- Returns
NaN
values if the input containsNaN
values. - Uses the formula:
r = sqrt(x² + y² + z²)
θ = atan2(y, x)
φ = acos(z / r)
public Vector3 ToCartesian()
- Converts a spherical coordinate to Cartesian form (x, y, z).
- Uses the formulas:
x = r * sin(φ) * cos(θ)
y = r * sin(φ) * sin(θ)
z = r * cos(φ)
public static double SurfaceArea(double radius)
- Computes the surface area of a sphere.
- Formula:
Surface Area = 4πr²
. - Returns
NaN
if the radius is negative.
public static double Volume(double radius)
- Computes the volume of a sphere.
- Formula:
Volume = (4/3)πr³
. - Returns
NaN
if the radius is negative.
public static double GreatCircleDistance(double radius, double phi1, double theta1, double phi2, double theta2)
- Computes the shortest distance between two points on a sphere.
- Uses the Haversine formula.
- Returns
NaN
if any input isNaN
.
public static double SphericalCap(double radius, double height)
- Computes the surface area of a spherical cap.
- Formula:
Cap Area = 2πr h
. - Returns
NaN
if any input isNaN
.
public static double SphericalZone(double radius, double height1, double height2)
- Computes the surface area of a spherical zone.
- Formula:
Zone Area = 2πr |h₂ - h₁|
. - Returns
NaN
if any input isNaN
.
public static double SphericalSegmentVolume(double radius, double height)
- Computes the volume of a spherical segment.
- Formula:
Volume = (π h² (3r - h)) / 3
. - Returns
NaN
if any input isNaN
.
public static double SphericalSectorVolume(double radius, double phi)
- Computes the volume of a spherical sector.
- Formula:
Volume = (2/3)πr³(1 - cos(φ))
. - Returns
NaN
if any input isNaN
.
Vector3 point = new Vector3(3, 4, 5);
Spherical spherical = Spherical.FromCartesian(point);
Console.WriteLine($"Radius: {spherical.Radius}, Theta: {spherical.Theta}, Phi: {spherical.Phi}");
Vector3 cartesian = spherical.ToCartesian();
Console.WriteLine($"Converted back: ({cartesian.X}, {cartesian.Y}, {cartesian.Z})");
This library provides robust mathematical operations for handling spherical coordinates, including conversions, distance calculations, and curvature analysis.
Copyright TranscendAI.tech 2025.
Authored by Warren Harding. AI assisted.