From a4f387edbb22cb86e20e8e7cd7af93397970092c Mon Sep 17 00:00:00 2001 From: RuiAlonso Date: Mon, 28 Mar 2022 10:27:34 +0200 Subject: [PATCH] refactor: moved rotate to separate method --- .../lib/src/components/shapes/arc_shape.dart | 25 ++++++++----------- .../components/shapes/bezier_curve_shape.dart | 15 ++++------- .../src/components/shapes/ellipse_shape.dart | 23 ++++++++--------- 3 files changed, 26 insertions(+), 37 deletions(-) diff --git a/packages/pinball_components/lib/src/components/shapes/arc_shape.dart b/packages/pinball_components/lib/src/components/shapes/arc_shape.dart index b2042c4d..3cd11cb0 100644 --- a/packages/pinball_components/lib/src/components/shapes/arc_shape.dart +++ b/packages/pinball_components/lib/src/components/shapes/arc_shape.dart @@ -13,17 +13,14 @@ class ArcShape extends ChainShape { required this.center, required this.arcRadius, required this.angle, - this.rotation = 0, }) { - final points = calculateArc( - center: center, - radius: arcRadius, - angle: angle, - ).map((vector) => vector..rotate(rotation)).toList(); - if (rotation != 0) { - points.map((vector) => vector..rotate(rotation)).toList(); - } - createChain(points); + createChain( + calculateArc( + center: center, + radius: arcRadius, + angle: angle, + ), + ); } /// The center of the arc. @@ -38,19 +35,19 @@ class ArcShape extends ChainShape { /// For example, two pi returns a complete circumference. final double angle; - /// Angle in radians to rotate the arc around its [center]. - final double rotation; + /// Rotates the arc by a given [angle] in radians. + void rotate(double angle) { + vertices.map((vector) => vector..rotate(angle)).toList(); + } ArcShape copyWith({ Vector2? center, double? arcRadius, double? angle, - double? rotation, }) => ArcShape( center: center ?? this.center, arcRadius: arcRadius ?? this.arcRadius, angle: angle ?? this.angle, - rotation: rotation ?? this.rotation, ); } diff --git a/packages/pinball_components/lib/src/components/shapes/bezier_curve_shape.dart b/packages/pinball_components/lib/src/components/shapes/bezier_curve_shape.dart index 37ede3c6..0b4e8fef 100644 --- a/packages/pinball_components/lib/src/components/shapes/bezier_curve_shape.dart +++ b/packages/pinball_components/lib/src/components/shapes/bezier_curve_shape.dart @@ -11,15 +11,8 @@ class BezierCurveShape extends ChainShape { /// {@macro bezier_curve_shape} BezierCurveShape({ required this.controlPoints, - this.rotation = 0, }) { - final points = calculateBezierCurve(controlPoints: controlPoints) - .map((vector) => vector..rotate(rotation)) - .toList(); - if (rotation != 0) { - points.map((vector) => vector..rotate(rotation)).toList(); - } - createChain(points); + createChain(calculateBezierCurve(controlPoints: controlPoints)); } /// Specifies the control points of the curve. @@ -28,6 +21,8 @@ class BezierCurveShape extends ChainShape { /// inner points between them set its final shape. final List controlPoints; - /// Which can be rotated by a given [rotation] in radians. - final double rotation; + /// Rotates the bezier curve by a given [angle] in radians. + void rotate(double angle) { + vertices.map((vector) => vector..rotate(angle)).toList(); + } } diff --git a/packages/pinball_components/lib/src/components/shapes/ellipse_shape.dart b/packages/pinball_components/lib/src/components/shapes/ellipse_shape.dart index 663b4bde..bfa7d435 100644 --- a/packages/pinball_components/lib/src/components/shapes/ellipse_shape.dart +++ b/packages/pinball_components/lib/src/components/shapes/ellipse_shape.dart @@ -13,17 +13,14 @@ class EllipseShape extends ChainShape { required this.center, required this.majorRadius, required this.minorRadius, - this.rotation = 0, }) { - final points = calculateEllipse( - center: center, - majorRadius: majorRadius, - minorRadius: minorRadius, + createChain( + calculateEllipse( + center: center, + majorRadius: majorRadius, + minorRadius: minorRadius, + ), ); - if (rotation != 0) { - points.map((vector) => vector..rotate(rotation)).toList(); - } - createChain(points); } /// The top left corner of the ellipse. @@ -38,19 +35,19 @@ class EllipseShape extends ChainShape { /// Minor radius is specified by [minorRadius]. final double minorRadius; - /// Which can be rotated by a given [rotation] in radians. - final double rotation; + /// Rotates the ellipse by a given [angle] in radians. + void rotate(double angle) { + vertices.map((vector) => vector..rotate(angle)).toList(); + } EllipseShape copyWith({ Vector2? center, double? majorRadius, double? minorRadius, - double? rotation, }) => EllipseShape( center: center ?? this.center, majorRadius: majorRadius ?? this.majorRadius, minorRadius: minorRadius ?? this.minorRadius, - rotation: rotation ?? this.rotation, ); }