Depth sorting rectangular polygons, all parts of model axes facing
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
If all of your rectangular polygons face the camera and each rectangle lies on a single depth plane, depth sorting is much simpler than the general polygon-ordering problem. In that restricted case, a single depth key per rectangle is often enough; once rectangles span conflicting depths or intersect visually, a Z-buffer becomes the safer and more general solution.
Why the Restricted Case Is Easier
General depth sorting is hard because two polygons can overlap on screen while crossing each other in 3D. Then neither polygon is globally "in front" of the other.
Your case is easier when all of these are true:
- each polygon is rectangular
- all polygons face the viewer
- each rectangle can be represented by one constant or near-constant depth value
- there is no cyclic overlap relationship
Under those assumptions, painter-style ordering works well:
- transform rectangles into camera space
- compute one depth value per rectangle
- sort from farthest to nearest
- draw in that order
This is valid because each rectangle behaves like one front-facing layer rather than a shape with complicated depth variation.
Compute the Depth in Camera Space
The safest place to sort is camera space, not model space. A rectangle's z value in world or model coordinates only matters after the camera transform is applied.
A simple Python example:
If the geometry guarantee really holds, this ordering is enough.
In a real renderer, z_camera might come from the transformed center of the rectangle or from the rectangle's plane distance in view space.
Which Depth Key Should You Use
When rectangles are truly parallel to the view plane, several keys are equivalent in practice:
- center depth
- average vertex depth
- any vertex depth, if all vertices share the same depth
A slightly more explicit example uses four vertices:
If every vertex has the same camera-space depth, then polygon sorting is stable and straightforward.
When the Trick Stops Working
A single depth value per polygon fails when any of these happen:
- one rectangle partially passes behind another
- rectangles intersect in 3D
- perspective makes one polygon cover a meaningful depth range
- two polygons form a cyclic order in screen overlap
At that point, painter's algorithm becomes unreliable because there is no single correct whole-polygon order.
That is why modern graphics pipelines use a depth buffer. A depth buffer resolves visibility per pixel rather than assuming one polygon order works everywhere.
A Tiny Z-Buffer Example
You do not need a full renderer to see the idea. A depth buffer stores the nearest depth seen so far for each pixel.
The nearer pixel wins even if whole-polygon ordering would have been ambiguous. That is the core reason Z-buffering is robust.
A Practical Rule of Thumb
Use polygon-level depth sorting when your scene guarantees make it valid. Use a depth buffer when the scene is even slightly more general than that.
For front-facing UI quads, sprites, or billboard-like rectangles, sorting by camera-space depth is often all you need. For arbitrary 3D model surfaces, it is not.
This matters because many rendering bugs come from applying a simple sort to geometry that no longer satisfies the assumptions behind the sort.
Common Pitfalls
The most common mistake is sorting by model-space depth instead of camera-space depth. The camera transform can change which object is in front.
Another issue is using centroid or average depth for geometry that spans a meaningful depth range. A single average value cannot represent per-pixel visibility correctly.
People also assume that because all polygons are rectangles, sorting must be easy. Rectangle shape is not the key issue; the important question is whether each rectangle has one valid global depth order relative to the others.
Finally, nearly equal depth values can cause unstable ordering or Z-fighting. If the geometry is intentionally layered, a small separation or explicit rendering order may still be necessary.
Summary
- Depth sorting is simple only when each rectangle can be represented by one valid depth value.
- Compute that depth in camera space, not model space.
- Painter-style ordering works for front-facing layered rectangles with no conflicting overlap.
- The method fails when rectangles intersect or span inconsistent depths.
- A Z-buffer is the robust solution once visibility becomes a per-pixel problem.
Related reading
- Design a Hashtable
- design a stack such that getMinimum should be O1
- Design an efficient algorithm to sort 5 distinct keys in fewer than 8 comparisons
- Design patterns for converting recursive algorithms to iterative ones
- Designing function f(f(n)) == -n
- Detecting self crossing in closed Bezier curves
- Designing this algorithm a better way?
- Detect differences between two strings

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.