[英]Finding the intersection of two circles
我試圖在 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)))
我希望取回值,但返回的集合是空的。
求解兩個圓的交點的正確方法是代數方法。 由於坐標系(實數)的無限精度,您不能使用點(x,y 坐標)來做到這一點。
如果兩個圓在兩個點相交,那么有一種直接的方法可以計算出這兩個交點。 Intersection of two circles
部分詳細介紹了代數。
我們還可以消除兩個圓不相交的情況,如下所示
返回兩個圓的兩個交點的代碼。 每個 cricle 由其中心 (x,y) 和半徑 (r) 描述
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 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_intersections(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_intersections(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_intersections(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')
輸出:
看看你生成了什么:
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]
首先,您已經生成了獨立的坐標列表; 您沒有任何類型的協調對點。
其次,你沒有列出圓上的所有點:你不能,因為那是一個無限集。 相反,您生成了一個等間距的列表(好吧,每個x
和y
一個)沒有數學理由期望您將在任何兩個這樣的坐標之間進行精確匹配,更不用說碰巧選擇兩個點了每個圓圈恰好是交點。
你什么也得不到,因為你的列表沒有共同點。 如果要找到交點,則需要通過代數求解、逐次逼近或其他一些方法來實現。 例如,取兩個圓的差值並求解y == 0
的方程。
如果您正在使用圓圈,獲得交點的正確方法是使用一些代數。 有四種可能的情況:不相交、一個相交(相切)、兩個相交和無限相交(它們是同一個圓)。我們重點關注兩個相交的情況。
從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)
上面繪制交點的代碼沒有正確繪制交點。 我已經調整了代碼來繪制它們如下:
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')
圖像中給出了輸出:
數學/幾何事物的錯誤語言。 這是更合適的語言(WL)中的樣子
Circle @@@ Thread @ {RandomReal[{-1,1},{3,2}], RandomReal[{.5,1},3]} //
Graphics[{
#, Red,
RegionIntersection @@@ #~Subsets~{2}
}]&
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.