feat: create ellipses from geometry

pull/84/head
RuiAlonso 4 years ago
parent 92404fbdba
commit 3b79292f8f

@ -144,6 +144,47 @@ class Pathway extends BodyComponent with InitialPosition, Layered {
); );
} }
/// Creates an ellipse [Pathway].
///
/// Does so with two [ChainShape] separated by a [width]. Can
/// be rotated by a given [rotation] in radians.
///
/// If [singleWall] is true, just one [ChainShape] is created.
factory Pathway.ellipse({
Color? color,
required Vector2 center,
required double width,
required double bigRadius,
required double smallRadius,
required double angle,
double rotation = 0,
bool singleWall = false,
}) {
final paths = <List<Vector2>>[];
// TODO(ruialonso): Refactor repetitive logic
final outerWall = calculateEllipse(
center: center,
bigRadius: bigRadius,
smallRadius: smallRadius,
).map((vector) => vector..rotate(rotation)).toList();
paths.add(outerWall);
if (!singleWall) {
final innerWall = calculateEllipse(
center: center,
bigRadius: bigRadius - width,
smallRadius: smallRadius - width,
).map((vector) => vector..rotate(rotation)).toList();
paths.add(innerWall);
}
return Pathway._(
color: color,
paths: paths,
);
}
final List<List<Vector2>> _paths; final List<List<Vector2>> _paths;
/// Constructs different [ChainShape]s to form the [Pathway] shape. /// Constructs different [ChainShape]s to form the [Pathway] shape.

@ -1,5 +1,5 @@
// ignore_for_file: public_member_api_docs // ignore_for_file: public_member_api_docs
import 'dart:math' as math;
import 'dart:async'; import 'dart:async';
import 'package:flame/extensions.dart'; import 'package:flame/extensions.dart';
import 'package:flame/input.dart'; import 'package:flame/input.dart';

@ -33,6 +33,43 @@ List<Vector2> calculateArc({
return points; return points;
} }
/// Calculates all [Vector2]s of an ellipse.
///
/// An ellipse can be achieved by specifying a [center], a [bigRadius] and a
/// [smallRadius].
/// In addition, a semi-ellipse can be achieved by specifying its [angle] and an
/// [offsetAngle] (both in radians).
///
/// The higher the [precision], the more [Vector2]s will be calculated;
/// achieving a more rounded ellipse.
///
/// For more information read: https://en.wikipedia.org/wiki/Ellipse.
List<Vector2> calculateEllipse({
required Vector2 center,
required double bigRadius,
required double smallRadius,
int precision = 100,
}) {
assert(
0 < smallRadius && smallRadius <= bigRadius,
'smallRadius ($smallRadius) and bigRadius ($bigRadius) must be in '
'range 0 < smallRadius <= bigRadius',
);
final stepAngle = 2 * math.pi / (precision - 1);
final points = <Vector2>[];
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 point = Vector2(xCoord, yCoord);
points.add(point);
}
return points;
}
/// Calculates all [Vector2]s of a bezier curve. /// Calculates all [Vector2]s of a bezier curve.
/// ///
/// A bezier curve of [controlPoints] that say how to create this curve. /// A bezier curve of [controlPoints] that say how to create this curve.

Loading…
Cancel
Save