반응형
https://www.acmicpc.net/problem/2166
2166번: 다각형의 면적
첫째 줄에 N이 주어진다. 다음 N개의 줄에는 다각형을 이루는 순서대로 N개의 점의 x, y좌표가 주어진다. 좌표값은 절댓값이 100,000을 넘지 않는 정수이다.
www.acmicpc.net
n = int(input())
points = []
for i in range(n):
points.append(list(map(int, input().split())))
# add first point again for the line connecting the beginning and the end.
points.append(points[0])
area = 0
for i in range(n):
cur_x, cur_y = points[i][0], points[i][1]
next_x, next_y = points[i+1][0], points[i+1][1]
area += (cur_x + next_x)*(cur_y - next_y)
print(round(abs(area*0.5), 2))
https://www.mathopenref.com/coordpolygonarea2.html
Area of a polygon algorithm - Math Open Reference
If you know the coordinates of the vertices of a polygon, this algorithm can be used to find the area. The algorithm assumes the usual mathematical convention that positive y points upwards. In computer systems where positive y is downwards (most of them)
www.mathopenref.com
댓글