1

I plot a certain set of circles as follows:

import numpy as np
import matplotlib.pyplot as plt

fig = plt.figure(1, figsize=(10,10))
numbers = [2,4,6]

for i in range(1,len(numbers)+1):
    for n in numbers:
        for j in range(1,4):

            x = np.linspace(-20, 25, 100)
            y = np.linspace(-20, 20, 100)

            X, Y = np.meshgrid(x,y)

            F = (X-i)**2 + Y**2 - (numbers[i-1]*j)**2

            ax = plt.gca()
            ax.set_aspect(1)
            plt.contour(X,Y,F,[0])

            plt.grid(linestyle='--')

plt.show()

And I receive:

enter image description here

How can I find all intersection points between circles?

Tomasz Przemski
  • 1,127
  • 9
  • 29
  • 1
    You get each circle 3 times because the `n` of `for n in numbers:` is never used. Probably you'ld want `for i, n in enumerate(numbers)` and `F = (X-i-1)**2 + Y**2 - (n*j)**2` ` – JohanC Nov 24 '19 at 19:43

1 Answers1

2

Here is some SymPy code to find all the intersections. Note that your code generates a lot of circles multiple times, so I put them in a set. (The circle-circle intersection of a circle with itself is of course itself, in which case intersect doesn't return a list, but just the circle.)

from sympy import *
from sympy.geometry import *
import itertools

numbers = [2,4,6]
circles = set()

for i in range(1,len(numbers)+1):
    for n in numbers:
        for j in range(1,4):
            circles.add(Circle(Point(i, 0), numbers[i-1]*j))

all_intersections = []
for c1, c2 in itertools.combinations(circles, 2):
    all_intersections += c1.intersection(c2)

print(all_intersections)
all_intersections_as_tuple = [tuple(p.evalf()) for p in all_intersections]

Which outputs:

[Point2D(5/2, -5*sqrt(23)/2), Point2D(5/2, 5*sqrt(23)/2), Point2D(-3, 0), Point2D(3/2, -3*sqrt(7)/2), Point2D(3/2, 3*sqrt(7)/2), Point2D(2, sqrt(35)), Point2D(2, -sqrt(35))]

Adding them to your plot:

plt.plot([x for x, y in all_intersections_as_tuple], [y for x, y in all_intersections_as_tuple], 'or', color='red')

intersections added to plot

JohanC
  • 71,591
  • 8
  • 33
  • 66
  • Can I use floating point values in sympy.geometry in the Point() function? – Tomasz Przemski Nov 29 '19 at 10:16
  • 1
    Yes, you can, but you have to be careful. As sympy loves to be exact, floating point errors can cause big problems. Best is to convert your floats to sympy fractions (i.e. 1.2345 as S(12345) /10000). See e.g. https://stackoverflow.com/questions/21005132/how-do-i-replace-floats-with-rationals-in-a-sympy-expression. Only at the very end convert to floats with evalf(). – JohanC Nov 29 '19 at 10:41