refactor: simplified centroid function

pull/39/head
alestiago 4 years ago
parent e3d62fccc3
commit ea9dcc843b

@ -66,9 +66,7 @@ class SlingShot extends BodyComponent {
/// For more information: https://en.wikipedia.org/wiki/Centroid /// For more information: https://en.wikipedia.org/wiki/Centroid
// TODO(alestiago): move to geometry package. // TODO(alestiago): move to geometry package.
Vector2 centroid(List<Vector2> vertices) { Vector2 centroid(List<Vector2> vertices) {
final centroid = Vector2.zero(); assert(vertices.isNotEmpty, 'Vertices must not be empty');
for (final vertex in vertices) { final sum = vertices.reduce((a, b) => a + b);
centroid.add(vertex); return sum / vertices.length.toDouble();
}
return centroid / vertices.length.toDouble();
} }

Loading…
Cancel
Save