# Sphere-Sphere Intersect

**URL:** https://discourse.processing.org/t/sphere-sphere-intersect/35364
**Category:** Coding Questions
**Created:** [February 21, 2022, 6:33pm UTC](https://discourse.processing.org/t/sphere-sphere-intersect/35364 "2022-02-21T18:33:07Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Divitiacus](https://avatars.discourse-cdn.com/v4/letter/d/848f3c/32.png) [@Divitiacus](https://discourse.processing.org/u/Divitiacus)
#### Post date: [February 21, 2022, 6:33pm UTC](https://discourse.processing.org/t/sphere-sphere-intersect/35364/1 "2022-02-21T18:33:07Z")

</div>

When two spheres overlap in 3D, they create a circle, where they intersect. I would like to be able to create a (random) point on this circle.

Here is what I have so far:  
Axis **a** through the center of the circle (long blue line)  
Center of circle (where both blue lines meet)  
Radius of the circle (based on 2D projection of the problem)

As a next step I would need to calculate a vector **p** , which, either from the circle center or from the origin points to the point on the circle.

I know that, if the circle is in the xy-plane, I could use **p** = **a** /2 +r **i** cos(t) + r **j** sin(t) with **i** and **j** the unit vectors for x and y, however because the circle is not in the xy-plane I would need to create two unit vectors in the circle plane perpendicular to the axis **a** but I don’t know how? Any suggestions?

 ![spheres](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/6/650db90ee611f72e5272c4753c163635a94c8521.png)

---

<div class="post-metadata">

### Author: ![scudly](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/scudly/32/5597_2.png) [@scudly](https://discourse.processing.org/u/scudly)
#### Post date: [February 22, 2022, 1:17am UTC](https://discourse.processing.org/t/sphere-sphere-intersect/35364/2 "2022-02-22T01:17:38Z")

</div>

if **a** is your vector between the sphere centers, then if you cross product it with any other vector **b** not parallel to **a** , you will get a new vector, call it **i** , that is perpendicular to **a**. Cross **a** with **i** to get **j**. Then normalize **i** and **j**.

To come up with **b** , pick the shortest component of **a** , that is, the component whose abs() is closest to 0, set that component to 1 and the other two to 0. So, if **a** was ( 7, -1, -8 ), you would choose **b** as ( 0, 1, 0 ).

---

<div class="post-metadata">

### Author: ![Divitiacus](https://avatars.discourse-cdn.com/v4/letter/d/848f3c/32.png) [@Divitiacus](https://discourse.processing.org/u/Divitiacus)
#### Post date: [February 22, 2022, 2:54am UTC](https://discourse.processing.org/t/sphere-sphere-intersect/35364/3 "2022-02-22T02:54:34Z")

</div>

Beautiful. That works.

---

<div class="post-metadata">

### Author: ![Divitiacus](https://avatars.discourse-cdn.com/v4/letter/d/848f3c/32.png) [@Divitiacus](https://discourse.processing.org/u/Divitiacus)
#### Post date: [February 22, 2022, 5:41pm UTC](https://discourse.processing.org/t/sphere-sphere-intersect/35364/4 "2022-02-22T17:41:31Z")

</div>

Just a little correction:

In the circle equation I wrote on top I use **a** /2 to point to the middle point.  
If I do that, then all coordinates calculated have the origin of **a** as origin. That means to show them correctly I have to add the red vector (where the blue axis vector starts).

If I use the vector pointing from the coordinate system origin to the circle middle point instead of **a** /2, then the coordinates need no correction.

 ![spheres2](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/8/8560c7d02a9677aae4fd6d225cf30a4ff9cd2429.png)
