2017-05-28 81 views
0

我一直在努力:如何獲得三角形外接圓的中心和半徑(2D)? 我迄今所做的:2D中三角形的圓圈

I used the 1.5 linear system that is easily computable, however my implementation does not work at all...

我的代碼(在Python中,使用的方便numpy的):

def cercle_circonscrit(T): 
    A = np.array([[x3-x1,y3-y1],[x3-x2,y3-y2]]) 
    Y = np.array([(x3**2 + y3**2 - x1**2 - y1**2),(x3**2+y3**2 - x2**2-y2**2)]) 
    if np.linalg.det(A) == 0: 
     return False 
    Ainv = np.linalg.inv(A) 
    X = 0.5*np.dot(Ainv,Y) 
    x,y = X[0],X[1] 
    r = sqrt((x-x1)**2+(y-y1)**2) 
    return (x,y),r 

到目前爲止返回莫名的結果...但是我沒有嘗試證明上面的公式,這可能是錯誤的... 謝謝

+0

哪裏有變量x1,X2,X3,等來的?它們應該是函數的參數或從參數中提取。 – lukess

回答

0

你可能忘了獲取(x1,y1)等作爲函數的參數。

這應該工作:

import numpy as np 
from math import sqrt 

def cercle_circonscrit(T): 
    (x1, y1), (x2, y2), (x3, y3) = T 
    A = np.array([[x3-x1,y3-y1],[x3-x2,y3-y2]]) 
    Y = np.array([(x3**2 + y3**2 - x1**2 - y1**2),(x3**2+y3**2 - x2**2-y2**2)]) 
    if np.linalg.det(A) == 0: 
     return False 
    Ainv = np.linalg.inv(A) 
    X = 0.5*np.dot(Ainv,Y) 
    x,y = X[0],X[1] 
    r = sqrt((x-x1)**2+(y-y1)**2) 
    return (x,y),r 

T = ((0, 0), (1, 0), (0, 1)) 
cercle_circonscrit(T) 
#--> ((0.5, 0.5), 0.7071067811865476) 
+0

謝謝!我確實把它們放在了我的代碼中(我在這裏做了一個錯誤的代碼複製代碼,而且我認爲在代碼中實際獲取x1,y1等代碼時也犯了一個錯誤,因爲代碼完美無缺)。 –