-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPolygons.py
35 lines (30 loc) · 994 Bytes
/
Polygons.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
"""
Some tools for computing various properties of polygons
Author: Travis Dick ([email protected])
"""
import numpy as np
def polygon_descriptors(corners):
""" Computes the area, perimeter, and center of mass of a polygon.
Parameters:
-----------
corners : (2,n) numpy array
The vertices of the polygon. Should be in clockwise or
counter-clockwise order.
Returns:
--------
A tuple (perimeter, area, (center of mass x, center of mass y)).
"""
n_points = corners.shape[1]
p, a, cx, cy = 0, 0, 0, 0
for i in xrange(n_points):
j = (i+1) % n_points
dot = corners[0,i]*corners[1,j] - corners[0,j]*corners[1,i]
a += dot
cx += (corners[0,i] + corners[0,j]) * dot
cy += (corners[1,i] + corners[1,j]) * dot
p += np.linalg.norm(corners[:,i] - corners[:,j])
a /= 2
cx /= 6*a
cy /= 6*a
a = abs(a)
return (p, a, (cx,cy))