Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
Depth sorting, or z-sorting, is something we've already discussed in Chapter 15, when you were just applying perspective to sprites. In that case, you sorted the array of sprites (actually subclasses of Sprite that had 3D properties) by their zpos property.
At this point however, you are not dealing with multiple sprites. All of the polygons are being drawn within the same graphics object of the main document class. Thus, whenever a particular polygon is drawn, it will be drawn on top of any that have been drawn earlier. So, rather than swapping anything's depth, you just need to determine which polygon gets drawn when. Specifically, you want to draw the ones that are farthest away first; then you draw the rest, working your way forward, so that the closest polygons are drawn last, covering anything they might be in front of.