计算三角形面积
描述
平面上有一个三角形,它的三个顶点坐标分别为(x1, y1), (x2, y2), (x3, y3),那么请问这个三角形的面积是多少。
输入输入仅一行,包括6个单精度浮点数,分别对应x1, y1, x2, y2, x3, y3。输出输出也是一行,输出三角形的面积,精确到小数点后两位。样例输入
0 0 4 0 0 3
样例输出
6.00
提示海伦公式
def dist(a,b,c,d):
return ((a-c)**2+(b-d)**2)**0.5
x1,y1,x2,y2,x3,y3=[float(i) for i in input().split()]
a=dist(x1,y1,x2,y2)
b=dist(x1,y1,x3,y3)
c=dist(x3,y3,x2,y2)
p=(a+b+c)/2
print("%.2f" % (p*(p-a)*(p-b)*(p-c))**0.5)
解释:
海伦公式为:三角形三边的长度分别为a,b,c。那么p=(a+b+b)/2。那么三角形的最终面价为sqrt(p*(p-a)*(p-b)*(p-c))
def dist(a,b,c,d):
return ((a-c)**2+(b-d)**2)**0.5
为定义函数,返回两点间的距离,在函数部分将详细讲解。
浙公网安备 33010602011771号