1
\$\begingroup\$

How can I compute the eight corners of a cuboid given a Depth and Height and two points: A(x,y,z) and B(x,y,z)?

Figure 1. Cuboid

Is it possible to calculate the eight corners/points of a cuboid given only a limited data? I'm trying to make a cuboid with only two 3D vectors by computing the eight vectors.

\$\endgroup\$
  • \$\begingroup\$ Are the two points always in the centers of the end faces of the cuboid, and does the height axis always point in some known global "up" direction? If not, then the problem is under-specified (cuboids shifted perpendicular to or rotated about the A-B axis aren't distinguishable). \$\endgroup\$ – DMGregory Jul 31 '17 at 3:13
2
\$\begingroup\$

This would do the trick for the 4 points around A

  • Corner1 = (A.x + Depth/2, A.y + Height / 2, A.z)
  • Corner2 = (A.x - Depth/2, A.y + Height / 2, A.z)
  • Corner3 = (A.x + Depth/2, A.y - Height / 2, A.z)
  • Corner4 = (A.x - Depth/2, A.y - Height / 2, A.z)

And for the 4 points around B

  • Corner5 = (B.x + Depth/2, B.y + Height / 2, B.z)
  • Corner6 = (B.x - Depth/2, B.y + Height / 2, B.z)
  • Corner7 = (B.x + Depth/2, B.y - Height / 2, B.z)
  • Corner8 = (B.x - Depth/2, B.y - Height / 2, B.z)

EDIT : As pointed out by @DMGregory, my solution only works with the cuboid being parallel to the Z axis.

enter image description here

\$\endgroup\$
  • \$\begingroup\$ Here you're assuming that the line AB is constrained to be parallel to the z axis, so this won't work if A and B are chosen to be, for instance, separated along the x axis, or lying diagonal to the axes. \$\endgroup\$ – DMGregory Jul 31 '17 at 13:03
  • \$\begingroup\$ Indeed, I shall specify this in my answer. \$\endgroup\$ – Shashimee Jul 31 '17 at 13:12
2
\$\begingroup\$

I'll assume you have a Vector3 and a Matrix3 class. First of all, let's define a unit cube:

(-0.5, -0.5, 0)
(0.5, -0.5, 0)
(0.5, 0.5, 0)
(-0.5, 0.5, 0)

(-0.5, -0.5, 1)
(0.5, -0.5, 1)
(0.5, 0.5, 1)
(-0.5, 0.5, 1)

Now, to create the rectangle you need, you need to scale the vectors based on the height, depth and the distance between A and B. To do this, you need a scale matrix:

    | Depth 0      0       |
S = | 0     Height 0       |
    | 0     0      |A - B| |

Where |A - B| is the length of the A - B vector

Now the cuboid has the correct size. The next step is to rotate it.

Because the A and B vectors only define the rotation around the X (pitch) and Y (yaw) axis, you only need to calculate those:

Let C be A - B for convenience.

The yaw can be calculated by throwing away the y axis, which projects the vector to the XY plane. Then the angle becomes

yaw = atan(C.y / C.x)

The pitch is a little bit different. You need the length and the z component of the vector:

pitch = arccos(C.z / |C|)

Now that you have the angles, you need to construct a rotation matrix:

    | 1 0          0           |   | cos(yaw)  0 sin(yaw) |
R = | 0 cos(pitch) -sin(pitch) | * | 0         1 0        |
    | 0 sin(pitch) cos(pitch)  |   | -sin(yaw) 0 cos(yaw) |

To get the angles of the cuboid you need to take every vector of the cuboid, multiply it by the matrices and add A to it:

newPoint = R * S * point + A
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.