Python三角形面积

img


这怎么写啊我是初学者以前没有编程基础,才上了一节课就布置题目了,请各位dl指点


# 输入三角形三个顶点的坐标,计算三角形的面积
import math

class Trangle:
    #定义三个点的类属性
    def Point1(self, x1, y1):
        self.x1 = x1
        self.y1 = y1

    def Point2(self, x2, y2):
        self.x2 = x2
        self.y2 = y2

    def Point3(self, x3, y3):
        self.x3 = x3
        self.y3 = y3

    #求三条边的距离
    def Distance1(self):
        self.a = pow(pow(int(self.x1) - int(self.x2), 2) + pow(int(self.y1) - int(self.y2), 2), 0.5)
        print("第一条边长为", self.a)
    def Distance2(self):
        self.b = pow(pow(int(self.x1) - int(self.x3), 2) + pow(int(self.y1) - int(self.y3), 2), 0.5)
        print("第二条边长为:", self.b)
    def Distance3(self):
        self.c = pow(pow(int(self.x2) - int(self.x3), 2) + pow(int(self.y2) - int(self.y3), 2), 0.5)
        print("第三条边长为:", self.c)

    #通过海伦公式用三条边求面积
    def Area(self):
        s = (self.a + self.b + self.c) / 2
        area = pow((s * (s - self.a) * (s - self.b) * (s - self.c)), 0.5)
        print("三角形的面积为:", area)


a = Trangle()
a.Point1(input('请输入x1:'), input('请输入y1:'))
a.Point2(input('请输入x2:'), input('请输入y2:'))
a.Point3(input('请输入x3:'), input('请输入y3:'))
a.Distance1()
a.Distance2()
a.Distance3()
a.Area()

img


有帮助的话采纳一下哦!


        def heron(a, b, c):
            p = 0.5*(a+b+c)
            return (p*abs(p-a)*abs(p-b)*abs(p-c)) ** 0.5