[英]How to find all intersection points of graphs in a loop in Python?
我绘制了一组特定的圆,如下所示:
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()
我收到:
如何找到圆之间的所有交点?
这是一些SymPy代码,用于查找所有交点。 请注意,您的代码会多次生成很多圆圈,因此我将它们放在一组中。 (当然,圆与自身的圆-圆交点本身就是圆,在这种情况下,相交不会返回列表,而只会返回圆。)
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]
哪个输出:
[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 for x, y in all_intersections_as_tuple], [y for x, y in all_intersections_as_tuple], 'or', color='red')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.