RoundedPolygon.fromFeatures constructor

RoundedPolygon.fromFeatures(
  1. List<Feature> features, {
  2. double centerX = double.nan,
  3. double centerY = double.nan,
})

Takes a list of Feature objects that define the polygon's shape and curves. By specifying the features directly, the summarization of Cubic objects to curves can be precisely controlled. This affects Morph's default mapping, as curves with the same type (convex or concave) are mapped with each other. For example, if you have a convex curve in your start polygon, Morph will map it to another convex curve in the end polygon.

The centerX and centerY parameters are optional. If not supplied, they will be estimated by calculating the average of all cubic anchor points.

features are the Features that describe the characteristics of each outline segment of the polygon.

centerX is the X coordinate of the center of the polygon, around which all vertices will be placed. If none provided, the center will be averaged.

centerY is the Y coordinate of the center of the polygon, around which all vertices will be placed. If none provided, the center will be averaged.

Throws ArgumentError if features length is less than 2 or if they don't describe a closed shape.

Implementation

factory RoundedPolygon.fromFeatures(
  List<Feature> features, {
  double centerX = double.nan,
  double centerY = double.nan,
}) {
  if (features.length < 2) {
    throw ArgumentError('Polygons must have at least 2 features.');
  }

  if (centerX.isNaN || centerY.isNaN) {
    final vertices = <double>[];

    for (final feature in features) {
      for (final cubic in feature.cubics) {
        vertices
          ..add(cubic.anchor0X)
          ..add(cubic.anchor0Y);
      }
    }

    final center = calculateCenter(vertices);

    final cX = centerX.isNaN ? center.x : centerX;
    final cY = centerY.isNaN ? center.y : centerY;

    return RoundedPolygon._(features, Point(cX, cY));
  }

  return RoundedPolygon._(features, Point(centerX, centerY));
}