checkio Area of a Convex Polygon

题目:
here is a convex polygon on a coordinate plane. This polygon is presented as a list of vertices coordinates. Each vertex is connected sequentially with the last connecting back to the first. A polygon has N vertices. You should write a program that will calculate the area of a polygon. The result should be given with one digits precision as ±0.1.
Example:

checkio([[1, 1], [9, 9], [9, 1]]) == 32
checkio([[4, 10], [7, 1], [1, 4]]) == 22.5
checkio([[1, 2], [3, 8], [9, 8], [7, 1]]) == 40
checkio([[3, 3], [2, 7], [5, 9], [8, 7], [7, 3]]) == 26
checkio([[7, 2], [3, 2], [1, 5],
[3, 9], [7, 9], [9, 6]]) == 42
checkio([[4, 1], [3, 4], [3, 7], [4, 8],
[7, 9], [9, 6], [7, 1]]) == 35.5
链接:
https://py.checkio.org/en/mission/area-of-a-convex-polygon/
代码:

def checkio(data):
    def san(x):
        a = x[2][1] - x[1][1]
        b = -(x[2][0] - x[1][0])
        c = (x[2][0] - x[1][0])*x[2][1] - (x[2][1] - x[1][1])*x[2][0]
        x0 = x[0][0]
        y0 = x[0][1]
        l1 = (a*x0+b*y0+c)/(a**2+b**2)**0.5
        l2 = (a**2+b**2)**0.5
        s = 0.5*l1*l2
        if s > 0:
            s = s
        else:
            s = -s
        return round(s, 1)
    x = data
    m = []
    for i in range(1, len(x)-1):
        m.append([x[0], x[i], x[i+1]])
    ss = 0
    for i in m:
        ss += san(i)
    return ss
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值