refactor: constructors with rotation instead of separated method

pull/87/head
RuiAlonso 4 years ago
parent 28804b8225
commit dcf1742fc9

@ -5,8 +5,8 @@ import 'package:vector_math/vector_math_64.dart';
/// Calculates all [Vector2]s of a circumference. /// Calculates all [Vector2]s of a circumference.
/// ///
/// A circumference can be achieved by specifying a [center] and a [radius]. /// 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 /// In addition, a semi-circle can be achieved by specifying its [angle] in
/// [offsetAngle] (both in radians). /// radians.
/// ///
/// 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 arc. /// achieving a more rounded arc.
@ -16,15 +16,14 @@ List<Vector2> calculateArc({
required Vector2 center, required Vector2 center,
required double radius, required double radius,
required double angle, required double angle,
double offsetAngle = 0,
int precision = 100, int precision = 100,
}) { }) {
final stepAngle = angle / (precision - 1); final stepAngle = angle / (precision - 1);
final points = <Vector2>[]; final points = <Vector2>[];
for (var i = 0; i < precision; i++) { for (var i = 0; i < precision; i++) {
final x = center.x + radius * math.cos((stepAngle * i) + offsetAngle); final x = center.x + radius * math.cos(stepAngle * i);
final y = center.y - radius * math.sin((stepAngle * i) + offsetAngle); final y = center.y - radius * math.sin(stepAngle * i);
final point = Vector2(x, y); final point = Vector2(x, y);
points.add(point); points.add(point);

@ -1,5 +1,6 @@
// ignore_for_file: public_member_api_docs // ignore_for_file: public_member_api_docs
import 'package:flame/extensions.dart';
import 'package:flame_forge2d/flame_forge2d.dart'; import 'package:flame_forge2d/flame_forge2d.dart';
import 'package:geometry/geometry.dart'; import 'package:geometry/geometry.dart';
@ -12,16 +13,17 @@ class ArcShape extends ChainShape {
required this.center, required this.center,
required this.arcRadius, required this.arcRadius,
required this.angle, required this.angle,
required this.rotation, this.rotation = 0,
}) { }) {
createChain( final points = calculateArc(
calculateArc( center: center,
center: center, radius: arcRadius,
radius: arcRadius, angle: angle,
angle: angle, ).map((vector) => vector..rotate(rotation)).toList();
offsetAngle: rotation, if (rotation != 0) {
), points.map((vector) => vector..rotate(rotation)).toList();
); }
createChain(points);
} }
/// The center of the arc. /// The center of the arc.

@ -11,10 +11,15 @@ class BezierCurveShape extends ChainShape {
/// {@macro bezier_curve_shape} /// {@macro bezier_curve_shape}
BezierCurveShape({ BezierCurveShape({
required this.controlPoints, required this.controlPoints,
this.rotation = 0,
}) { }) {
createChain( final points = calculateBezierCurve(controlPoints: controlPoints)
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. /// Specifies the control points of the curve.
@ -23,8 +28,6 @@ class BezierCurveShape extends ChainShape {
/// inner points between them set its final shape. /// inner points between them set its final shape.
final List<Vector2> controlPoints; final List<Vector2> controlPoints;
/// Rotates the curve by a given [angle] in radians. /// Which can be rotated by a given [rotation] in radians.
void rotate(double angle) { final double rotation;
vertices.map((vector) => vector..rotate(angle)).toList();
}
} }

@ -13,14 +13,17 @@ class EllipseShape extends ChainShape {
required this.center, required this.center,
required this.majorRadius, required this.majorRadius,
required this.minorRadius, required this.minorRadius,
this.rotation = 0,
}) { }) {
createChain( final points = calculateEllipse(
calculateEllipse( center: center,
center: center, majorRadius: majorRadius,
majorRadius: majorRadius, minorRadius: minorRadius,
minorRadius: minorRadius,
),
); );
if (rotation != 0) {
points.map((vector) => vector..rotate(rotation)).toList();
}
createChain(points);
} }
/// The top left corner of the ellipse. /// The top left corner of the ellipse.
@ -35,19 +38,19 @@ class EllipseShape extends ChainShape {
/// Minor radius is specified by [minorRadius]. /// Minor radius is specified by [minorRadius].
final double minorRadius; final double minorRadius;
/// Rotates the ellipse by a given [angle] in radians. /// Which can be rotated by a given [rotation] in radians.
void rotate(double angle) { final double rotation;
vertices.map((vector) => vector..rotate(angle)).toList();
}
EllipseShape copyWith({ EllipseShape copyWith({
Vector2? center, Vector2? center,
double? majorRadius, double? majorRadius,
double? minorRadius, double? minorRadius,
double? rotation,
}) => }) =>
EllipseShape( EllipseShape(
center: center ?? this.center, center: center ?? this.center,
majorRadius: majorRadius ?? this.majorRadius, majorRadius: majorRadius ?? this.majorRadius,
minorRadius: minorRadius ?? this.minorRadius, minorRadius: minorRadius ?? this.minorRadius,
rotation: rotation ?? this.rotation,
); );
} }

Loading…
Cancel
Save