Rotate a 3D Point Around the Z-Axis

3D GeometryMedium+15 XP+15

Rotate a 3D Point Around the Z-Axis

Spinning a point around the z-axis is a 2D rotation in disguise: the x and y coordinates swirl around, and z doesn't move at all — it's the axis you're turning about, like a bead on a skewer.

x′=xcos⁡θ−ysin⁡θ,y′=xsin⁡θ+ycos⁡θ,z′=zx' = x\cos\theta - y\sin\theta, \qquad y' = x\sin\theta + y\cos\theta, \qquad z' = zx′=xcosθ−ysinθ,y′=xsinθ+ycosθ,z′=z

Task: write rotate_z(point, degrees) returning the rotated point [x', y', z'], each coordinate rounded to 4 decimal places.

  • point is [x, y, z].
  • degrees is in degrees — convert to radians with math.radians before using sin and cos, because they expect radians.
  • Positive angles rotate counter-clockwise looking down the z-axis from above: a point on the positive x-axis swings toward the positive y-axis. Negative angles go the other way.
  • Return floats for all three coordinates, z included.

Two details worth noticing. The minus sign sits in the x' formula only — swap it onto y' and you've silently built a rotation that goes the wrong way. And the rotation preserves distance from the axis: x'² + y'² always equals x² + y², which is the cleanest way to check your implementation on any input at all.

Examples

point = [1, 0, 0]
degrees = 90
returns [0.0, 1.0, 0.0]
point = [1, 0, 5]
degrees = 180
returns [-1.0, 0.0, 5.0]

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run