Initializing 3D Canvas...

The Sphere

1 min read1 page

Perfect Symmetry

A Sphere is the set of all points in 3D space that are at a fixed distance (the radius) from a single point (the center). In the world of computational geometry, the sphere is the ultimate symbol of symmetry and efficiency.

Unlike a Mesh, which is an approximation made of flat faces, a mathematical Sphere is infinitely smooth.

Implicit vs. Explicit: A Mesh Sphere is explicit(made of points). A Mathematical Sphere is implicit(defined by x² + y² + z² = r²). Implicit geometry never loses precision.
Radius
2.00
1 min read1 page

Data Structure

Because of its mathematical perfection, a Sphere requires almost no memory. To define a sphere in a CAD system, you only need:

  1. Center: A Point3d.
  2. Radius: A single double (floating point number).
python
1class Sphere:
2 """
3 Represents a mathematical sphere.
4 """
5 def __init__(self, center: Point3d, radius: float):
6 self.Center = center
7 self.Radius = radius

PointAt(u, v):

Longitude (U) and Latitude (V) to 3D point.
python
1def PointAt(self, u_degrees: float, v_degrees: float) -> Point3d:
2 """Maps longitude (u) and latitude (v) to a 3D Point."""
3 u_rad = math.radians(u_degrees)
4 v_rad = math.radians(v_degrees)
5 x = self.Radius * sin(v_rad) * cos(u_rad)
6 y = self.Radius * sin(v_rad) * sin(u_rad)
7 z = self.Radius * cos(v_rad)
8 return Point3d(x + self.Center.X, y + self.Center.Y, z + self.Center.Z)
While a Sphere is mathematically perfect, rendering engines still need to "unwrap" it to apply textures. We use UV Mapping, where U maps to Longitude and V maps to Latitude. This mapping allows us to treat a 3D sphere as a 2D flat image, though it inevitably creates distortion at the North and South poles.The Singularities: At the exact top and bottom of a sphere (the poles), all longitudinal lines converge to a single point. This creates "Tris" or "Poles" that can cause issues during subdivision or smoothing.
Longitude (U)
45.00
Latitude (V)
45.00
1 min read1 page

Translate(vector):

Moving a sphere is computationally trivial — you only need to add a translation vector to its Center point.

python
1def Translate(self, vector: 'Vector3d'):
2 """Moving a sphere only requires moving its Center point."""
3 self.Center += vector
Move X
2.00
Move Y
1.00
1 min read1 page

Scale(factor):

Scaling a sphere is mathematically elegant — you simply multiply theRadius by a scalar factor.
python
1def Scale(self, factor: float):
2 """Scaling a sphere is simply multiplying its Radius."""
3 self.Radius *= factor
Scale Factor
1.50
1 min read1 page

Rotate(axis, angle):

A mathematical sphere is rotationally invariant. Rotating it around its center point changes absolutely nothing about its mathematical representation (Center + Radius).
In 3D modeling, we only rotate a sphere if it has a texture (like the Earth) or if we are tracking its local orientation axes.
2 min read1 page

Compute(sphere):

A sphere has two key analytical properties computable from the radius alone — no numerical integration needed. Volume is (4/3)πr³ and surface area is 4πr². These are exact, unlike mesh-based approximations. Essential for material estimation, hydrostatic calculations, and structural BIM data.
python
1import math
2
3def SphereVolume(sphere: Sphere) -> dict:
4 """Computes exact analytical properties of a sphere."""
5 r = sphere.Radius
6
7 volume = (4 / 3) * math.pi * r ** 3
8 surface_area = 4 * math.pi * r ** 2
9
10 # Or use the unified API:
11 props = VolumeMassProperties.Compute(sphere)
12 return { "volume": props.Volume, "centroid": props.Centroid }
Radius
2.00
3 min read1 page

Intersection:

Sphere intersections (like Ray-Sphere) are mathematically beautiful because they can be solved perfectly using the standard quadratic formula, avoiding the need for complex polygon meshes. This makes spheres incredibly efficient for raytracing.
python
1def RaySphereIntersection(ray: 'Ray3d', sphere: 'Sphere') -> list['Point3d']:
2 """Finds intersection points between a ray and a sphere."""
3
4 # Solved using the quadratic formula: at^2 + bt + c = 0
5 # where t is the distance along the ray
6
7 L = ray.Position - sphere.Center
8 a = Vector3d.DotProduct(ray.Direction, ray.Direction)
9 b = 2.0 * Vector3d.DotProduct(ray.Direction, L)
10 c = Vector3d.DotProduct(L, L) - (sphere.Radius ** 2)
11
12 discriminant = b*b - 4*a*c
13
14 if discriminant < 0:
15 return [] # No intersection
16 elif discriminant == 0:
17 t = -b / (2*a)
18 return [ray.PointAt(t)] # One hit (tangent)
19 else:
20 t1 = (-b + math.sqrt(discriminant)) / (2*a)
21 t2 = (-b - math.sqrt(discriminant)) / (2*a)
22 return [ray.PointAt(t1), ray.PointAt(t2)] # Two hits
Move Ray X
0.00
4 min read1 page

SphereSphere(a, b):

Two overlapping spheres intersect along a circle — the boundary where their volumes cross. The position and radius of this circle depends on the radii of both spheres and the distance between their centers. This is used in wave interference simulations, dome structural overlap analysis, and computational biology (atom packing).
python
1def SphereSphereIntersect(a: Sphere, b: Sphere) -> Circle:
2 """Calculates the circle of intersection between two spheres."""
3 # Vector from center A to center B
4 dx = b.Center.X - a.Center.X
5 dy = b.Center.Y - a.Center.Y
6 dz = b.Center.Z - a.Center.Z
7 d = math.sqrt(dx**2 + dy**2 + dz**2)
8
9 # Check for no intersection
10 if d > (a.Radius + b.Radius) or d < abs(a.Radius - b.Radius) or d == 0:
11 return None
12
13 # Distance from center A to the circle plane
14 x = (d**2 - b.Radius**2 + a.Radius**2) / (2 * d)
15
16 # Radius of the intersection circle
17 circle_radius = math.sqrt(a.Radius**2 - x**2)
18
19 # Center of the intersection circle
20 cx = a.Center.X + (dx / d) * x
21 cy = a.Center.Y + (dy / d) * x
22 cz = a.Center.Z + (dz / d) * x
23 circle_center = Point3d(cx, cy, cz)
24
25 # Normal of the circle's plane
26 normal = Vector3d(dx/d, dy/d, dz/d)
27
28 return Circle(circle_center, normal, circle_radius)
Center Distance
2.50
1 min read1 page

Contains:

Checking if a point is inside a sphere is as simple as calculating the distance from the point to the sphere's center, and checking if that distance is less than the sphere's radius.
python
1def SphereContains(sphere: 'Sphere', pt: 'Point3d', strict: bool) -> bool:
2 """Checks if a point is inside a sphere."""
3
4 # Calculate the distance from the point to the sphere's center
5 dist = pt.DistanceTo(sphere.Center)
6
7 # Compare with radius
8 if strict:
9 return dist < sphere.Radius
10 else:
11 return dist <= sphere.Radius
Point X
1.50
Point Y
1.00
2 min read1 page

NormalAt(u, v):

Calculates the normal vector at a given latitude and longitude.
python
1def NormalAt(self, u_degrees: float, v_degrees: float) -> Vector3d:
2 """Returns the normal vector pointing outward from the surface at a UV coordinate."""
3 # Calculate the point on the sphere surface
4 u_rad = math.radians(u_degrees)
5 v_rad = math.radians(v_degrees)
6 x = sin(v_rad) * cos(u_rad)
7 y = sin(v_rad) * sin(u_rad)
8 z = cos(v_rad)
9
10 # The normal of a sphere is simply the normalized vector from the center to the point
11 return Vector3d(x, y, z)
Since a sphere curves uniformly, the normal vector at any surface point always points straight out from the center.
Longitude (U)
60.00
Latitude (V)
45.00
3 min read1 page

Tangent Plane:

Constructs a mathematically flat plane that perfectly touches the curved surface at a single point.
python
1import math
2
3# Sphere parametrization:
4# u = longitude (0 to 2PI), v = latitude (-PI/2 to PI/2)
5
6def GetTangentPlane(sphere: Sphere, u: float, v: float) -> Plane:
7 """Returns the tangent plane at a specific (u,v) coordinate on the sphere."""
8 # Get the 3D point
9 pt = sphere.PointAt(u, v)
10
11 # The normal of a sphere always points from the center to the surface point
12 normal = sphere.NormalAt(u, v)
13
14 # Create a plane at that point, facing the normal direction
15 tangent_plane = Plane(pt, normal)
16
17 return tangent_plane
A "Tangent Plane" is the flat approximation of a curved surface at a specific location. It acts as a local coordinate system that sits exactly on the surface, with its Z-axis pointing straight out (the normal). This is essential for placing objects onto spheres. If you want to place a solar panel on a dome, or a connection node on a geodesic structure, you use the tangent plane to orient the object correctly against the curvature.
U: Longitude (deg)
45.00
V: Latitude (deg)
30.00
2 min read1 page

Great Circle:

The shortest path between two points on the surface of a sphere.
python
1def CreateGreatCircle(sphere: Sphere, ptA: Point3d, ptB: Point3d) -> Circle:
2 """Creates the great circle passing through two points on a sphere."""
3
4 # A Great Circle is created by slicing the sphere with a plane
5 # that passes through the sphere's center and the two target points.
6
7 # Plane through 3 points: Center, A, B
8 plane = Plane(sphere.Center, ptA, ptB)
9
10 # The great circle has the exact same radius as the sphere
11 great_circle = Circle(plane, sphere.Radius)
12
13 return great_circle
If you draw a straight 3D line between two cities on Earth, the line goes underground through the planet's crust. To travel on the surface, you must follow a curved path. The shortest possible curved path is called a "Geodesic" or "Great Circle". A Great Circle is formed by slicing the sphere with a plane that passes directly through the sphere's absolute center. This geometry is the basis for global flight paths, dome construction, and geodesic grid generation.
Move Destination
45.00
1 min read1 page

ClosestPoint:

Finding the point on a sphere that is closest to an external point is mathematically elegant. You create a vector from the sphere's center to the test point,Unitize (normalize) it to length 1.0, and then scale it by the sphere's radius.
python
1def ClosestPoint(sphere: Sphere, test_pt: Point3d) -> Point3d:
2 """Projects a point onto the sphere boundary."""
3 # 1. Get direction from center
4 dir = test_pt - sphere.Center
5 # 2. Force length to match Radius
6 dir.Unitize()
7 return sphere.Center + (dir * sphere.Radius)
Search Pt X
4.00
Search Pt Y
4.00
Radius
2.00
2 min read1 page

DistanceTo(point):

Calculates the shortest distance from an external point to the surface of the sphere.
python
1def DistanceTo(self, pt: Point3d) -> float:
2 """Calculates the shortest distance from a point to the surface of the sphere."""
3 # Calculate distance from the test point to the center of the sphere
4 dist_to_center = self.Center.DistanceTo(pt)
5
6 # Subtract the radius. If the point is inside, this will be negative,
7 # so we return max(0, distance) if we only want exterior distance.
8 return max(0.0, dist_to_center - self.Radius)
This is highly efficient because we only need to measure the distance to the center, and then subtract the sphere's radius.
Point X
3.00
Point Y
2.00
Radius
2.00
2 min read1 page

Plane Intersection:

When an infinite plane slices through a sphere, the intersection is always a circle (or a single point if it's perfectly tangent). We can find the center of this circle by projecting the sphere's center onto the plane, and compute its radius using the Pythagorean theorem.
python
1def PlaneSphereIntersection(sphere: 'Sphere', plane: 'Plane') -> tuple[bool, 'Circle']:
2 """Finds the circle of intersection between a sphere and a plane."""
3 # Distance from sphere center to plane
4 # Assuming plane is defined by an Origin and Normal
5 vector_to_center = sphere.Center - plane.Origin
6 dist = Vector3d.DotProduct(vector_to_center, plane.Normal)
7
8 if abs(dist) > sphere.Radius:
9 return False, None # No intersection
10
11 # Radius of the resulting intersection circle
12 circle_radius = math.sqrt(sphere.Radius**2 - dist**2)
13
14 # Center of the intersection circle
15 circle_center = sphere.Center - (plane.Normal * dist)
16
17 return True, Circle(Plane(circle_center, plane.Normal), circle_radius)
Move Plane
0.50
2 min read1 page

Box Intersection:

To check if a sphere intersects a bounding box, we first find the closest point on the box to the sphere's center. If the distance from the sphere's center to this closest point is less than or equal to the sphere's radius, they intersect.
python
1def SphereIntersectsBox(sphere: 'Sphere', box_min: Point3d, box_max: Point3d) -> bool:
2 """Checks if a sphere intersects an AABB by finding the closest point on the box to the sphere."""
3 # Find closest point on box to sphere center
4 closest_x = max(box_min.X, min(sphere.Center.X, box_max.X))
5 closest_y = max(box_min.Y, min(sphere.Center.Y, box_max.Y))
6 closest_z = max(box_min.Z, min(sphere.Center.Z, box_max.Z))
7
8 # Check if distance to closest point is within radius
9 closest_point = Point3d(closest_x, closest_y, closest_z)
10 distance = sphere.Center.DistanceTo(closest_point)
11
12 return distance <= sphere.Radius
Move Sphere X
0.00
Move Sphere Y
2.50
1 min read1 page

Bounding Box Extraction:

Computing the AABB of a sphere is computationally trivial. Since a sphere's surface is equidistant from its center in all directions, its bounding box is just a cube formed by subtracting and adding the radius to the center coordinates.
python
1def SphereBoundingBox(sphere: 'Sphere') -> 'BoundingBox':
2 """Computes the Axis-Aligned Bounding Box (AABB) of a sphere."""
3 # A sphere's AABB is perfectly symmetric around its center
4 min_pt = Point3d(sphere.Center.X - sphere.Radius,
5 sphere.Center.Y - sphere.Radius,
6 sphere.Center.Z - sphere.Radius)
7
8 max_pt = Point3d(sphere.Center.X + sphere.Radius,
9 sphere.Center.Y + sphere.Radius,
10 sphere.Center.Z + sphere.Radius)
11
12 return BoundingBox(min_pt, max_pt)
Change Radius
1.50
2 min read1 page

BoundingSphere(box):

Creates the smallest possible sphere that completely encloses a given bounding box.
python
1def BoundingSphere(bbox: BoundingBox) -> Sphere:
2 """Creates a sphere that exactly bounds an Axis-Aligned Bounding Box."""
3 # The center of the sphere is the center of the bounding box
4 center = bbox.Center
5
6 # The radius is the distance from the center to any of the box's corners
7 radius = center.DistanceTo(bbox.Max)
8
9 return Sphere(center, radius)
Spheres are often used as early-rejection bounding volumes because checking intersection against a sphere is computationally cheaper than checking against a box.
Box Width
2.00
Box Depth
3.00
Box Height
1.50
3 min read1 page

FitSphere(points):

Constructs a sphere that encompasses a collection of points.
python
1def FitSphere(pts: list[Point3d]) -> Sphere:
2 """Calculates a best-fit sphere for a collection of points."""
3 # A simple bounding approach is taking the bounding box,
4 # finding its center, and setting the radius to the farthest point.
5
6 # 1. Find bounding box center
7 min_pt, max_pt = GetBoundingBox(pts)
8 center = Point3d(
9 (min_pt.X + max_pt.X) / 2,
10 (min_pt.Y + max_pt.Y) / 2,
11 (min_pt.Z + max_pt.Z) / 2
12 )
13
14 # 2. Find the farthest point from the center
15 max_dist = 0.0
16 for pt in pts:
17 dist = center.DistanceTo(pt)
18 if dist > max_dist:
19 max_dist = dist
20
21 return Sphere(center, max_dist)
There are multiple ways to fit a sphere. A fast approximation is finding the bounding box center and setting the radius to encompass the farthest point. A more mathematically rigorous approach solves the linear system for exactly 4 points, or uses Welzl's algorithm for the absolute minimum bounding sphere.
Spread
2.00