-2

I am developing a code in python to check whether a sphere having a center at (x, y, z) coordinates and with radius R, intersects the cube of dimension one, i.e., l = 1, b = 1, h = 1. As mentioned above, I want to know if the sphere intersects the cube at any point or direction, or proportion.

I have a list of sphere coordinates (x,y,z) that must be checked for the intersection. I had done some research on it but couldn't clear my doubts regarding how to approach this example.

I would love to know both the math and coding part of it. Can someone please help me solve it..?? Edit: Cube is axis aligned and placed at the origin.

  • Welcome to Stack Overflow. Please include the code that you're using to attempt to answer this question. I believe it would simplify if you 'assumed' that the sphere is at the origin. Then once you have a basic understanding, allow the sphere to be anywhere. – ewokx May 11 '22 at 04:19
  • 1
    Is cube axis-aligned? How do you define its coordinates? – MBo May 11 '22 at 04:54
  • Yes cube is axis aligned – John Styles May 11 '22 at 05:23
  • Where is the cube?? You have told us that its side-length is 1, and that it is axis-aligned. But it still could be anywhere. Note how for the sphere you mentioned the coordinates of a center, but for the cube you didn't mention any coordinates. – Stef May 11 '22 at 08:30
  • I have edited in above: it is placed at origin (0,0,0) – John Styles May 12 '22 at 10:12

1 Answers1

1

To reveal a fact of intersection, you can calculate distance from cube to sphere center and compare it with sphere radius. 2D case is described here, it could be easily extended to 3D case.

Get cube center as (rcx, rcy, rcz) and find coordinate differences from cube center to sphere center

dx, dy, dz = x - rcx, y - rcy, z - rcz

Let SquaredDist = 0, and for every coordinate make:

t = dx + 0.5                # 0.5 is half-size of your cube
if t < 0:
  SquaredDist += t * t
else:
   t = dx - 0.5
   if t > 0:
  SquaredDist += t * t

finally compare SquaredDist with R*R

Some explanation to comment:

Look at the picture in linked answer. For rectangle ABCD we have center G and coordinate differences GK and GJ, they include half of width and half of height. Squared distance (EC here) is sum of squared distances to proper side lines (planes in 3D case). When the closest (to sphere center) point is cube corner, we take into account three planes, when closest point lies at the edge - we take into account two planes, when closest point lies at facet - we take into account only one plane, and when sphere center is inside - SquaredDist remains zero.

MBo
  • 77,366
  • 5
  • 53
  • 86
  • Thanks for the reply MBo.. what does SquaredDist why it is equal to zero represent in this code and why are we adding half of the cube length to the coordinate difference...?? – John Styles May 11 '22 at 08:39
  • Too long for comment, added to answer – MBo May 11 '22 at 09:06
  • do closest point means the point traced by the sphere center point on the cube surface..?? – John Styles May 11 '22 at 11:02
  • No, just shortest distance to any cube point. I think you mean traced point like projection of sphere center onto cube planes - this is only for the third case (closest point lies at facet) – MBo May 11 '22 at 11:05
  • Thanks MBo for the answer, but there are two more things I am not understanding is , 1. "sum of squared distances to proper side lines (planes in 3D case)" how to do it and 2. "finally compare SquaredDist with R*R" how will comparison take place. – John Styles May 12 '22 at 10:42
  • It would be great if I could clear the above doubts...It will help me a lot. – John Styles May 12 '22 at 10:45