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