ExpressiveRoundedPolygon.fromFeatures constructor

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

Takes a list of ExpressiveFeature objects that define the polygon's shape and curves. By specifying the features directly, the summarization of ExpressiveShapeCubic objects to curves can be precisely controlled. This affects ExpressivePolygonMorph'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, ExpressivePolygonMorph 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 ExpressiveFeatures 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 ExpressiveRoundedPolygon.fromFeatures(
  List<ExpressiveFeature> 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 ExpressiveRoundedPolygon._(features, ExpressivePoint(cX, cY));
  }

  return ExpressiveRoundedPolygon._(
    features,
    ExpressivePoint(centerX, centerY),
  );
}