refactor: renaming params

pull/84/head
RuiAlonso 4 years ago
parent af1da61ca5
commit 7c01edceaa

@ -164,16 +164,16 @@ class Pathway extends BodyComponent with InitialPosition, Layered {
// TODO(ruialonso): Refactor repetitive logic // TODO(ruialonso): Refactor repetitive logic
final outerWall = calculateEllipse( final outerWall = calculateEllipse(
center: center, center: center,
bigRadius: bigRadius, majorRadius: bigRadius,
smallRadius: smallRadius, minorRadius: smallRadius,
).map((vector) => vector..rotate(rotation)).toList(); ).map((vector) => vector..rotate(rotation)).toList();
paths.add(outerWall); paths.add(outerWall);
if (!singleWall) { if (!singleWall) {
final innerWall = calculateEllipse( final innerWall = calculateEllipse(
center: center, center: center,
bigRadius: bigRadius - width, majorRadius: bigRadius - width,
smallRadius: smallRadius - width, minorRadius: smallRadius - width,
).map((vector) => vector..rotate(rotation)).toList(); ).map((vector) => vector..rotate(rotation)).toList();
paths.add(innerWall); paths.add(innerWall);
} }

@ -23,10 +23,10 @@ List<Vector2> calculateArc({
final points = <Vector2>[]; final points = <Vector2>[];
for (var i = 0; i < precision; i++) { for (var i = 0; i < precision; i++) {
final xCoord = center.x + radius * math.cos((stepAngle * i) + offsetAngle); final x = center.x + radius * math.cos((stepAngle * i) + offsetAngle);
final yCoord = center.y - radius * math.sin((stepAngle * i) + offsetAngle); final y = center.y - radius * math.sin((stepAngle * i) + offsetAngle);
final point = Vector2(xCoord, yCoord); final point = Vector2(x, y);
points.add(point); points.add(point);
} }
@ -35,8 +35,8 @@ List<Vector2> calculateArc({
/// Calculates all [Vector2]s of an ellipse. /// Calculates all [Vector2]s of an ellipse.
/// ///
/// An ellipse can be achieved by specifying a [center], a [bigRadius] and a /// An ellipse can be achieved by specifying a [center], a [majorRadius] and a
/// [smallRadius]. /// [minorRadius].
/// ///
/// The higher the [precision], the more [Vector2]s will be calculated; /// The higher the [precision], the more [Vector2]s will be calculated;
/// achieving a more rounded ellipse. /// achieving a more rounded ellipse.
@ -44,13 +44,13 @@ List<Vector2> calculateArc({
/// For more information read: https://en.wikipedia.org/wiki/Ellipse. /// For more information read: https://en.wikipedia.org/wiki/Ellipse.
List<Vector2> calculateEllipse({ List<Vector2> calculateEllipse({
required Vector2 center, required Vector2 center,
required double bigRadius, required double majorRadius,
required double smallRadius, required double minorRadius,
int precision = 100, int precision = 100,
}) { }) {
assert( assert(
0 < smallRadius && smallRadius <= bigRadius, 0 < minorRadius && minorRadius <= majorRadius,
'smallRadius ($smallRadius) and bigRadius ($bigRadius) must be in ' 'smallRadius ($minorRadius) and bigRadius ($majorRadius) must be in '
'range 0 < smallRadius <= bigRadius', 'range 0 < smallRadius <= bigRadius',
); );
@ -58,10 +58,10 @@ List<Vector2> calculateEllipse({
final points = <Vector2>[]; final points = <Vector2>[];
for (var i = 0; i < precision; i++) { for (var i = 0; i < precision; i++) {
final xCoord = center.x + smallRadius * math.cos(stepAngle * i); final x = center.x + minorRadius * math.cos(stepAngle * i);
final yCoord = center.y - bigRadius * math.sin(stepAngle * i); final y = center.y - majorRadius * math.sin(stepAngle * i);
final point = Vector2(xCoord, yCoord); final point = Vector2(x, y);
points.add(point); points.add(point);
} }
@ -98,17 +98,15 @@ List<Vector2> calculateBezierCurve({
final points = <Vector2>[]; final points = <Vector2>[];
do { do {
var xCoord = 0.0; var x = 0.0;
var yCoord = 0.0; var y = 0.0;
for (var i = 0; i <= n; i++) { for (var i = 0; i <= n; i++) {
final point = controlPoints[i]; final point = controlPoints[i];
xCoord += x += binomial(n, i) * math.pow(1 - t, n - i) * math.pow(t, i) * point.x;
binomial(n, i) * math.pow(1 - t, n - i) * math.pow(t, i) * point.x; y += binomial(n, i) * math.pow(1 - t, n - i) * math.pow(t, i) * point.y;
yCoord +=
binomial(n, i) * math.pow(1 - t, n - i) * math.pow(t, i) * point.y;
} }
points.add(Vector2(xCoord, yCoord)); points.add(Vector2(x, y));
t = t + step; t = t + step;
} while (t <= 1); } while (t <= 1);

@ -37,8 +37,8 @@ void main() {
test('returns by default 100 points as indicated by precision', () { test('returns by default 100 points as indicated by precision', () {
final points = calculateEllipse( final points = calculateEllipse(
center: Vector2.zero(), center: Vector2.zero(),
bigRadius: 100, majorRadius: 100,
smallRadius: 50, minorRadius: 50,
); );
expect(points.length, 100); expect(points.length, 100);
}); });
@ -46,8 +46,8 @@ void main() {
test('returns as many points as indicated by precision', () { test('returns as many points as indicated by precision', () {
final points = calculateEllipse( final points = calculateEllipse(
center: Vector2.zero(), center: Vector2.zero(),
bigRadius: 100, majorRadius: 100,
smallRadius: 50, minorRadius: 50,
precision: 50, precision: 50,
); );
expect(points.length, 50); expect(points.length, 50);
@ -57,16 +57,16 @@ void main() {
expect( expect(
() => calculateEllipse( () => calculateEllipse(
center: Vector2.zero(), center: Vector2.zero(),
bigRadius: 100, majorRadius: 100,
smallRadius: 150, minorRadius: 150,
), ),
throwsA(isA<AssertionError>()), throwsA(isA<AssertionError>()),
); );
expect( expect(
() => calculateEllipse( () => calculateEllipse(
center: Vector2.zero(), center: Vector2.zero(),
bigRadius: 100, majorRadius: 100,
smallRadius: 0, minorRadius: 0,
), ),
throwsA(isA<AssertionError>()), throwsA(isA<AssertionError>()),
); );

Loading…
Cancel
Save