Primitives

struct P3 {
    long long x, y, z;
    P3 operator+(const P3& o) const { return {x+o.x, y+o.y, z+o.z}; }
    P3 operator-(const P3& o) const { return {x-o.x, y-o.y, z-o.z}; }
};
 
long long dot(P3 a, P3 b) { return a.x*b.x + a.y*b.y + a.z*b.z; }
 
P3 cross(P3 a, P3 b) {
    return { a.y*b.z - a.z*b.y,
             a.z*b.x - a.x*b.z,
             a.x*b.y - a.y*b.x };
}
 
long long triple(P3 a, P3 b, P3 c) { return dot(a, cross(b, c)); }
long long norm2(P3 a) { return dot(a, a); }

The dot product is unchanged from 2D. The cross product becomes a vector: perpendicular to both inputs, with length equal to the parallelogram area, direction by the right-hand rule.

The scalar triple product

This is the 3D orientation test:

ValueMeaning
is above the plane (right-hand rule)
the four points are coplanar
below

And is 6× the tetrahedron volume, or the volume of the parallelepiped.

int orient3(P3 a, P3 b, P3 c, P3 d) {
    long long v = triple(b - a, c - a, d - a);
    return (v > 0) - (v < 0);
}

Overflow warning: this is a degree-3 expression. With coordinates up to it reaches — already past long long. Use __int128 unless the coordinates are small.

Planes

A plane is with normal . From three points: , .

TaskFormula
Signed distance from
Which side is on?sign of (exact in integers)
Projection of onto the plane
Reflection across the plane
Line-plane intersectionsolve for the parameter ; parallel iff
Plane-plane intersectiona line with direction
Angle between planesthe angle between the normals

Distances

PairFormula
Point to line
Point to plane
Line to line (skew)
Point to segment / triangleproject and clamp
Point to sphere

The skew-line formula is worth remembering: the common perpendicular direction is , and the distance is the projection of the connecting vector onto it. When the lines are parallel and the point-to-line formula applies.

Volumes

SolidFormula
Tetrahedron
Parallelepiped
Convex polyhedron (divergence theorem)
Polyhedron from triangles over outward-oriented triangles
Sphere
Intersection of two spheresa lens; two spherical caps

The triangle-sum volume formula is the 3D shoelace: sum the signed tetrahedron volumes from the origin to each face, and everything outside cancels.

3D convex hull

expected with randomised incremental construction plus a conflict graph; with the simple incremental algorithm. Output is faces (Euler: ).

Substantially harder than 2D — the degeneracies (coplanar points, collinear edges) are much nastier, and the in-sphere/orientation predicates overflow easily.

Payoff: the 3D hull of points lifted onto the paraboloid projects to the 2D Delaunay triangulation.

Rotations

RepresentationNote
Rotation matrix (3×3 orthogonal)composes by multiplication; 9 numbers
Euler anglesintuitive but suffers gimbal lock
Axis-angle (Rodrigues)
Quaternions4 numbers, no gimbal lock, composes by quaternion multiplication, interpolates smoothly

Quaternions are the standard in graphics and robotics. In competitive programming, rotations by fixed axis-aligned amounts (90° about an axis) are much more common, and those are simple coordinate permutations with sign flips — there are 24 of them for a cube.

Spherical geometry

TaskFormula
Great-circle distance, or the numerically better haversine formula
Spherical triangle area — the spherical excess
Convert lat/long to Cartesian

Use the haversine form for great-circle distance: acos of a dot product loses precision badly for nearby points.

See also: Vectors · Cross Product · Convex Hull