How To Find All Intersection Points Of Graphs In A Loop In Python?
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
Solution 1:
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 inrange(1,len(numbers)+1):
for n in numbers:
for j inrange(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')
Post a Comment for "How To Find All Intersection Points Of Graphs In A Loop In Python?"