How can I calculate the minimum distance between a point on the perimeter of a disk in 3d space and a point above the disk?
For example, there is a disk in 3d space with center [0,0,0]. It has radius 3 and lies flat on the x,y plane. If there is a particle above the disk at [5,5,5], how do I calculate the minimum distance from this particle to a point on the perimeter of the disk?
Here is my attempt so far:
vec1 = vector between disk center and particle
vec1 = [(5 - 0), (5 - 0), (5 - 0)]
vec1 = [5,5,5]
unitvec1 = unit vector in direction of vec1
unitvec1 = vec1/norm(vec1)
unitvec1 = [0.5774, 0.5774, 0.5774]
vec2 = vector between disk center and point on the perimeter closest to the particle
vec2 = disk radius * unitvec1, and make z element = 0
vec2 = 3 * [0.5774, 0.5774, 0]
vec2 = [1.7321, 1.7321, 0]
vec3 = vector between particle and point on the perimeter closest to the particle
vec3 = vec1 - vec2
vec3 = [3.2679, 3.2679, 5.0000]
So the min distance is
norm(vec3) = 6.8087
But this method doesn't always work. If I try it with disk center [0,0,0], particle location [0,0,6], and disk radius 9, it gives the minimum distance to be 6. this can't be correct, because the distance between the center of the disk and the particle will be 6, so the distance to the perimeter must be larger.
What am I doing wrong, and how should I actually calculate this?
Thanks!
note: I am using pseudo code, not an actual programing language
Best Answer
Your code is almost completely correct (and avoids the use of atan2, which is nice). The one problem you have is that when the vector from the disk-center to your point happens to point perpendicular to the disk, i.e., when the test point happens to be exactly above the disk's center, it falls apart: projecting that vector to the $xy$-plane gives the zero vector, whose tip does not lie on the disk's boundary circle.
Here's replacement code (mostly yours):
That'll handle the special case on which your current code is failing. Note that you might want to replace the test "norm (pvec1) = 0" with "norm (pvec1) < .001" to make the code a bit more numerically stable, and introduce almost no error.