File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ var Point = require ( './Point' ) ;
2+
3+ var GetCentroid = function ( points , out )
4+ {
5+ if ( out === undefined ) { out = new Point ( ) ; }
6+
7+ if ( ! Array . isArray ( points ) )
8+ {
9+ throw new Error ( 'GetCentroid points argument must be an array' ) ;
10+ }
11+
12+ var len = points . length ;
13+
14+ if ( len < 1 )
15+ {
16+ throw new Error ( 'GetCentroid points array must not be empty' ) ;
17+ }
18+ else if ( len === 1 )
19+ {
20+ out . x = points [ 0 ] . x ;
21+ out . y = points [ 0 ] . y ;
22+ }
23+ else
24+ {
25+ for ( var i = 0 ; i < len ; i ++ )
26+ {
27+ out . x += points [ i ] . x ;
28+ out . y += points [ i ] . y ;
29+ }
30+
31+ out . x /= len ;
32+ out . y /= len ;
33+ }
34+
35+ return out ;
36+ } ;
37+
38+ module . exports = GetCentroid ;
Original file line number Diff line number Diff line change @@ -11,6 +11,7 @@ Point.Divide = require('./Divide');
1111Point . Dot = require ( './Dot' ) ;
1212Point . Equals = require ( './Equals' ) ;
1313Point . Floor = require ( './Floor' ) ;
14+ Point . GetCentroid = require ( './GetCentroid' ) ;
1415Point . GetMagnitude = require ( './GetMagnitude' ) ;
1516Point . GetMagnitudeSq = require ( './GetMagnitudeSq' ) ;
1617Point . Interpolate = require ( './Interpolate' ) ;
You can’t perform that action at this time.
0 commit comments