From dcf1742fc9b81730c39a4a8da82b0d1ee864ef20 Mon Sep 17 00:00:00 2001 From: RuiAlonso Date: Mon, 28 Mar 2022 10:15:22 +0200 Subject: [PATCH] refactor: constructors with rotation instead of separated method --- packages/geometry/lib/src/geometry.dart | 9 ++++---- .../lib/src/components/shapes/arc_shape.dart | 20 ++++++++-------- .../components/shapes/bezier_curve_shape.dart | 17 ++++++++------ .../src/components/shapes/ellipse_shape.dart | 23 +++++++++++-------- 4 files changed, 38 insertions(+), 31 deletions(-) diff --git a/packages/geometry/lib/src/geometry.dart b/packages/geometry/lib/src/geometry.dart index 6975f8cb..8eea9e90 100644 --- a/packages/geometry/lib/src/geometry.dart +++ b/packages/geometry/lib/src/geometry.dart @@ -5,8 +5,8 @@ import 'package:vector_math/vector_math_64.dart'; /// Calculates all [Vector2]s of a circumference. /// /// A circumference can be achieved by specifying a [center] and a [radius]. -/// In addition, a semi-circle can be achieved by specifying its [angle] and an -/// [offsetAngle] (both in radians). +/// In addition, a semi-circle can be achieved by specifying its [angle] in +/// radians. /// /// The higher the [precision], the more [Vector2]s will be calculated; /// achieving a more rounded arc. @@ -16,15 +16,14 @@ List calculateArc({ required Vector2 center, required double radius, required double angle, - double offsetAngle = 0, int precision = 100, }) { final stepAngle = angle / (precision - 1); final points = []; for (var i = 0; i < precision; i++) { - final x = center.x + radius * math.cos((stepAngle * i) + offsetAngle); - final y = center.y - radius * math.sin((stepAngle * i) + offsetAngle); + final x = center.x + radius * math.cos(stepAngle * i); + final y = center.y - radius * math.sin(stepAngle * i); final point = Vector2(x, y); points.add(point); 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 ef392e34..3e3e6287 100644 --- a/packages/pinball_components/lib/src/components/shapes/arc_shape.dart +++ b/packages/pinball_components/lib/src/components/shapes/arc_shape.dart @@ -1,5 +1,6 @@ // ignore_for_file: public_member_api_docs +import 'package:flame/extensions.dart'; import 'package:flame_forge2d/flame_forge2d.dart'; import 'package:geometry/geometry.dart'; @@ -12,16 +13,17 @@ class ArcShape extends ChainShape { required this.center, required this.arcRadius, required this.angle, - required this.rotation, + this.rotation = 0, }) { - createChain( - calculateArc( - center: center, - radius: arcRadius, - angle: angle, - offsetAngle: rotation, - ), - ); + 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); } /// The center of the arc. 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 d1806aea..37ede3c6 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,10 +11,15 @@ class BezierCurveShape extends ChainShape { /// {@macro bezier_curve_shape} BezierCurveShape({ required this.controlPoints, + this.rotation = 0, }) { - createChain( - calculateBezierCurve(controlPoints: controlPoints), - ); + final points = calculateBezierCurve(controlPoints: controlPoints) + .map((vector) => vector..rotate(rotation)) + .toList(); + if (rotation != 0) { + points.map((vector) => vector..rotate(rotation)).toList(); + } + createChain(points); } /// Specifies the control points of the curve. @@ -23,8 +28,6 @@ class BezierCurveShape extends ChainShape { /// inner points between them set its final shape. final List controlPoints; - /// Rotates the curve by a given [angle] in radians. - void rotate(double angle) { - vertices.map((vector) => vector..rotate(angle)).toList(); - } + /// Which can be rotated by a given [rotation] in radians. + final double rotation; } 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 bfa7d435..663b4bde 100644 --- a/packages/pinball_components/lib/src/components/shapes/ellipse_shape.dart +++ b/packages/pinball_components/lib/src/components/shapes/ellipse_shape.dart @@ -13,14 +13,17 @@ class EllipseShape extends ChainShape { required this.center, required this.majorRadius, required this.minorRadius, + this.rotation = 0, }) { - createChain( - calculateEllipse( - center: center, - majorRadius: majorRadius, - minorRadius: minorRadius, - ), + final points = 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. @@ -35,19 +38,19 @@ class EllipseShape extends ChainShape { /// Minor radius is specified by [minorRadius]. final double minorRadius; - /// Rotates the ellipse by a given [angle] in radians. - void rotate(double angle) { - vertices.map((vector) => vector..rotate(angle)).toList(); - } + /// Which can be rotated by a given [rotation] in radians. + final double rotation; 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, ); }