diff --git a/lib/game/components/pathway.dart b/lib/game/components/pathway.dart index 13595cc9..1fc48af1 100644 --- a/lib/game/components/pathway.dart +++ b/lib/game/components/pathway.dart @@ -1,7 +1,7 @@ import 'package:flame/extensions.dart'; import 'package:flame_forge2d/flame_forge2d.dart'; import 'package:flutter/material.dart'; -import 'package:maths/maths.dart'; +import 'package:geometry/geometry.dart'; /// {@template pathway} /// [Pathway] creates lines of various shapes that the [Ball] can collide @@ -21,7 +21,7 @@ class Pathway extends BodyComponent { } /// {@macro pathway} - /// [Pathway.straight] creates a straight pathway for the ball. + /// [Pathway.straight] creates a straight pathway for the [Ball]. /// /// given a [position] for the body, between a [start] and [end] points. /// It creates two [ChainShape] separated by a [pathwayWidth]. @@ -60,7 +60,7 @@ class Pathway extends BodyComponent { } /// {@macro pathway} - /// [Pathway.arc] creates an arc pathway for the ball. + /// [Pathway.arc] creates an arc pathway for the [Ball]. /// /// The arc is created given a [position] for the body, a [radius] for the /// circumference and an [angle] to specify the size of it (360 will return @@ -107,7 +107,7 @@ class Pathway extends BodyComponent { } /// {@macro pathway} - /// [Pathway.bezierCurve] creates a bezier curve pathway for the ball. + /// [Pathway.bezierCurve] creates a bezier curve pathway for the [Ball]. /// /// The curve is created given a [position] for the body, and /// with a list of control points specified by [controlPoints]. diff --git a/packages/maths/.gitignore b/packages/geometry/.gitignore similarity index 100% rename from packages/maths/.gitignore rename to packages/geometry/.gitignore diff --git a/packages/maths/README.md b/packages/geometry/README.md similarity index 100% rename from packages/maths/README.md rename to packages/geometry/README.md diff --git a/packages/maths/analysis_options.yaml b/packages/geometry/analysis_options.yaml similarity index 100% rename from packages/maths/analysis_options.yaml rename to packages/geometry/analysis_options.yaml diff --git a/packages/geometry/lib/geometry.dart b/packages/geometry/lib/geometry.dart new file mode 100644 index 00000000..2453ed05 --- /dev/null +++ b/packages/geometry/lib/geometry.dart @@ -0,0 +1,3 @@ +library geometry; + +export 'src/geometry.dart'; diff --git a/packages/maths/lib/src/maths.dart b/packages/geometry/lib/src/geometry.dart similarity index 53% rename from packages/maths/lib/src/maths.dart rename to packages/geometry/lib/src/geometry.dart index d0912418..c992e5a2 100644 --- a/packages/maths/lib/src/maths.dart +++ b/packages/geometry/lib/src/geometry.dart @@ -1,9 +1,15 @@ import 'dart:math' as math; import 'package:flame/extensions.dart'; -/// Method to calculate all points (with a required precision amount of them) -/// of a circumference based on angle, offsetAngle and radius -/// https://en.wikipedia.org/wiki/Trigonometric_functions +/// Calculates all [Vector2]s of a circumference. +/// +/// Circumference is created from a [center] and a [radius] +/// Also semi circumference could be created, specifying its [angle] in degrees +/// and the offset start angle [offsetAngle] for this semi circumference. +/// The higher the [precision], the more [Vector2]s will be calculated, +/// achieving a more rounded arc. +/// +/// For more information read: https://en.wikipedia.org/wiki/Trigonometric_functions. List calculateArc({ required Vector2 center, required double radius, @@ -26,18 +32,28 @@ List calculateArc({ return points; } -/// Method that calculates all points of a bezier curve of degree 'g' and -/// n=g-1 control points and range 0<=t<=1 -/// https://en.wikipedia.org/wiki/B%C3%A9zier_curve +/// Calculates all [Vector2]s of a bezier curve. +/// +/// A bezier curve of [controlPoints] that say how to create this curve. +/// First and last points specify the beginning and the end respectively +/// of the curve. The inner points specify the shape of the curve and +/// its turning points. +/// The [step] must be in range 0<=step<=1 and indicates the precision to +/// calculate the curve. +/// For more information read: https://en.wikipedia.org/wiki/B%C3%A9zier_curve List calculateBezierCurve({ required List controlPoints, double step = 0.001, }) { - assert(0 <= step && step <= 1, 'Range 0<=step<=1'); + assert( + 0 <= step && step <= 1, + 'Step ($step) must be in range 0 <= step <= 1', + ); assert( controlPoints.length >= 2, - 'At least 2 control points to create a bezier curve', + 'At least 2 control points needed to create a bezier curve', ); + var t = 0.0; final n = controlPoints.length - 1; final points = []; @@ -62,9 +78,10 @@ List calculateBezierCurve({ } /// Method to calculate the binomial coefficient of 'n' and 'k' -/// https://en.wikipedia.org/wiki/Binomial_coefficient +/// For more information read: https://en.wikipedia.org/wiki/Binomial_coefficient num binomial(num n, num k) { - assert(0 <= k && k <= n, 'Range 0<=k<=n'); + assert(0 <= k && k <= n, 'k ($k) and n ($n) must be in range 0 <= k <= n'); + if (k == 0 || n == k) { return 1; } else { @@ -73,12 +90,11 @@ num binomial(num n, num k) { } /// Method to calculate the factorial of some number 'n' -/// https://en.wikipedia.org/wiki/Factorial +/// For more information read: https://en.wikipedia.org/wiki/Factorial num factorial(num n) { - assert(0 <= n, 'Non negative n'); - if (n == 0) { - return 1; - } else if (n == 1) { + assert(n >= 0, 'Factorial is not defined for negative number n ($n)'); + + if (n == 0 || n == 1) { return 1; } else { return n * factorial(n - 1); diff --git a/packages/maths/pubspec.yaml b/packages/geometry/pubspec.yaml similarity index 95% rename from packages/maths/pubspec.yaml rename to packages/geometry/pubspec.yaml index b037f4a4..99c09cd0 100644 --- a/packages/maths/pubspec.yaml +++ b/packages/geometry/pubspec.yaml @@ -1,4 +1,4 @@ -name: maths +name: geometry description: A Very Good Project created by Very Good CLI. version: 1.0.0+1 publish_to: none diff --git a/packages/maths/test/src/maths_test.dart b/packages/geometry/test/src/geometry_test.dart similarity index 98% rename from packages/maths/test/src/maths_test.dart rename to packages/geometry/test/src/geometry_test.dart index eaaf9367..6ecf66c8 100644 --- a/packages/maths/test/src/maths_test.dart +++ b/packages/geometry/test/src/geometry_test.dart @@ -1,7 +1,7 @@ // ignore_for_file: prefer_const_constructors, cascade_invocations import 'package:flame/extensions.dart'; import 'package:flutter_test/flutter_test.dart'; -import 'package:maths/maths.dart'; +import 'package:geometry/geometry.dart'; class Binomial { Binomial({required this.n, required this.k}); @@ -31,6 +31,7 @@ void main() { expect(points.length, 50); }); }); + group('calculateBezierCurve', () { test('fails if step not in range', () { expect( @@ -44,6 +45,7 @@ void main() { throwsAssertionError, ); }); + test('fails if not enough control points', () { expect( () => calculateBezierCurve(controlPoints: [Vector2.zero()]), @@ -81,12 +83,15 @@ void main() { test('fails if k is negative', () { expect(() => binomial(1, -1), throwsAssertionError); }); + test('fails if n is negative', () { expect(() => binomial(-1, 1), throwsAssertionError); }); + test('fails if n < k', () { expect(() => binomial(1, 2), throwsAssertionError); }); + test('for a specific input gives a correct value', () { final binomialInputsToExpected = { Binomial(n: 0, k: 0): 1, @@ -123,10 +128,12 @@ void main() { }); }); }); + group('factorial', () { test('fails if negative number', () { expect(() => factorial(-1), throwsAssertionError); }); + test('for a specific input gives a correct value', () { final factorialInputsToExpected = { 0: 1, diff --git a/packages/maths/lib/maths.dart b/packages/maths/lib/maths.dart deleted file mode 100644 index b340388b..00000000 --- a/packages/maths/lib/maths.dart +++ /dev/null @@ -1,3 +0,0 @@ -library maths; - -export 'src/maths.dart'; diff --git a/pubspec.lock b/pubspec.lock index db3b239a..5dee0c71 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -198,6 +198,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "2.1.2" + geometry: + dependency: "direct main" + description: + path: "packages/geometry" + relative: true + source: path + version: "1.0.0+1" glob: dependency: transitive description: @@ -261,13 +268,6 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "0.1.3" - maths: - dependency: "direct main" - description: - path: "/Users/ruialonso/dev/flutter/googleIO22/pinball/packages/maths" - relative: false - source: path - version: "1.0.0+1" meta: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index 7b9c9c19..1d0c8ada 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -17,9 +17,9 @@ dependencies: flutter_bloc: ^8.0.1 flutter_localizations: sdk: flutter + geometry: + path: packages/geometry intl: ^0.17.0 - maths: - path: packages/maths dev_dependencies: bloc_test: ^9.0.2