We present an optimization approach that can be employed to calculate the globally optimal segmentation of a 2-D magnetic system into uniformly magnetized pieces. For each segment, the algorithm calculates the optimal shape and the optimal direction of the remanent flux density vector, with respect to a linear objective functional. We illustrate the approach with results for magnet design problems from different areas, such as a permanent magnet electric motor, a beam-focusing quadrupole magnet for particle accelerators, and a rotary device for magnetic refrigeration.