cartesianToSpherical property

List<num> cartesianToSpherical

Transforms Cartesian coordinates [x, y, z] to Polar Spherical coordinates [r, theta, phi].

  • first coordinate: x (the first horizontal coordinate),
  • second coordinate: y (the second horizontal coordinate),
  • third coordinate: z (the vertical coordinate).

Implementation

List<num> get cartesianToSpherical {
  if (this[0] == 0 && this[1] == 0) {
    // Origin
    if (this[2] == 0) {
      return [0, 0, 0];
    }
    // Point on z-axis
    return [this[2].abs(), this[2].isNegative ? pi : 0, 0];
  }

  // Rest of points
  num rxy2 = this[0] * this[0] + this[1] * this[1];
  num r = sqrt(rxy2 + this[2] * this[2]);
  return [r, acos(this[2] / r), this[1].sign * acos(this[0] / sqrt(rxy2))];
}