Painter's algorithm for z-ordering