refactor: changed math package to geometry

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

@ -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].

@ -0,0 +1,3 @@
library geometry;
export '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<Vector2> calculateArc({
required Vector2 center,
required double radius,
@ -26,18 +32,28 @@ List<Vector2> 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<Vector2> calculateBezierCurve({
required List<Vector2> 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 = <Vector2>[];
@ -62,9 +78,10 @@ List<Vector2> 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);

@ -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

@ -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,

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

@ -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:

@ -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

Loading…
Cancel
Save