@@ -53,33 +53,116 @@ public static Vector3 InverseTransformPoint(this Vector3 point, Vector3 translat
5353 return Vector3 . Scale ( scaleInv , ( Quaternion . Inverse ( rotation ) * ( point - translation ) ) ) ;
5454 }
5555
56- public static Vector3 Average ( ICollection < Vector3 > vectors )
56+ public static Vector2 Average ( this IEnumerable < Vector2 > vectors )
5757 {
58- if ( vectors . Count == 0 )
58+ float x = 0f ;
59+ float y = 0f ;
60+ int count = 0 ;
61+ foreach ( var pos in vectors )
62+ {
63+ x += pos . x ;
64+ y += pos . y ;
65+ count ++ ;
66+ }
67+ return new Vector2 ( x / count , y / count ) ;
68+ }
69+
70+ public static Vector3 Average ( this IEnumerable < Vector3 > vectors )
71+ {
72+ float x = 0f ;
73+ float y = 0f ;
74+ float z = 0f ;
75+ int count = 0 ;
76+ foreach ( var pos in vectors )
77+ {
78+ x += pos . x ;
79+ y += pos . y ;
80+ z += pos . z ;
81+ count ++ ;
82+ }
83+ return new Vector3 ( x / count , y / count , z / count ) ;
84+ }
85+
86+ public static Vector2 Average ( this ICollection < Vector2 > vectors )
87+ {
88+ int count = vectors . Count ;
89+ if ( count == 0 )
90+ {
91+ return Vector2 . zero ;
92+ }
93+
94+ float x = 0f ;
95+ float y = 0f ;
96+ foreach ( var pos in vectors )
97+ {
98+ x += pos . x ;
99+ y += pos . y ;
100+ }
101+ return new Vector2 ( x / count , y / count ) ;
102+ }
103+
104+ public static Vector3 Average ( this ICollection < Vector3 > vectors )
105+ {
106+ int count = vectors . Count ;
107+ if ( count == 0 )
59108 {
60109 return Vector3 . zero ;
61110 }
62111
63- var x = 0f ;
64- var y = 0f ;
65- var z = 0f ;
112+ float x = 0f ;
113+ float y = 0f ;
114+ float z = 0f ;
66115 foreach ( var pos in vectors )
67116 {
68117 x += pos . x ;
69118 y += pos . y ;
70119 z += pos . z ;
71120 }
72- return new Vector3 ( x / vectors . Count , y / vectors . Count , z / vectors . Count ) ;
121+ return new Vector3 ( x / count , y / count , z / count ) ;
122+ }
123+
124+ public static Vector2 Median ( this IEnumerable < Vector2 > vectors )
125+ {
126+ int count = vectors . Count ( ) ;
127+ if ( count == 0 )
128+ {
129+ return Vector2 . zero ;
130+ }
131+
132+ return vectors . OrderBy ( v => v . sqrMagnitude ) . ElementAt ( count / 2 ) ;
133+ }
134+
135+ public static Vector3 Median ( this IEnumerable < Vector3 > vectors )
136+ {
137+ int count = vectors . Count ( ) ;
138+ if ( count == 0 )
139+ {
140+ return Vector3 . zero ;
141+ }
142+
143+ return vectors . OrderBy ( v => v . sqrMagnitude ) . ElementAt ( count / 2 ) ;
144+ }
145+
146+ public static Vector2 Median ( this ICollection < Vector2 > vectors )
147+ {
148+ int count = vectors . Count ;
149+ if ( count == 0 )
150+ {
151+ return Vector2 . zero ;
152+ }
153+
154+ return vectors . OrderBy ( v => v . sqrMagnitude ) . ElementAt ( count / 2 ) ;
73155 }
74156
75- public static Vector3 Median ( ICollection < Vector3 > vectors )
157+ public static Vector3 Median ( this ICollection < Vector3 > vectors )
76158 {
77- if ( vectors . Count == 0 )
159+ int count = vectors . Count ;
160+ if ( count == 0 )
78161 {
79162 return Vector3 . zero ;
80163 }
81164
82- return vectors . OrderBy ( v => v . sqrMagnitude ) . ElementAt ( vectors . Count / 2 ) ;
165+ return vectors . OrderBy ( v => v . sqrMagnitude ) . ElementAt ( count / 2 ) ;
83166 }
84167 }
85168}
0 commit comments