How many points can two circles intersect?

Here's an implementation in Javascript using vectors. The code is well documented, you should be able to follow it. Here's the original source

See live demo here:

How many points can two circles intersect?

// Let EPS (epsilon) be a small value var EPS = 0.0000001; // Let a point be a pair: (x, y) function Point(x, y) { this.x = x; this.y = y; } // Define a circle centered at (x,y) with radius r function Circle(x,y,r) { this.x = x; this.y = y; this.r = r; } // Due to double rounding precision the value passed into the Math.acos // function may be outside its domain of [-1, +1] which would return // the value NaN which we do not want. function acossafe(x) { if (x >= +1.0) return 0; if (x <= -1.0) return Math.PI; return Math.acos(x); } // Rotates a point about a fixed point at some angle 'a' function rotatePoint(fp, pt, a) { var x = pt.x - fp.x; var y = pt.y - fp.y; var xRot = x * Math.cos(a) + y * Math.sin(a); var yRot = y * Math.cos(a) - x * Math.sin(a); return new Point(fp.x+xRot,fp.y+yRot); } // Given two circles this method finds the intersection // point(s) of the two circles (if any exists) function circleCircleIntersectionPoints(c1, c2) { var r, R, d, dx, dy, cx, cy, Cx, Cy; if (c1.r < c2.r) { r = c1.r; R = c2.r; cx = c1.x; cy = c1.y; Cx = c2.x; Cy = c2.y; } else { r = c2.r; R = c1.r; Cx = c1.x; Cy = c1.y; cx = c2.x; cy = c2.y; } // Compute the vector <dx, dy> dx = cx - Cx; dy = cy - Cy; // Find the distance between two points. d = Math.sqrt( dx*dx + dy*dy ); // There are an infinite number of solutions // Seems appropriate to also return null if (d < EPS && Math.abs(R-r) < EPS) return []; // No intersection (circles centered at the // same place with different size) else if (d < EPS) return []; var x = (dx / d) * R + Cx; var y = (dy / d) * R + Cy; var P = new Point(x, y); // Single intersection (kissing circles) if (Math.abs((R+r)-d) < EPS || Math.abs(R-(r+d)) < EPS) return [P]; // No intersection. Either the small circle contained within // big circle or circles are simply disjoint. if ( (d+r) < R || (R+r < d) ) return []; var C = new Point(Cx, Cy); var angle = acossafe((r*r-d*d-R*R)/(-2.0*d*R)); var pt1 = rotatePoint(C, P, +angle); var pt2 = rotatePoint(C, P, -angle); return [pt1, pt2]; }

Given a number n, we need to find the maximum number of times n circles intersect.

Input :  n = 2
Output : 2

Input :  n = 3
Output : 6

As we can see in above diagram, for each pair of circles, there can be maximum two intersection points. Therefore if we have n circles then there can be nC2 pairs of circles in which each pair will have two intersections. So by this, we can conclude that by looking at all possible pairs of circles the mathematical formula can be made for the maximum number of intersections by n circles is given by 2 * nC2
2 * nC2 = 2 * n * (n – 1)/2 = n * (n-1)

#include <bits/stdc++.h>

using namespace std;

int intersection(int n)

{

   return n * (n - 1);

}

int main()

{

    cout << intersection(3) << endl;

    return 0;

}

import java.io.*;

public class GFG {

    static int intersection(int n)

    {

       return n * (n - 1);

    }

    public static void main(String[] args) throws IOException

    {

        System.out.println(intersection(3));

    }

}

def intersection(n):

   return n * (n - 1);

print(intersection(3))

using System;

class GFG {

    static int intersection(int n)

    {

        return n * (n - 1);

    }

public static void Main()

{

    Console.WriteLine(intersection(3));

}

}

<?php

function intersection($n)

{

   return $n * ($n - 1);

}

echo intersection(3);

?>

<script>

function intersection(n)

{

   return n * (n - 1);

}

document.write(intersection(3));

</script>

Time Complexity: O(1)
Auxiliary Space: O(1)

Easy solution is to consider another plane such that the centers are along an axis.

Given the points $(x_1,y_1)$ and $(x_2,y_2)$. We focus on the center point of both circles given by $$ \left( \frac{x_1+x_2}{2}, \frac{y_1+y_2}{2} \right). $$

The distance between the centers of the circles is given by $$ R = \sqrt{ (x_2-x_1)^2 + (y_2-y_1)^2}. $$

We can consider the following orthogonal vectors $$ \vec{a} = \left( \frac{x_2-x_1}{R}, \frac{y_2-y_1}{R} \right), \vec{b} = \left( \frac{y_2-y_1}{R}, - \frac{x_2-x_1}{R} \right). $$

How many points can two circles intersect?

In the $(\vec{a},\vec{b})$ plane we get the equations $$ \big( a + R / 2 \big)^2 + b^2 = r_1^2,\\ \big( a - R / 2 \big)^2 + b^2 = r_2^2. $$

Whence $$ a = \frac{r_1^2 - r_2^2}{2R},\\ b = \pm \sqrt{ \frac{r_1^2+r_2^2}{2} - \frac{(r_1^2-r_2^2)^2}{4R^2} - \frac{R^2}{4}}. $$ The intersection points are given by $$ (x,y) = \frac{1}{2} \big( x_1+x_2, y_1+y_2 \big) + \frac{r_1^2 - r_2^2}{2R^2} \big( x_2-x_1, y_2-y_1 \big)\\ \pm \frac{1}{2} \sqrt{ 2 \frac{r_1^2+r_2^2}{R^2} - \frac{(r_1^2-r_2^2)^2}{R^4} - 1} \big( y_2-y_1, x_1-x_2 \big), $$ where $R$ is the distance between the centers of the circles.