How to compute the distance between two 3D objects in the space My question is about how to compute the distance between an object knowing its coordinates (x, y, z) and a table whose I know the coordinates of its center, its length and its width. Could you give me any suggestion, please?
 A: You can use vectors to help with the math here. For example, represent the position of the center as $\vec{c}=(c_x,c_y,c_z)$. We also need a normal to the table to represent orientation, $\hat{n}=(n_x,n_y,n_z)$. Note the hat on $n$. We want $\hat{n}$ to be a unit vector. 
Given the normal to a plane, in this case, the surface of the table, and some point on the the plane, given here as $\vec{c}$, any point on the plane $\vec{r}$ satisfies the relation :$(\vec{r}-\vec{c})\cdot \hat{n}=0$
Given a point in space $\vec{q}=(q_x,q_y,q_z)$, the minimal distance to the plane is: $l=(\vec{q}-\vec{c})\cdot \hat{n}$. 
No point on the table can be closer than this, but the plane containing the surface of the table has points that are not on the table. The point on the plane closest to $\vec{q}$ might be a non-table point. 
We can tell whether this is a non-table point by calculating $\vec{p}=(\vec{q}-\vec{c})-[(\vec{q}-\vec{c}))\cdot \hat{n}]\hat{n}$. Here $\vec{p}$ represents the shortest distance from the nearest point on the plane to $\vec{q}$ to the center $\vec{c}$. 
Now if $\sqrt{\vec{p}\cdot \vec{p}}$ is less than half the smallest of length and width, then we area done, the length is: $(\vec{q}-\vec{c})\cdot \vec{n}$.
If its greater than half the longest of the two, then  a straight line from the closest point on the plane to the center intersects the table at closest point. 
If it's between, you need a way to determine if you are "on the table" mathematically which you can do if you have a way to of representing a unit vector in the direction of length and width, call them $\hat{i}$ and $\hat{j}$.
Then your distance is $\sqrt{l^2+[(\vec{p}\cdot \vec{i})-L/2]^2+[(\vec{p}\cdot \vec{i})-W/2]^2}$
Where $L$ is zero unless $\vec{p}\cdot \vec{i}$ is greater than $L/2$ and $W$ is zero unless that quantity is greater than $W/2$. In the case of excess, they take on the value of length and width respectively. 
A: Let's assume your table is "horizontal"; that is, all the points on the table have the same $z$-coordinate.  Let's further assume that the table is aligned to the $x$-axis and $y$-axis.
Then your basic approach should be to divide the $x$-$y$ plane into nine sections, which I will name according to the eight compass directions:


*

*Inside the table

*North, south, east, or west of the table

*Northeast, southeast, northwest, or southwest of the table


What we do then is to project the robot's location into the plane of the table.  If this projection is inside the table, then the nearest point is this projection.  If the projection is north, south, east, or west of the table, then the nearest point is on the edge of the table.  Finally, if the projection is northeast, southeast, northwest, or southwest of the table, the nearest point is at the corner of the table.
To find the distance from the robot to the nearest point, suppose the two points are $(x_r, y_r, z_r)$ for the robot, and $(x_n, y_n, z_n)$ for the nearest point, then the distance is just
$$
d = \sqrt{(x_r-x_n)^2+(y_r-y_n)^2+(z_r-z_n)^2}
$$

A short example, to make this more concrete: Suppose that the table has center $(3, 2, 7)$ and has length in the $x$ direction of $8$, and width in the $y$ direction of $6$.  Then the corners of the table are at $(-1, -1, 7), (-1, 5, 7), (7, 5, 7), (7, -1, 7)$.

Three examples:


*

*If the robot is at $(10, 4, 2)$, we project its location up to $(10, 4, 7)$.  This point is "east" of the table, so the nearest point is on the eastern edge of the table, at $(7, 4, 7)$.  The distance is $\sqrt{(10-7)^2+(4-4)^2+(2-7)^2} = \sqrt{9+25} = \sqrt{34}$.

*If the robot is at $(6, 3, 9)$, we project its location down to $(6, 3, 7)$.  This point is "inside" the table, so the nearest point has these projected coordinates, $(6, 3, 7)$.  The distance is $\sqrt{(6-6)^2+(3-3)^2+(9-7)^2} = \sqrt{4} = 2$.

*If the robot is at $(-2, 8, 8)$, we project its location down to $(-2, 8, 7)$.  This point is "northwest" of the table, so the nearest point is the northwest corner of the table, at $(-1, 5, 7)$.  The distance is $\sqrt{[-2-(-1)]^2+(8-5)^2+(8-7)^2} = \sqrt{1+9+1} = \sqrt{11}$.
