-
Notifications
You must be signed in to change notification settings - Fork 1
/
centroid.py
42 lines (29 loc) · 819 Bytes
/
centroid.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
def find_centroid(v):
ans = [0, 0]
n = len(v)
signedArea = 0
# For all vertices
for i in range(len(v)):
x0 = v[i][0]
y0 = v[i][1]
x1 = v[(i + 1) % n][0]
y1 =v[(i + 1) % n][1]
# Calculate value of A
# using shoelace formula
A = (x0 * y1) - (x1 * y0)
signedArea += A
# Calculating coordinates of
# centroid of polygon
ans[0] += (x0 + x1) * A
ans[1] += (y0 + y1) * A
signedArea *= 0.5
ans[0] = (ans[0]) / (6 * signedArea)
ans[1] = (ans[1]) / (6 * signedArea)
return ans
def testCentroid():
vp = [ [ 1, 2 ],
[ 3, -4 ],
[ 6, -7 ] ]
ans = find_centroid(vp)
print(round(ans[0], 12), ans[1])
#testCentroid()