Converting 3D polar coordinates to cartesian coordinates
Categories:
Mastering 3D Coordinate Transformations: Polar to Cartesian

Unlock the secrets of 3D space by learning how to convert polar coordinates (radius, azimuth, elevation) into their Cartesian (x, y, z) equivalents. This guide covers the mathematical principles and provides practical code examples.
Understanding how to convert between different coordinate systems is fundamental in fields like computer graphics, physics, robotics, and engineering. While Cartesian coordinates (x, y, z) are intuitive for many applications, polar coordinates (often spherical polar in 3D) offer a more natural way to describe positions relative to a central point, especially when dealing with rotations or radial distances. This article will guide you through the mathematical formulas and provide practical code implementations for converting 3D spherical polar coordinates to Cartesian coordinates.
Understanding 3D Spherical Polar Coordinates
In a 3D spherical polar coordinate system, a point in space is defined by three values:
- Radius (r or ρ): The distance from the origin to the point. This value is always non-negative.
- Azimuthal Angle (θ or φ): The angle in the XY-plane from the positive X-axis to the projection of the point onto the XY-plane. This angle typically ranges from 0 to 2π radians (0° to 360°).
- Polar Angle (φ or θ): The angle from the positive Z-axis to the point itself. This angle typically ranges from 0 to π radians (0° to 180°).
It's important to note that conventions for the azimuthal and polar angles can vary. Some systems swap the definitions of θ and φ, or measure the polar angle from the XY-plane instead of the Z-axis. For this article, we will use the convention where r
is the radius, θ
(theta) is the azimuthal angle (XY-plane), and φ
(phi) is the polar angle (from Z-axis).
graph TD A["3D Spherical Polar Coordinates"] --> B["Radius (r)"] A --> C["Azimuthal Angle (θ)"] A --> D["Polar Angle (φ)"] B --> E["Distance from Origin"] C --> F["Angle in XY-plane (from +X-axis)"] D --> G["Angle from +Z-axis"] E & F & G --> H["Define Point in 3D Space"]
Components of 3D Spherical Polar Coordinates
The Conversion Formulas
To convert from spherical polar coordinates (r, θ, φ) to Cartesian coordinates (x, y, z), we use the following trigonometric relationships:
- x = r * sin(φ) * cos(θ)
- y = r * sin(φ) * sin(θ)
- z = r * cos(φ)
Let's break down why these formulas work:
r * cos(φ)
gives the projection of the radiusr
onto the Z-axis, which directly yieldsz
.r * sin(φ)
gives the projection of the radiusr
onto the XY-plane. Let's call this projected radiusr_xy
.- In the XY-plane,
r_xy
andθ
form a 2D polar coordinate system. Therefore,x = r_xy * cos(θ)
andy = r_xy * sin(θ)
. - Substituting
r_xy = r * sin(φ)
into the 2D formulas gives us the final 3D Cartesianx
andy
components.
Math.sin
, Math.cos
in JavaScript, math.sin
, math.cos
in Python) expect angles in radians.Practical Implementation in Code
Below are examples of how to implement this conversion in various programming languages. We'll assume r
is the radius, theta
is the azimuthal angle, and phi
is the polar angle, all in radians.
Python
import math
def polar_to_cartesian_3d(r, theta, phi): """ Converts 3D spherical polar coordinates to Cartesian coordinates. Args: r (float): Radius (distance from origin). theta (float): Azimuthal angle in radians (from +X-axis in XY-plane). phi (float): Polar angle in radians (from +Z-axis). Returns: tuple: (x, y, z) Cartesian coordinates. """ x = r * math.sin(phi) * math.cos(theta) y = r * math.sin(phi) * math.sin(theta) z = r * math.cos(phi) return x, y, z
Example usage:
radius = 10.0 azimuth_angle = math.pi / 4 # 45 degrees polar_angle = math.pi / 3 # 60 degrees
x, y, z = polar_to_cartesian_3d(radius, azimuth_angle, polar_angle) print(f"Cartesian coordinates: x={x:.2f}, y={y:.2f}, z={z:.2f}")
Expected output: x=3.54, y=3.54, z=5.00
JavaScript
function polarToCartesian3D(r, theta, phi) { /**
- Converts 3D spherical polar coordinates to Cartesian coordinates.
- @param {number} r - Radius (distance from origin).
- @param {number} theta - Azimuthal angle in radians (from +X-axis in XY-plane).
- @param {number} phi - Polar angle in radians (from +Z-axis).
- @returns {object} - {x, y, z} Cartesian coordinates. */ const x = r * Math.sin(phi) * Math.cos(theta); const y = r * Math.sin(phi) * Math.sin(theta); const z = r * Math.cos(phi); return { x, y, z }; }
// Example usage: const radius = 10.0; const azimuthAngle = Math.PI / 4; // 45 degrees const polarAngle = Math.PI / 3; // 60 degrees
const { x, y, z } = polarToCartesian3D(radius, azimuthAngle, polarAngle);
console.log(Cartesian coordinates: x=${x.toFixed(2)}, y=${y.toFixed(2)}, z=${z.toFixed(2)}
);
// Expected output: Cartesian coordinates: x=3.54, y=3.54, z=5.00
C#
using System;
public class CoordinateConverter { public static (double x, double y, double z) PolarToCartesian3D(double r, double theta, double phi) { // Converts 3D spherical polar coordinates to Cartesian coordinates. // r: Radius (distance from origin). // theta: Azimuthal angle in radians (from +X-axis in XY-plane). // phi: Polar angle in radians (from +Z-axis). double x = r * Math.Sin(phi) * Math.Cos(theta); double y = r * Math.Sin(phi) * Math.Sin(theta); double z = r * Math.Cos(phi); return (x, y, z); }
public static void Main(string[] args)
{
// Example usage:
double radius = 10.0;
double azimuthAngle = Math.PI / 4; // 45 degrees
double polarAngle = Math.PI / 3; // 60 degrees
(double x, double y, double z) = PolarToCartesian3D(radius, azimuthAngle, polarAngle);
Console.WriteLine($"Cartesian coordinates: x={x:F2}, y={y:F2}, z={z:F2}");
// Expected output: Cartesian coordinates: x=3.54, y=3.54, z=5.00
}
}
theta
for azimuthal and phi
for polar angle is common but not universal. Always double-check the convention used in your specific domain or library documentation. If phi
is measured from the XY-plane (elevation angle), the formulas will change slightly: z = r * sin(phi)
and r_xy = r * cos(phi)
.