题解 | 三角形面积

三角形面积

https://www.nowcoder.com/practice/52992a1ac2b842cc84d3fd3813b9566d

import math

class Point:
    def __init__(self, x, y):
        self.x = x
        self.y = y

class Triangle:
    def __init__(self, a, b, c):
        self.a = a
        self.b = b
        self.c = c

def get_area(T):
    # TODO: 计算三角形T的面积
    # 计算向量 AB 和 AC
    AB = (T.b.x - T.a.x, T.b.y - T.a.y)
    AC = (T.c.x - T.a.x, T.c.y - T.a.y)
    
    # 计算叉乘
    cross_product = AB[0] * AC[1] - AB[1] * AC[0]
    
    # 计算面积
    area = 0.5 * abs(cross_product)
    
    return round(area, 2)














































































































































































































































































































































def main():
    x, y = map(int, input().split())
    a = Point(x, y)
    
    x, y = map(int, input().split())
    b = Point(x, y)
    
    x, y = map(int, input().split())
    c = Point(x, y)
    
    T = Triangle(a, b, c)
    print("{:.2f}".format(get_area(T)))

if __name__ == "__main__":
    main()

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务