如何在Python中找到循环中图形的所有交点?

如何在Python中找到循环中图形的所有交点?,python,python-3.x,numpy,matplotlib,Python,Python 3.x,Numpy,Matplotlib,我绘制了一组特定的圆,如下所示: 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, 10

我绘制了一组特定的圆,如下所示:

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()
我收到:


如何找到圆之间的所有交点?

以下是一些查找所有交点的通用代码。请注意,您的代码多次生成许多圆圈,因此我将它们放在一个集合中。(圆与自身的圆交点当然是自身,在这种情况下,intersect不返回列表,只返回圆。)

来自sympy导入的
*
从sympy.geometry导入*
进口itertools
数字=[2,4,6]
圆=集合()
对于范围(1,长度(数字)+1)内的i:
对于n个数字:
对于范围(1,4)内的j:
圆。添加(圆(点(i,0),数字[i-1]*j))
所有交叉点=[]
对于itertools中的c1、c2。组合(圆圈,2):
所有交叉点+=c1.交叉点(c2)
打印(所有交叉点)
all_交叉点_as_tuple=[tuple(p.evalf())表示所有_交叉点中的p]
哪些产出:

[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))]
将它们添加到绘图中:

plt.plot([x代表x,y代表所有交叉点,y代表tuple],[y代表x,y代表所有交叉点,y代表tuple],'or',color='red')

每个圆圈都会出现3次,因为编号为n的
n
从未使用过。可能您希望枚举(数字)中的i,n使用
F=(X-i-1)**2+Y**2-(n*j)**2
`我可以在点()函数的sympy.geometry中使用浮点值吗?可以,但必须小心。由于sympy喜欢精确,浮点错误可能会导致大问题。最好是将您的浮动转换为同分数(即1.2345为S(12345)/10000)。见例。仅在最后使用evalf()转换为浮点。