[BOJ] 2166번 다각형의 면적 - Python

"기하"

Posted by Yongmin on September 28, 2021

문제

다각형의 면적

풀이

삼각형 넓이 구하는 신발끈 공식으로, 한 점을 기준으로 삼각형을 만들어내서 다 구한다. 이 때, 매 삼각형을 구할 때 부호를 고려하여 빼주고 더해주기를 연산시켜준다. 그 이후 최종 값을 절댓값을 취한 뒤 정답을 도출해낸다.

소스 코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import sys
from math import *

def ccw(x1, y1, x2, y2, x3, y3):
    ccw_result = (x1*y2 + x2*y3 + x3*y1) - (y1*x2 + y2*x3 + y3*x1)
    return ccw_result

number = int(input())

pointlist = []

x, y = map(int, input().split())

for _ in range(0, number-1):
    a, b = map(int, input().split())
    pointlist.append([a, b])

result = 0
for i in range(0, number-2):
    
    result += ccw(x, y, pointlist[i][0], pointlist[i][1], pointlist[i+1][0], pointlist[i+1][1])
    
    
print(round(abs(result)/2, 1))


# # #