PIXELBANKv9.1.0
Menu

Sphere Signed Distance Function

Implement a function to query the signed distance function (SDF) for a sphere, which is crucial in 3D reconstruction and surface representations. This function calculates the distance from any point in space to the nearest surface of the sphere.

The concept of SDF is essential in understanding how to represent and manipulate 3D objects, as it enables powerful operations like Boolean combinations, morphing, and ray marching. The SDF for a sphere can be understood as the difference between the Euclidean distance from a point to the sphere's center and the sphere's radius.

To compute the SDF for a sphere, consider the following steps:

  1. Calculate the Euclidean distance between the query point pp and the sphere's center cc.
  2. Subtract the sphere's radius rr from this distance to obtain the signed distance.
SDFsphere(p)=∥p−c∥−rSDF_{sphere}(p) = \|p - c\| - r

This technique is widely used in computer vision and graphics applications.

Example:

Input:
sphere_sdf([2, 0, 0], [0, 0, 0], 1)
Output:
1.0
Reasoning:

Point at [2,0,0], sphere centered at origin with radius 1: distance to center = sqrt(2² + 0² + 0²) = 2

  • SDF = 2 - 1 = 1.0 Point is 1 unit outside the sphere.

Constraints:

  • point: query point [x, y, z]
  • center: sphere center [cx, cy, cz]
  • radius: sphere radius
  • Return signed distance, rounded to 4 decimal places
🔒

Editor locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test Results

0/0
Run code to see test results.