Last active
August 29, 2015 14:27
-
-
Save you-ssk/9a3e6bae2cdb3ec7101f 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
require 'matrix' | |
def centroid points | |
base = points[0] | |
z = (points[1] - base).cross(points[2] - base).normalize | |
ds = [] | |
total_area = 0.0 | |
points.drop(1).each_cons(2) do |cons| | |
a, b = cons[0] - base, cons[1] - base | |
c = (base + cons[0] + cons[1])/3.0 | |
area = a.cross(b).magnitude/2 | |
ds << c * area | |
total_area += area | |
end | |
ds.inject(:+)/total_area | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment