@@ -25,34 +25,35 @@ d3.geom.hull = function(vertices) {
2525
2626 var fx = d3_functor ( x ) ,
2727 fy = d3_functor ( y ) ,
28+ i ,
2829 n = data . length ,
29- points = [ ] , // of the form [[x0, y0, 0], ..., [xn, yn, n]]
30- flipped_points = [ ] ;
30+ points = [ ] , // of the form [[x0, y0, 0], ..., [xn, yn, n]]
31+ flippedPoints = [ ] ;
3132
32- for ( var i = 0 ; i < n ; i ++ ) {
33+ for ( i = 0 ; i < n ; i ++ ) {
3334 points . push ( [ + fx . call ( this , data [ i ] , i ) , + fy . call ( this , data [ i ] , i ) , i ] ) ;
3435 }
3536
3637 // sort ascending by x-coord first, y-coord second
37- points . sort ( function ( a , b ) { return ( a [ 0 ] - b [ 0 ] || a [ 1 ] - b [ 1 ] ) ; } ) ;
38+ points . sort ( d3_geom_hullOrder ) ;
3839
3940 // we flip bottommost points across y axis so we can use the upper hull routine on both
40- for ( var i = 0 ; i < n ; i ++ ) flipped_points . push ( [ points [ i ] [ 0 ] , - points [ i ] [ 1 ] ] ) ;
41+ for ( i = 0 ; i < n ; i ++ ) flippedPoints . push ( [ points [ i ] [ 0 ] , - points [ i ] [ 1 ] ] ) ;
4142
42- var uhull = d3_geom_hull_find_upper_hull ( points ) ;
43- var lhull = d3_geom_hull_find_upper_hull ( flipped_points ) ;
43+ var upper = d3_geom_hullUpper ( points ) ,
44+ lower = d3_geom_hullUpper ( flippedPoints ) ;
4445
45- // construct the poly , removing possible duplicate endpoints
46- var skip_l = ( lhull [ 0 ] === uhull [ 0 ] ) ,
47- skip_r = ( lhull [ lhull . length - 1 ] === uhull [ uhull . length - 1 ] ) ,
48- poly = [ ] ;
46+ // construct the polygon , removing possible duplicate endpoints
47+ var skipLeft = lower [ 0 ] === upper [ 0 ] ,
48+ skipRight = lower [ lower . length - 1 ] === upper [ upper . length - 1 ] ) ,
49+ polygon = [ ] ;
4950
50- for ( var i = uhull . length - 1 ; i >= 0 ; i -- )
51- poly . push ( data [ points [ uhull [ i ] ] [ 2 ] ] ) ; // add upper hull in r->l order
52- for ( var i = + skip_l ; i < lhull . length - skip_r ; i ++ )
53- poly . push ( data [ points [ lhull [ i ] ] [ 2 ] ] ) ; // add lower hull in l->r order
51+ for ( i = upper . length - 1 ; i >= 0 ; -- i )
52+ polygon . push ( data [ points [ upper [ i ] ] [ 2 ] ] ) ; // add upper hull in r->l order
53+ for ( i = + skipLeft ; i < lower . length - skipRight ; ++ i )
54+ polygon . push ( data [ points [ lower [ i ] ] [ 2 ] ] ) ; // add lower hull in l->r order
5455
55- return poly ;
56+ return polygon ;
5657 }
5758
5859 hull . x = function ( _ ) {
@@ -69,7 +70,7 @@ d3.geom.hull = function(vertices) {
6970// finds the 'upper convex hull' (see wiki link above)
7071// assumes points arg has >=3 elements, is sorted by x, unique in y
7172// returns array of indices into points in left to right order
72- function d3_geom_hull_find_upper_hull ( points ) {
73+ function d3_geom_hullUpper ( points ) {
7374 var n = points . length ,
7475 hull = [ 0 , 1 ] ,
7576 hs = 2 ; // hull size
@@ -89,3 +90,6 @@ function d3_geom_hull_find_upper_hull(points) {
8990function d3_geom_hull_CW ( a , b , c ) {
9091 return ( b [ 0 ] - a [ 0 ] ) * ( c [ 1 ] - a [ 1 ] ) - ( b [ 1 ] - a [ 1 ] ) * ( c [ 0 ] - a [ 0 ] ) > 0 ;
9192}
93+
94+ // comparator for ascending sort by x-coord first, y-coord second
95+ function d3_geom_hullOrder ( a , b ) { return a [ 0 ] - b [ 0 ] || a [ 1 ] - b [ 1 ] ; }
0 commit comments