OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Get distance from a point to the nearest box

  • Thread starter Thread starter MirceaKitsune
  • Start date Start date
M

MirceaKitsune

Guest
I have a 3D space where positions are stored as tuples, eg: (2, 0.5, -4). If I want to know the distance between two points I just do dist = (abs(x1 -x2), abs(y1 - y2), abs(z1 - z2)) and if I want a radius distf = (dist[0] + dist[1] + dist[2]) / 3. Now I have boxes each defined by two min / max positions (eg: (-4 8 -16) to (4, 12, 6)) and I want to know the distance between my point to the closest one: What is the simplest way to know the distance to the closest face in all 3 directions, or 0 in case the position is inside a box? Just looking for the lightest solution that doesn't require numpy or libraries other than defaults like math since I'm not using those in my project.

This is my messy solution which should probably work but I'd like to know if there's anything better.

Code:
point = (8, 12, 16)
box_min = (-4, -4, -4)
box_max = (4, 4, 4)

box_center = ((box_min[0] + box_max[0]) / 2, (box_min[1] + box_max[1]) / 2, (box_min[2] + box_max[2]) / 2)
box_scale = (abs(box_max[0] - box_min[0]), abs(box_max[1] - box_min[1]), abs(box_max[2] - box_min[2]))
dist = (abs(box_center[0] - point[0]) + box_scale[0] / 2, abs(box_center[1] - point[1]) + box_scale[1] / 2, abs(box_center[2] - point[2]) + box_scale[2] / 2)
<p>I have a 3D space where positions are stored as tuples, eg: <code>(2, 0.5, -4)</code>. If I want to know the distance between two points I just do <code>dist = (abs(x1 -x2), abs(y1 - y2), abs(z1 - z2))</code> and if I want a radius <code>distf = (dist[0] + dist[1] + dist[2]) / 3</code>. Now I have boxes each defined by two min / max positions (eg: <code>(-4 8 -16)</code> to <code>(4, 12, 6)</code>) and I want to know the distance between my point to the closest one: What is the simplest way to know the distance to the closest face in all 3 directions, or 0 in case the position is inside a box? Just looking for the lightest solution that doesn't require numpy or libraries other than defaults like <code>math</code> since I'm not using those in my project.</p>
<p>This is my messy solution which should probably work but I'd like to know if there's anything better.</p>
<pre><code>point = (8, 12, 16)
box_min = (-4, -4, -4)
box_max = (4, 4, 4)

box_center = ((box_min[0] + box_max[0]) / 2, (box_min[1] + box_max[1]) / 2, (box_min[2] + box_max[2]) / 2)
box_scale = (abs(box_max[0] - box_min[0]), abs(box_max[1] - box_min[1]), abs(box_max[2] - box_min[2]))
dist = (abs(box_center[0] - point[0]) + box_scale[0] / 2, abs(box_center[1] - point[1]) + box_scale[1] / 2, abs(box_center[2] - point[2]) + box_scale[2] / 2)
</code></pre>
 

Latest posts

Top