From 7c01edceaab846a0f981b53515492221f6e088c0 Mon Sep 17 00:00:00 2001 From: RuiAlonso Date: Wed, 23 Mar 2022 22:20:54 +0100 Subject: [PATCH] refactor: renaming params --- lib/game/components/pathway.dart | 8 ++--- packages/geometry/lib/src/geometry.dart | 36 +++++++++---------- packages/geometry/test/src/geometry_test.dart | 16 ++++----- 3 files changed, 29 insertions(+), 31 deletions(-) diff --git a/lib/game/components/pathway.dart b/lib/game/components/pathway.dart index 0fb67f72..0ca6828a 100644 --- a/lib/game/components/pathway.dart +++ b/lib/game/components/pathway.dart @@ -164,16 +164,16 @@ class Pathway extends BodyComponent with InitialPosition, Layered { // TODO(ruialonso): Refactor repetitive logic final outerWall = calculateEllipse( center: center, - bigRadius: bigRadius, - smallRadius: smallRadius, + majorRadius: bigRadius, + minorRadius: smallRadius, ).map((vector) => vector..rotate(rotation)).toList(); paths.add(outerWall); if (!singleWall) { final innerWall = calculateEllipse( center: center, - bigRadius: bigRadius - width, - smallRadius: smallRadius - width, + majorRadius: bigRadius - width, + minorRadius: smallRadius - width, ).map((vector) => vector..rotate(rotation)).toList(); paths.add(innerWall); } diff --git a/packages/geometry/lib/src/geometry.dart b/packages/geometry/lib/src/geometry.dart index 1b54a4c8..6975f8cb 100644 --- a/packages/geometry/lib/src/geometry.dart +++ b/packages/geometry/lib/src/geometry.dart @@ -23,10 +23,10 @@ List calculateArc({ final points = []; for (var i = 0; i < precision; i++) { - final xCoord = center.x + radius * math.cos((stepAngle * i) + offsetAngle); - final yCoord = center.y - radius * math.sin((stepAngle * i) + offsetAngle); + final x = center.x + radius * math.cos((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); } @@ -35,8 +35,8 @@ List calculateArc({ /// Calculates all [Vector2]s of an ellipse. /// -/// An ellipse can be achieved by specifying a [center], a [bigRadius] and a -/// [smallRadius]. +/// An ellipse can be achieved by specifying a [center], a [majorRadius] and a +/// [minorRadius]. /// /// The higher the [precision], the more [Vector2]s will be calculated; /// achieving a more rounded ellipse. @@ -44,13 +44,13 @@ List calculateArc({ /// For more information read: https://en.wikipedia.org/wiki/Ellipse. List calculateEllipse({ required Vector2 center, - required double bigRadius, - required double smallRadius, + required double majorRadius, + required double minorRadius, int precision = 100, }) { assert( - 0 < smallRadius && smallRadius <= bigRadius, - 'smallRadius ($smallRadius) and bigRadius ($bigRadius) must be in ' + 0 < minorRadius && minorRadius <= majorRadius, + 'smallRadius ($minorRadius) and bigRadius ($majorRadius) must be in ' 'range 0 < smallRadius <= bigRadius', ); @@ -58,10 +58,10 @@ List calculateEllipse({ final points = []; for (var i = 0; i < precision; i++) { - final xCoord = center.x + smallRadius * math.cos(stepAngle * i); - final yCoord = center.y - bigRadius * math.sin(stepAngle * i); + final x = center.x + minorRadius * math.cos(stepAngle * i); + final y = center.y - majorRadius * math.sin(stepAngle * i); - final point = Vector2(xCoord, yCoord); + final point = Vector2(x, y); points.add(point); } @@ -98,17 +98,15 @@ List calculateBezierCurve({ final points = []; do { - var xCoord = 0.0; - var yCoord = 0.0; + var x = 0.0; + var y = 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; + 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; } - points.add(Vector2(xCoord, yCoord)); + points.add(Vector2(x, y)); t = t + step; } while (t <= 1); diff --git a/packages/geometry/test/src/geometry_test.dart b/packages/geometry/test/src/geometry_test.dart index 9db1a833..7a49b2b2 100644 --- a/packages/geometry/test/src/geometry_test.dart +++ b/packages/geometry/test/src/geometry_test.dart @@ -37,8 +37,8 @@ void main() { test('returns by default 100 points as indicated by precision', () { final points = calculateEllipse( center: Vector2.zero(), - bigRadius: 100, - smallRadius: 50, + majorRadius: 100, + minorRadius: 50, ); expect(points.length, 100); }); @@ -46,8 +46,8 @@ void main() { test('returns as many points as indicated by precision', () { final points = calculateEllipse( center: Vector2.zero(), - bigRadius: 100, - smallRadius: 50, + majorRadius: 100, + minorRadius: 50, precision: 50, ); expect(points.length, 50); @@ -57,16 +57,16 @@ void main() { expect( () => calculateEllipse( center: Vector2.zero(), - bigRadius: 100, - smallRadius: 150, + majorRadius: 100, + minorRadius: 150, ), throwsA(isA()), ); expect( () => calculateEllipse( center: Vector2.zero(), - bigRadius: 100, - smallRadius: 0, + majorRadius: 100, + minorRadius: 0, ), throwsA(isA()), );