toSpherical method
Converts the Vector from Cartesian to Spherical coordinates.
Returns a list of doubles r, theta, phi
where:
- r is the radius (distance from origin)
- theta is the inclination (angle from the z-axis, in range
0, pi
) - phi is the azimuth (angle from the x-axis in the xy-plane, in range
0, 2*pi
)
Example:
Vector v = Vector.fromList([1, 1, 1]);
print(v.toSpherical());
// Output: [1.7320508075688772, 0.9553166181245093, 0.7853981633974483]
Implementation
List<double> toSpherical() {
if (length != 3) {
throw Exception(
"Vector must be 3D for conversion to Spherical coordinates");
}
double x = _data[0].toDouble();
double y = _data[1].toDouble();
double z = _data[2].toDouble();
double r = math.sqrt(x * x + y * y + z * z);
double theta = math.acos(z / r);
double phi = math.atan2(y, x);
return [r, theta, phi];
}