天天看點

python求圓和線段/直線的交點,糾正網上有問題的代碼

目的:求圓和線段的交點

網上搜出的代碼大多千篇一律,都是這樣:

python求圓和線段/直線的交點,糾正網上有問題的代碼
python求圓和線段/直線的交點,糾正網上有問題的代碼

千篇一律,或許根本運都沒運作就發出來了;

源代碼

# 計算圓 與 直接相交的點
def line_intersect_circle(p, lsp, esp):
    # p is the circle parameter, lsp and lep is the two end of the line
    x0, y0, r0 = p
    x1, y1 = lsp
    x2, y2 = esp
    if r0 == 0:
        return [[x1, y1]]
    if x1 == x2:
        if abs(r0) >= abs(x1 - x0):
            p1 = x1, round(y0 - math.sqrt(r0 ** 2 - (x1 - x0) ** 2), 5)
            p2 = x1, round(y0 + math.sqrt(r0 ** 2 - (x1 - x0) ** 2), 5)
            inp = [p1, p2]
            # select the points lie on the line segment
            inp = [p for p in inp if p[0] >= min(x1, x2) and p[0] <= max(x1, x2)]
        else:
            inp = []
    else:
        k = (y1 - y2) / (x1 - x2)
        b0 = y1 - k * x1
        a = k ** 2 + 1
        b = 2 * k * (b0 - y0) - 2 * x0
        c = (b0 - y0) ** 2 + x0 ** 2 - r0 ** 2
        delta = b ** 2 - 4 * a * c
        if delta >= 0:
            p1x = round((-b - math.sqrt(delta)) / (2 * a), 5)
            p2x = round((-b + math.sqrt(delta)) / (2 * a), 5)
            p1y = round(k * x1 + b0, 5)
            p2y = round(k * x2 + b0, 5)
            inp = [[p1x, p1y], [p2x, p2y]]
            # select the points lie on the line segment
            inp = [p for p in inp if p[0] >= min(x1, x2) and p[0] <= max(x1, x2)]
        else:
            inp = []

    return inp if inp != [] else [[x1, y1]]      

出現的問題

我輸入,求 圓心為(0,0),半徑為1的圓 與 線段(0.5,-0.5)(1.5,-1)的交點:

print(line_intersect_circle((0, 0, 1), (0.5, -0.5), (1.5, -1)))      

結果輸出:

​[[0.77178, -1.0]]​

一般一眼就能看出結果 x 沒問題,但是 y 錯了;

python求圓和線段/直線的交點,糾正網上有問題的代碼

x應該在0.5 和 1.5 之間,且還沒到1,y應該在-0.5到-1之間;

錯誤代碼

倒數第八行:

p1y = round(k * x1 + b0, 5)
            p2y = round(k * x2 + b0, 5)      

很明顯這裡的 p1y 和 p2y 還是用的函數一開始傳進去的 原始x坐标,而沒有用解方程得到的 交點x坐标,交點x坐标分别是p1x和p2x;

是以,這裡改為:

p1y = round(k * p1x + b0, 5)
            p2y = round(k * p2x + b0, 5)      

正确代碼

# 計算圓 與 直接相交的點
def line_intersect_circle(p, lsp, esp):
    # p is the circle parameter, lsp and lep is the two end of the line
    x0, y0, r0 = p
    x1, y1 = lsp
    x2, y2 = esp
    if r0 == 0:
        return [[x1, y1]]
    if x1 == x2:
        if abs(r0) >= abs(x1 - x0):
            p1 = x1, round(y0 - math.sqrt(r0 ** 2 - (x1 - x0) ** 2), 5)
            p2 = x1, round(y0 + math.sqrt(r0 ** 2 - (x1 - x0) ** 2), 5)
            inp = [p1, p2]
            # select the points lie on the line segment
            inp = [p for p in inp if p[0] >= min(x1, x2) and p[0] <= max(x1, x2)]
        else:
            inp = []
    else:
        k = (y1 - y2) / (x1 - x2)
        b0 = y1 - k * x1
        a = k ** 2 + 1
        b = 2 * k * (b0 - y0) - 2 * x0
        c = (b0 - y0) ** 2 + x0 ** 2 - r0 ** 2
        delta = b ** 2 - 4 * a * c
        if delta >= 0:
            p1x = round((-b - math.sqrt(delta)) / (2 * a), 5)
            p2x = round((-b + math.sqrt(delta)) / (2 * a), 5)
            p1y = round(k * p1x + b0, 5)
            p2y = round(k * p2x + b0, 5)
            inp = [[p1x, p1y], [p2x, p2y]]
            # select the points lie on the line segment
            inp = [p for p in inp if p[0] >= min(x1, x2) and p[0] <= max(x1, x2)]
        else:
            inp = []

    return inp if inp != [] else [[x1, y1]]