求三角形外接圆公式

python代码:

import math

x1,y1 = (0,0)
x2,y2 = (2,2)
x3,y3 = (3,0)

# 三角形3条边
a = math.sqrt(math.pow(x1-x2, 2) + math.pow(y1-y2, 2))
b = math.sqrt(math.pow(x1-x3, 2) + math.pow(y1-y3, 2))
c = math.sqrt(math.pow(x3-x2, 2) + math.pow(y3-y2, 2))

# 求外接圆半径
p = (a + b + c)/2
S = math.sqrt(p*(p - a)*(p - b)*(p - c))
r = ( a*b*c ) / (4.0*S)

# 求外接圆圆心坐标
x=((y2-y1)*(y3*y3-y1*y1+x3*x3-x1*x1)-(y3-y1)*(y2*y2-y1*y1+x2*x2-x1*x1))/(2.0*((x3-x1)*(y2-y1)-(x2-x1)*(y3-y1)))
y=((x2-x1)*(x3*x3-x1*x1+y3*y3-y1*y1)-(x3-x1)*(x2*x2-x1*x1+y2*y2-y1*y1))/(2.0*((y3-y1)*(x2-x1)-(y2-y1)*(x3-x1)))

from matplotlib.patches import Circle
fig = plt.figure(num=2, figsize=(7, 7))
cir1 = Circle(xy=(x,y), radius=r, alpha=0.3, color='red')
axes2 = fig.add_subplot(1, 1, 1)
axes2.add_patch(cir1)
plt.plot([x1,x2],[y1,y2])
plt.plot([x1,x3],[y1,y3])
plt.plot([x3,x2],[y3,y2])
plt.scatter(x,y)
plt.xlim((x-r-0.2, x+r+0.2))
plt.ylim((y-r-0.2, y+r+0.2))
plt.show()

外接圆:

求三角形外接圆公式_第1张图片

你可能感兴趣的:(python,三角形,外接圆)