feat: created maths package for calculate curves

pull/14/head
RuiAlonso 3 years ago
parent fee6a000a4
commit 4e2f56a70c

@ -0,0 +1,7 @@
# See https://www.dartlang.org/guides/libraries/private-files
# Files and directories created by pub
.dart_tool/
.packages
build/
pubspec.lock

@ -0,0 +1,11 @@
# maths
[![style: very good analysis][very_good_analysis_badge]][very_good_analysis_link]
[![License: MIT][license_badge]][license_link]
A Very Good Project created by Very Good CLI.
[license_badge]: https://img.shields.io/badge/license-MIT-blue.svg
[license_link]: https://opensource.org/licenses/MIT
[very_good_analysis_badge]: https://img.shields.io/badge/style-very_good_analysis-B22C89.svg
[very_good_analysis_link]: https://pub.dev/packages/very_good_analysis

@ -0,0 +1 @@
include: package:very_good_analysis/analysis_options.2.4.0.yaml

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

@ -0,0 +1,81 @@
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
List<Vector2> calculateArc({
required Vector2 center,
required double radius,
required double angle,
double offsetAngle = 0,
int precision = 100,
}) {
final stepAngle = radians(angle / precision);
final stepOffset = radians(offsetAngle);
final points = <Vector2>[];
for (var i = 0; i <= precision; i++) {
final xCoord = center.x + radius * math.cos((stepAngle * i) + stepOffset);
final yCoord = center.y - radius * math.sin((stepAngle * i) + stepOffset);
final point = Vector2(xCoord, yCoord);
points.add(point);
}
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
List<Vector2> calculateBezierCurve({
required List<Vector2> controlPoints,
double step = 0.001,
}) {
assert(
controlPoints.length >= 2,
'At least 2 control points to create a bezier curve',
);
var t = 0.0;
final n = controlPoints.length - 1;
final points = <Vector2>[];
do {
var xCoord = 0.0;
var yCoord = 0.0;
for (var i = 0; i <= n; i++) {
final point = controlPoints[i];
xCoord +=
_binomial(n, i) * math.pow(1 - t, n - i) * math.pow(t, i) * point.x;
yCoord +=
_binomial(n, i) * math.pow(1 - t, n - i) * math.pow(t, i) * point.y;
}
points.add(Vector2(xCoord, yCoord));
t = t + step;
} while (t <= 1);
return points;
}
/// Method to calculate the binomial coefficient of 'n' and 'k'
/// https://en.wikipedia.org/wiki/Binomial_coefficient
num _binomial(num n, num k) {
assert(0 <= k && k <= n, 'Range 0<=k<=n');
if (k == 0 || n == k) {
return 1;
} else {
return _factorial(n) / (_factorial(k) * _factorial(n - k));
}
}
/// Method to calculate the factorial of some number 'n'
/// https://en.wikipedia.org/wiki/Factorial
num _factorial(num n) {
if (n == 1) {
return 1;
} else {
return n * _factorial(n - 1);
}
}

@ -0,0 +1,15 @@
name: maths
description: A Very Good Project created by Very Good CLI.
version: 1.0.0+1
publish_to: none
environment:
sdk: ">=2.16.0 <3.0.0"
dev_dependencies:
coverage: ^1.1.0
mocktail: ^0.2.0
test: ^1.19.2
very_good_analysis: ^2.4.0
dependencies:
flame: ^1.0.0

@ -0,0 +1,11 @@
// ignore_for_file: prefer_const_constructors
import 'package:maths/maths.dart';
import 'package:test/test.dart';
void main() {
group('Maths', () {
test('can be instantiated', () {
expect(Maths(), isNotNull);
});
});
}
Loading…
Cancel
Save