refactor: changed math package to geometry

pull/14/head
RuiAlonso 4 years ago
parent 4faf703648
commit 4290cb5a8d

@ -1,7 +1,7 @@
import 'package:flame/extensions.dart'; import 'package:flame/extensions.dart';
import 'package:flame_forge2d/flame_forge2d.dart'; import 'package:flame_forge2d/flame_forge2d.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:maths/maths.dart'; import 'package:geometry/geometry.dart';
/// {@template pathway} /// {@template pathway}
/// [Pathway] creates lines of various shapes that the [Ball] can collide /// [Pathway] creates lines of various shapes that the [Ball] can collide
@ -21,7 +21,7 @@ class Pathway extends BodyComponent {
} }
/// {@macro pathway} /// {@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. /// given a [position] for the body, between a [start] and [end] points.
/// It creates two [ChainShape] separated by a [pathwayWidth]. /// It creates two [ChainShape] separated by a [pathwayWidth].
@ -60,7 +60,7 @@ class Pathway extends BodyComponent {
} }
/// {@macro pathway} /// {@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 /// 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 /// circumference and an [angle] to specify the size of it (360 will return
@ -107,7 +107,7 @@ class Pathway extends BodyComponent {
} }
/// {@macro pathway} /// {@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 /// The curve is created given a [position] for the body, and
/// with a list of control points specified by [controlPoints]. /// with a list of control points specified by [controlPoints].

@ -0,0 +1,3 @@
library geometry;
export 'src/geometry.dart';

@ -1,9 +1,15 @@
import 'dart:math' as math; import 'dart:math' as math;
import 'package:flame/extensions.dart'; import 'package:flame/extensions.dart';
/// Method to calculate all points (with a required precision amount of them) /// Calculates all [Vector2]s of a circumference.
/// of a circumference based on angle, offsetAngle and radius ///
/// https://en.wikipedia.org/wiki/Trigonometric_functions /// 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<Vector2> calculateArc({ List<Vector2> calculateArc({
required Vector2 center, required Vector2 center,
required double radius, required double radius,
@ -26,18 +32,28 @@ List<Vector2> calculateArc({
return points; return points;
} }
/// Method that calculates all points of a bezier curve of degree 'g' and /// Calculates all [Vector2]s of a bezier curve.
/// n=g-1 control points and range 0<=t<=1 ///
/// https://en.wikipedia.org/wiki/B%C3%A9zier_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<Vector2> calculateBezierCurve({ List<Vector2> calculateBezierCurve({
required List<Vector2> controlPoints, required List<Vector2> controlPoints,
double step = 0.001, 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( assert(
controlPoints.length >= 2, 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; var t = 0.0;
final n = controlPoints.length - 1; final n = controlPoints.length - 1;
final points = <Vector2>[]; final points = <Vector2>[];
@ -62,9 +78,10 @@ List<Vector2> calculateBezierCurve({
} }
/// Method to calculate the binomial coefficient of 'n' and 'k' /// 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) { 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) { if (k == 0 || n == k) {
return 1; return 1;
} else { } else {
@ -73,12 +90,11 @@ num binomial(num n, num k) {
} }
/// Method to calculate the factorial of some number 'n' /// 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) { num factorial(num n) {
assert(0 <= n, 'Non negative n'); assert(n >= 0, 'Factorial is not defined for negative number n ($n)');
if (n == 0) {
return 1; if (n == 0 || n == 1) {
} else if (n == 1) {
return 1; return 1;
} else { } else {
return n * factorial(n - 1); return n * factorial(n - 1);

@ -1,4 +1,4 @@
name: maths name: geometry
description: A Very Good Project created by Very Good CLI. description: A Very Good Project created by Very Good CLI.
version: 1.0.0+1 version: 1.0.0+1
publish_to: none publish_to: none

@ -1,7 +1,7 @@
// ignore_for_file: prefer_const_constructors, cascade_invocations // ignore_for_file: prefer_const_constructors, cascade_invocations
import 'package:flame/extensions.dart'; import 'package:flame/extensions.dart';
import 'package:flutter_test/flutter_test.dart'; import 'package:flutter_test/flutter_test.dart';
import 'package:maths/maths.dart'; import 'package:geometry/geometry.dart';
class Binomial { class Binomial {
Binomial({required this.n, required this.k}); Binomial({required this.n, required this.k});
@ -31,6 +31,7 @@ void main() {
expect(points.length, 50); expect(points.length, 50);
}); });
}); });
group('calculateBezierCurve', () { group('calculateBezierCurve', () {
test('fails if step not in range', () { test('fails if step not in range', () {
expect( expect(
@ -44,6 +45,7 @@ void main() {
throwsAssertionError, throwsAssertionError,
); );
}); });
test('fails if not enough control points', () { test('fails if not enough control points', () {
expect( expect(
() => calculateBezierCurve(controlPoints: [Vector2.zero()]), () => calculateBezierCurve(controlPoints: [Vector2.zero()]),
@ -81,12 +83,15 @@ void main() {
test('fails if k is negative', () { test('fails if k is negative', () {
expect(() => binomial(1, -1), throwsAssertionError); expect(() => binomial(1, -1), throwsAssertionError);
}); });
test('fails if n is negative', () { test('fails if n is negative', () {
expect(() => binomial(-1, 1), throwsAssertionError); expect(() => binomial(-1, 1), throwsAssertionError);
}); });
test('fails if n < k', () { test('fails if n < k', () {
expect(() => binomial(1, 2), throwsAssertionError); expect(() => binomial(1, 2), throwsAssertionError);
}); });
test('for a specific input gives a correct value', () { test('for a specific input gives a correct value', () {
final binomialInputsToExpected = { final binomialInputsToExpected = {
Binomial(n: 0, k: 0): 1, Binomial(n: 0, k: 0): 1,
@ -123,10 +128,12 @@ void main() {
}); });
}); });
}); });
group('factorial', () { group('factorial', () {
test('fails if negative number', () { test('fails if negative number', () {
expect(() => factorial(-1), throwsAssertionError); expect(() => factorial(-1), throwsAssertionError);
}); });
test('for a specific input gives a correct value', () { test('for a specific input gives a correct value', () {
final factorialInputsToExpected = { final factorialInputsToExpected = {
0: 1, 0: 1,

@ -1,3 +0,0 @@
library maths;
export 'src/maths.dart';

@ -198,6 +198,13 @@ packages:
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "2.1.2" version: "2.1.2"
geometry:
dependency: "direct main"
description:
path: "packages/geometry"
relative: true
source: path
version: "1.0.0+1"
glob: glob:
dependency: transitive dependency: transitive
description: description:
@ -261,13 +268,6 @@ packages:
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "0.1.3" 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: meta:
dependency: transitive dependency: transitive
description: description:

@ -17,9 +17,9 @@ dependencies:
flutter_bloc: ^8.0.1 flutter_bloc: ^8.0.1
flutter_localizations: flutter_localizations:
sdk: flutter sdk: flutter
geometry:
path: packages/geometry
intl: ^0.17.0 intl: ^0.17.0
maths:
path: packages/maths
dev_dependencies: dev_dependencies:
bloc_test: ^9.0.2 bloc_test: ^9.0.2

Loading…
Cancel
Save