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.
///
/// 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<Vector2> calculateArc({
required Vector2 center,
required double radius,
required double angle,
double offsetAngle = 0,
int precision = 100,
}) {
final stepAngle = angle / (precision - 1);
final points = <Vector2>[];
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);

@ -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.

@ -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<Vector2> 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;
}

@ -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,
);
}

Loading…
Cancel
Save