from collections import namedtuple
from math import sqrt
# A point has an x and a y coordinate
Point = namedtuple('Point', 'x y')
# A triangle is defined by three points
Triangle = namedtuple('Triangle', 'a b c')
def distance(p, q):
"""Returns the distance between the provided pair of points"""
return sqrt((p.x - q.x)**2 + (p.y - q.y)**2)
def perimeter(triangle):
"""Returns the perimeter of the provided triangle"""
return distance(triangle.a, triangle.b) + distance(triangle.b, triangle.c) + distance(triangle.c, triangle.a)
def area(triangle):
"""Returns the area of the provided triangle using Heron's formula"""
docstring에 따라 영역 함수의 본문을 작성합니다.
응답
# Unpack the triangle's points
a, b, c = triangle
# Calculate the semi-perimeter of the triangle
s = perimeter(triangle) / 2
# Return the area of the triangle using Heron's formula
return sqrt(s * (s - distance(a, b)) * (s - distance(b, c)) * (s - distance(c, a)))