feat: geometric centroid (#38)

* docs: rephrased description

* feat: included geometry method

* feat: simplified centroid

* Update packages/geometry/test/src/geometry_test.dart

Co-authored-by: Allison Ryan <77211884+allisonryan0002@users.noreply.github.com>

* chore: rebase

* refactor: removed throwsAssertionError for throwsA matcher

* docs: rephrased doc comment

Co-authored-by: Allison Ryan <77211884+allisonryan0002@users.noreply.github.com>
pull/46/head
Alejandro Santiago 4 years ago committed by GitHub
parent 45f1c6e48b
commit 902aacce2c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -3,7 +3,7 @@
[![style: very good analysis][very_good_analysis_badge]][very_good_analysis_link] [![style: very good analysis][very_good_analysis_badge]][very_good_analysis_link]
[![License: MIT][license_badge]][license_link] [![License: MIT][license_badge]][license_link]
Helper package to calculate points of lines, arcs and curves for the pathways of the ball. Provides a set of helpers for working with 2D geometry.
[license_badge]: https://img.shields.io/badge/license-MIT-blue.svg [license_badge]: https://img.shields.io/badge/license-MIT-blue.svg
[license_link]: https://opensource.org/licenses/MIT [license_link]: https://opensource.org/licenses/MIT

@ -106,3 +106,12 @@ num factorial(num n) {
return n * factorial(n - 1); return n * factorial(n - 1);
} }
} }
/// Arithmetic mean position of all the [Vector2]s in a polygon.
///
/// For more information read: https://en.wikipedia.org/wiki/Centroid
Vector2 centroid(List<Vector2> vertices) {
assert(vertices.isNotEmpty, 'Vertices must not be empty');
final sum = vertices.reduce((a, b) => a + b);
return sum / vertices.length.toDouble();
}

@ -1,5 +1,5 @@
name: geometry name: geometry
description: Helper package to calculate points of lines, arcs and curves for the pathways of the ball description: Provides a set of helpers for working with 2D geometry.
version: 1.0.0+1 version: 1.0.0+1
publish_to: none publish_to: none

@ -166,4 +166,29 @@ void main() {
}); });
}); });
}); });
group('centroid', () {
test('throws AssertionError when vertices are empty', () {
expect(() => centroid([]), throwsA(isA<AssertionError>()));
});
test('is correct when one vertex is given', () {
expect(centroid([Vector2.zero()]), Vector2.zero());
});
test('is correct when two vertex are given', () {
expect(centroid([Vector2.zero(), Vector2(1, 1)]), Vector2(0.5, 0.5));
});
test('is correct when three vertex are given', () {
expect(
centroid([
Vector2.zero(),
Vector2(1, 1),
Vector2(2, 2),
]),
Vector2(1, 1),
);
});
});
} }

Loading…
Cancel
Save