<p>Let's get our orientation straight first. When I speak of a direction, I mean the direction relative to our viewpoint. So the &quot;front&quot; face of a cuboid is the face nearest the camera; &quot;back&quot; means the face pointing away from the camera. The &quot;left&quot; face of a cuboid is the face pointing to the left from the camera's view.</p>
<p>A cuboid X is in front of cuboid Y if one of the following is true:</p>
<ul>
<li>The back face of X is in front of the front face of Y</li>
<li>The left face of X is to the right of the right face of Y.</li>
</ul>
<p>If either of these is true, then Y should be drawn before X. This is how you build your sort order. If one of the above is true, then Y should be &quot;less than&quot; X and thus come earlier in the sort.</p>
<p>Note that this algorithm assumes exact math is used to compute the faces of the cuboids for comparison purposes. If you're using floating-point math, you may encounter precision issues that cause problems.</p>

CC BY-SA 4.0
2022-02-10T17:39:10.660Z
2022-02-10T17:51:53.173Z
2022-02-10T17:51:53.173Z
Nicol Bolas
<p>I am working on a 3D packing algorithm (MIT-licensed). The box and the cuboids it contains are displayed as a 2D projection. The order of drawing the cuboids (now represented as polygons) is important: a cuboid placed at the back of the box must not be drawn after the cuboids in the front row. I am currently using order by(cuboid.X + cuboid.Y + cuboid.Z) and it works around 99% of the time, but not always. Some aberrations do ocurr, as depicted below. I have tried multiplying the coordinates, and order by X then by Y then by z, and other combinations. Adding the coordinates is the one that gave the best result. POV-ordering has been suggested, but it won't work, as illustrated by the second diagram.  Can someone enlighten me?
<a href="https://i.sstatic.net/aGyXA.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/aGyXA.png" alt="enter image description here" /></a>
<a href="https://i.sstatic.net/DoNuu.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/DoNuu.png" alt="enter image description here" /></a></p>

CC BY-SA 4.0
2022-02-10T17:29:37.693Z
I assume that using a depth buffer is off the table, right?
Nicol Bolas
<geometry><projections><2d-graphics>
When fitting 3d cuboids into a box, how to find the order of drawing (in a 2D projection) so that the cuboids don't appear to overlap?
developer1405
# When fitting 3d cuboids into a box, how to find the order of drawing (in a 2D projection) so that the cuboids don't appear to overlap?

(Asked by: username_0 on 2022-02-10)

I am working on a 3D packing algorithm (MIT-licensed). The box and the cuboids it contains are displayed as a 2D projection. The order of drawing the cuboids (now represented as polygons) is important: a cuboid placed at the back of the box must not be drawn after the cuboids in the front row. I am currently using order by(cuboid.X + cuboid.Y + cuboid.Z) and it works around 99% of the time, but not always. Some aberrations do ocurr, as depicted below. I have tried multiplying the coordinates, and order by X then by Y then by z, and other combinations. Adding the coordinates is the one that gave the best result. POV-ordering has been suggested, but it won't work, as illustrated by the second diagram. Can someone enlighten me?
[![enter image description here](https://i.sstatic.net/aGyXA.png)](https://i.sstatic.net/aGyXA.png)
[![enter image description here](https://i.sstatic.net/DoNuu.png)](https://i.sstatic.net/DoNuu.png)

**Tags:** geometry, projections, 2d-graphics

> **Comments:**
> **username_1**: I assume that using a depth buffer is off the table, right?  

---

## 1 answers

### Answer by username_1 on 2022-02-10. Score: 3 | Accepted answer

Let's get our orientation straight first. When I speak of a direction, I mean the direction relative to our viewpoint. So the "front" face of a cuboid is the face nearest the camera; "back" means the face pointing away from the camera. The "left" face of a cuboid is the face pointing to the left from the camera's view.

A cuboid X is in front of cuboid Y if one of the following is true:

* The back face of X is in front of the front face of Y
* The left face of X is to the right of the right face of Y.

If either of these is true, then Y should be drawn before X. This is how you build your sort order. If one of the above is true, then Y should be "less than" X and thus come earlier in the sort.

Note that this algorithm assumes exact math is used to compute the faces of the cuboids for comparison purposes. If you're using floating-point math, you may encounter precision issues that cause problems.


---

