enter image description here

I have created this set of spheres in Mathematica.

The code I used to produced this is as follows:

satnum=Input["Select Number of Satellites"]
alt=Input["Select an orbit altitude (km)"]
sensorstr=Input["Select sensor strength (km)"]

Basically I want to draw a circle of a given radius r around the transparent sphere in the middle and count how many solid spheres contain a point at a given angle (once a starting point is defined). In this case the # of containing spheres would always be 1 or 2 as long as the circle has a small enough radius. I've looked into functions such as RegionIntersection or Surface Intersection, but I can't seem to figure out exactly how I should approach this problem.

Thanks for any help.

  • $\begingroup$ If you can give examples of your code this could help others? $\endgroup$
    – Dunlop
    Jun 14, 2017 at 20:11
  • $\begingroup$ at least partly answered here: mathematica.stackexchange.com/questions/79524/… $\endgroup$
    – george2079
    Jun 14, 2017 at 20:22
  • $\begingroup$ Look at RegionMember. You should simply check if a given point is a member of each of the spheres. $\endgroup$
    – yohbs
    Jun 14, 2017 at 20:23
  • $\begingroup$ Looks like I could put together a solution using RegionMember, thanks @yohbs $\endgroup$
    – Evan
    Jun 14, 2017 at 20:38

2 Answers 2


So the core of the problem is to determine of a point intersects a region. A Sphere is hollow, a Ball is solid, so you want Ball. Example here...

bunchofballs = Table[Ball[{x, 0, 0}], {x, 0, 4}]


enter image description here

Now count intersections for a point p={1,0,0}

Count[Map[RegionMember[#, p] &, bunchofballs], True]

(*  3  *)

MikeY's solution is the right thing to do in the general case. In the case of spheres you can gain a lot of computation time by checking directly if the distance to the sphere center is smaller than the radius. Here's a comparison for 10000 spheres:

(*set up the spheres:*)
n = 10000;
radii = RandomReal[{0.5, 1.5}, n];
centers = RandomReal[{0, 10}, {n, 3}];
bunchofballs = Table[Ball[centers[[i]], radii[[i]]], {i, n}];
(*see how many spheres contain p0:*)
p0 = RandomReal[{0, 10}, 3];
AbsoluteTiming@Count[Map[RegionMember[#, p0] &, bunchofballs], True]
AbsoluteTiming@ Count[Table[Norm[p0 - centers[[i]]] < radii[[i]], {i, n}],True]
{4.75374, 28}
{0.038338, 28}

That's a 100 times faster.

  • 1
    $\begingroup$ + faster still UnitStep[radii - (Norm[# - p0] & /@ centers)] // Total $\endgroup$
    – george2079
    Jun 15, 2017 at 14:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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