Created
October 22, 2015 08:05
-
-
Save zeffii/47af1bdb8873d0efdfa5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# http://www.mathopenref.com/coordpolygonarea.html | |
from mathutils import Vector | |
def calc_area_from_2d_colist(v): | |
sum = 0 | |
n = len(v) | |
for i in range(n-1): | |
sum += ((v[i][0] * v[i+1][1]) - (v[i][1] * v[i+1][0])) | |
sum += ((v[n-1][0] * v[0][1]) - (v[n-1][1] * v[0][0])) | |
return abs(sum / 2) | |
def calc_area_from_2d_vectorlist(v): | |
sum = 0 | |
n = len(v) | |
for i in range(n-1): | |
sum += ((v[i].x * v[i+1].y) - (v[i].y * v[i+1].x)) | |
sum += ((v[n-1].x * v[0].y) - (v[n-1].y * v[0].x)) | |
return abs(sum / 2) | |
cl = [[3, 7], [9, 7], [11, 2], [2, 2]] | |
f = calc_area_from_2d_colist(cl) | |
print(f) | |
vl = [Vector((3, 7)), Vector((9, 7)), Vector((11, 2)), Vector((2, 2))] | |
f = calc_area_from_2d_vectorlist(vl) | |
print(f) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment