我正在尝试使用Python(使用Matplotlib)查找两个圆之间的交点,但无法获取任何值。
我这样做是通过为每个单独的圆创建X和Y的列表来实现的(Matplotlib在绘制圆时将第一个参数作为X值,第二个参数作为Y值),然后相应地与列表相交(例如, circle1 x值和circle2 x值)。
import numpy
import math
import matplotlib.pyplot as plt
import random
def origin_circle():
global x_points
global y_points
global r
global n
r=1
n=2**16
x_points=[(r*math.cos(t)) for t in numpy.linspace(0, 2*numpy.pi*r, n+1)]
y_points=[(r*math.sin(t)) for t in numpy.linspace(0, 2*numpy.pi*r, n+1)]
def new_circle(x_offset, y_offset):
global x_points1
global y_points1
x_points1=[x_offset+(r*math.cos(t)) for t in numpy.linspace(0, 2*numpy.pi*r, n+1)]
y_points1=[y_offset+(r*math.sin(t)) for t in numpy.linspace(0, 2*numpy.pi*r, n+1)]
origin_circle()
new_center= random.randint(0, len(x_points))
x_offset = x_points[new_center]
y_offset = y_points[new_center]
new_circle(x_offset, y_offset)
print(set(x_points1).intersection(set(x_points)))
print(set(y_points1).intersection(set(y_points)))
我希望取回值,但是返回的集合为空。
P.S .:希望这个问题写得不错,因为这是D,这可能是我在本网站上的最后一个问题:
答案 0 :(得分:1)
看看您生成的内容:
new_center= random.randint(0, len(x_points))
x_offset = x_points[new_center]
y_offset = y_points[new_center]
new_circle(x_offset, y_offset)
# I'm sorting these for easier visualization
print(sorted(x_points))
print(sorted(x_points1))
输出:
[-1.0, -0.9807852804032304, -0.9807852804032304, -0.9238795325112868,
-0.9238795325112867, -0.8314696123025455, -0.8314696123025453, -0.7071067811865477,
-0.7071067811865475, -0.5555702330196022, -0.555570233019602, -0.38268343236509034,
-0.3826834323650897, -0.19509032201612866, -0.1950903220161282,
-1.8369701987210297e-16, 6.123233995736766e-17, 0.1950903220161283,
0.19509032201612833, 0.38268343236508984, 0.38268343236509, 0.5555702330196018
, 0.5555702330196023, 0.7071067811865474, 0.7071067811865476, 0.8314696123025452,
0.8314696123025452, 0.9238795325112865, 0.9238795325112867, 0.9807852804032303,
0.9807852804032304, 1.0, 1.0]
[-2.0, -1.9807852804032304, -1.9807852804032304, -1.923879532511287,
-1.9238795325112867, -1.8314696123025453, -1.8314696123025453, -1.7071067811865477,
-1.7071067811865475, -1.5555702330196022, -1.555570233019602, -1.3826834323650903,
-1.3826834323650896, -1.1950903220161286, -1.1950903220161282, -1.0000000000000002,
-0.9999999999999999, -0.8049096779838717, -0.8049096779838717, -0.6173165676349102,
-0.6173165676349099, -0.44442976698039816, -0.4444297669803977, -0.29289321881345265,
-0.2928932188134524, -0.16853038769745476, -0.16853038769745476,
-0.07612046748871348, -0.07612046748871326, -0.01921471959676968,
-0.01921471959676957, 0.0, 0.0]
首先,您已经生成了独立的坐标列表;您没有 points 作为任何类型的协调对。
第二,您没有不列出圆上所有点的所有:您不能,因为那是一个无限集合。相反,您生成了一个等间距的列表(嗯,x
和y
每个)没有数学上的理由期望您会匹配完全相同的 在任何两个这样的坐标之间,更不用说碰巧选择每个圆上的两个点了,它们精确地相交点。
您一无所获,因为您的列表没有共同点。如果要找到相交点,则需要通过代数解,逐次逼近或某种其他方法来进行。例如,取两个圆的差并为y == 0
求解该方程。
答案 1 :(得分:1)
如果正在使用圆,则获取相交的正确方法是使用一些代数。有四种可能的情况:无相交,一个相交(相切),两个相交和无限相交(它们是相同的圆)。让我们集中讨论两个相交的情况。
您可以从https://math.stackexchange.com/a/256123/647423获得一个线性方程,该线性方程将x和y沿着穿过两个相交点的直线相关:
−2x(x1center−x2center)−2y(y1center−y2center) = (r1)^2−(r2)^2−((x1center)^2−(x2center)^2)−((y1center)^2−(y2center)^2).
据此,您可以根据x获得y的公式,然后将y替换为您的一个圆公式,从而获得x的二次方。 如果您不想实现二次方程式求解器,则可以像这样使用numpy.roots:
root_array = np.roots(quadratic_coeff, linear_coeff, constant_coef)
答案 2 :(得分:1)
求解两个圆的交点的正确方法是代数式的。由于坐标系(实数)的无限精度,因此无法使用点(x,y坐标)来执行此操作。
如果两个圆在两个点处相交,则有一种简单的方法可以计算这两个相交点。 Intersection of two circles
下的here详细介绍了代数。
我们还可以消除两个圆圈不相交的情况
返回两个圆的两个相交点的代码。每个小节均由其中心(x,y)和半径(r)来描述
def get_intercetions(x0, y0, r0, x1, y1, r1):
# circle 1: (x0, y0), radius r0
# circle 2: (x1, y1), radius r1
d=math.sqrt((x1-x0)**2 + (y1-y0)**2)
# non intersecting
if d > r0 + r1 :
return None
# One circle within other
if d < abs(r0-r1):
return None
# coincident circles
if d == 0 and r0 == r1:
return None
else:
a=(r0**2-r1**2+d**2)/(2*d)
h=math.sqrt(r0**2-a**2)
x2=x0+a*(x1-x0)/d
y2=y0+a*(y1-y0)/d
x3=x2+h*(y1-y0)/d
y3=y2-h*(x1-x0)/d
x4=x2-h*(y1-y0)/d
y4=y2+h*(x1-x0)/d
return (x3, y3, x4, y4)
让我们通过绘图对其进行可视化测试
# intersection circles
x0, y0 = 0, 0
r0 = 5
x1, y1 = 2, 2
r1 = 5
# intersecting with (x1, y1) but not with (x0, y0)
x2, y2 = -1,0
r2 = 2.5
circle1 = plt.Circle((x0, y0), r0, color='b', fill=False)
circle2 = plt.Circle((x1, y1), r1, color='b', fill=False)
circle3 = plt.Circle((x2, y2), r2, color='b', fill=False)
fig, ax = plt.subplots()
ax.set_xlim((-10, 10))
ax.set_ylim((-10, 10))
ax.add_artist(circle1)
ax.add_artist(circle2)
ax.add_artist(circle3)
intersections = get_intercetions(x0, y0, r0, x1, y1, r1)
if intersections is not None:
i_x3, i_y3, i_x4, i_y4 = intersections
plt.plot([i_x3, i_x4], [i_y3, i_y4], '.', color='r')
intersections = get_intercetions(x0, y0, r0, x2, y2, r2)
if intersections is not None:
i_x3, i_y3, i_x4, i_y4 = intersections
plt.plot([i_x3, i_x4], [i_y3, i_y4], '.', color='r')
intersections = get_intercetions(x1, y1, r1, x2, y2, r2)
if intersections is not None:
i_x3, i_y3, i_x4, i_y4 = intersections
plt.plot([i_x3, i_x4], [i_y3, i_y4], '.', color='r')
plt.gca().set_aspect('equal', adjustable='box')
输出:
答案 3 :(得分:0)
数学/几何事物的语言错误。这是使用更合适的语言(WL)的样子
Circle @@@ Thread @ {RandomReal[{-1,1},{3,2}], RandomReal[{.5,1},3]} //
Graphics[{
#, Red,
RegionIntersection @@@ #~Subsets~{2}
}]&
答案 4 :(得分:0)
上面绘制交点的代码没有正确绘制交点。 我已经调整了代码来绘制它们如下:
import matplotlib.pyplot as plt
import math
def get_intersections(x0, y0, r0, x1, y1, r1):
# circle 1: (x0, y0), radius r0
# circle 2: (x1, y1), radius r1
d=math.sqrt((x1-x0)**2 + (y1-y0)**2)
# non intersecting
if d > r0 + r1 :
return {}
# One circle within other
if d < abs(r0-r1):
return {}
# coincident circles
if d == 0 and r0 == r1:
return {}
else:
a=(r0**2-r1**2+d**2)/(2*d)
h=math.sqrt(r0**2-a**2)
x2=x0+a*(x1-x0)/d
y2=y0+a*(y1-y0)/d
x3=x2+h*(y1-y0)/d
y3=y2-h*(x1-x0)/d
x4=x2-h*(y1-y0)/d
y4=y2+h*(x1-x0)/d
return x3, y3, x4, y4
# intersection circles
x0, y0 = 0, 0
r0 = 5
x1, y1 = 2, 2
r1 = 5
# intersecting with (x1, y1) but not with (x0, y0)
x2, y2 = -1,0
r2 = 2.5
circle1 = plt.Circle((x0, y0), r0, color='b', fill=False)
circle2 = plt.Circle((x1, y1), r1, color='b', fill=False)
circle3 = plt.Circle((x2, y2), r2, color='b', fill=False)
fig = plt.figure(figsize = (10, 10))
plt.grid(True)
ax = fig.add_subplot(111)
ax.set_xlim((-10, 10))
ax.set_ylim((-10, 10))
ax.add_artist(circle1)
ax.add_artist(circle2)
ax.add_artist(circle3)
intersections1 = get_intersections(x0, y0, r0, x1, y1, r1)
print(intersections1)
if len(intersections1) > 0:
print(intersections3)
i_x3, i_y3, i_x4, i_y4 = intersections1
#plt.plot([i_x3, i_x4], [i_y3, i_y4], 'o', color='r')
ax.scatter([i_x3, i_x4],[i_y3, i_y4] ,marker ='X',s=300,alpha=1)
intersections2 = get_intersections(x0, y0, r0, x2, y2, r2)
print(intersections2)
if len(intersections2) > 0:
i_x3, i_y3, i_x4, i_y4 = intersections2
plt.plot([i_x3, i_x4], [i_y3, i_y4], 'o', color='r')
ax.scatter([i_x3, i_x4],[i_y3, i_y4] ,marker ='X',s=300,alpha=1)
intersections3 = get_intersections(x1, y1, r1, x2, y2, r2)
if len(intersections3) > 0:
print(intersections3)
i_x3, i_y3, i_x4, i_y4 = intersections3
#plt.plot([i_x3, i_x4], [i_y3, i_y4], 'o', color='r')
ax.scatter([i_x3, i_x4],[i_y3, i_y4] ,marker ='X',s=300,alpha=1)
plt.gca().set_aspect('equal', adjustable='box')
输出在图像中给出: