这里提供两种方法,一种是通过输入三个顶点的坐标计算边长和面积,另一种是通过输入三条边的长度计算面积。

方法一:

import math

# 输入三个顶点的坐标
x1, y1 = map(float, input("请输入第一个顶点的坐标(用空格隔开):").split())
x2, y2 = map(float, input("请输入第二个顶点的坐标(用空格隔开):").split())
x3, y3 = map(float, input("请输入第三个顶点的坐标(用空格隔开):").split())

# 计算三角形三条边的长度
a = math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2)
b = math.sqrt((x3 - x2) ** 2 + (y3 - y2) ** 2)
c = math.sqrt((x1 - x3) ** 2 + (y1 - y3) ** 2)

# 计算半周长
s = (a + b + c) / 2

# 计算面积
area = math.sqrt(s * (s - a) * (s - b) * (s - c))

# 输出结果
print("边长为:{:.2f},{:.2f},{:.2f}".format(a, b, c))
print("面积为:{:.2f}".format(area))

方法二:

import math

# 输入三条边的长度
a, b, c = map(float, input("请输入三角形三条边的长度(用空格隔开):").split())

# 计算半周长
s = (a + b + c) / 2

# 计算面积
area = math.sqrt(s * (s - a) * (s - b) * (s - c))

# 输出结果
print("边长为:{:.2f},{:.2f},{:.2f}".format(a, b, c))
print("面积为:{:.2f}".format(area))

这里对结果进行了保留两位小数的处理。

用python输出三角形的边 面积

原文地址: https://www.cveoy.top/t/topic/7Mx 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录