PBC Cheat Sheet This cheat sheet is based on the article A Practical Guide to Periodic Boundary Conditions.
This cheat sheet works for vectors with any number of dimensions. It's sufficient to treat each dimension individually. Each dimension can have different boundaries. A boundary is a periodic interval $[a, b)$ with an inclusive lower bound and an exclusive upper bound.

Wrap Positions

Ensures that positions stay inside the periodic boundaries.

On Boundaries $[0, 1)$:

$$x = x - \text{floor}(x)$$

On Generic Boundaries $[a, b)$:

$$x = x - \text{floor}\left(\frac{x - a}{b - a}\right) \cdot (b - a)$$

Shortest Connection

Determine the shortest connection of two positions in your periodic space. The connection can wrap around boundaries. First, compute the connection in the usual way:

$$dx = x_2 - x_1$$

Then, wrap the connection:

On Boundaries $[0, 1)$:

$$dx = dx - \text{round}(dx)$$

On Generic Boundaries $[a, b)$:

$$dx = dx - \text{round}\left(\frac{dx}{b - a}\right) \cdot (b - a)$$