RoundedPolygon.fromVertices constructor
RoundedPolygon.fromVertices(
- List<
Offset> vertices, { - CornerRounding rounding = CornerRounding.unrounded,
- List<
CornerRounding> ? perVertexRounding, - Offset? center,
Creates a polygon with the given vertices.
The list must be ordered: the outline runs from each vertex to the next and closes from the last back to the first. Any other order gives undefined results.
rounding rounds every vertex the same way. perVertexRounding
overrides it, and must have the same length as vertices when it is not
null. The default leaves the corners sharp and the edges straight.
center defaults to the average of vertices.
Throws ArgumentError if vertices has fewer than 3 elements, or if
perVertexRounding has the wrong number of elements.
Implementation
factory RoundedPolygon.fromVertices(
List<Offset> vertices, {
CornerRounding rounding = CornerRounding.unrounded,
List<CornerRounding>? perVertexRounding,
Offset? center,
}) {
if (vertices.length < 3) {
throw ArgumentError('Polygons must have at least 3 vertices.');
}
if (perVertexRounding != null && perVertexRounding.length != vertices.length) {
throw ArgumentError(
'perVertexRounding list should be either null or '
'the same size as the number of vertices.',
);
}
final corners = <List<CubicBezier>>[];
final int n = vertices.length;
final roundedCorners = <_RoundedCorner>[];
for (var i = 0; i < n; i++) {
final CornerRounding vtxRounding = perVertexRounding?[i] ?? rounding;
final int prevIndex = (i + n - 1) % n;
final int nextIndex = (i + 1) % n;
roundedCorners.add(
_RoundedCorner(vertices[prevIndex], vertices[i], vertices[nextIndex], vtxRounding),
);
}
// For each side, check if we have enough space to do the cuts needed, and
// if not split the available space, first for round cuts, then for
// smoothing if there is space left. Each element in this list is a pair,
// that represent how much we can do of the cut for the given side (side i
// goes from corner i to corner i+1), the elements of the pair are: first
// is how much we can use of expectedRoundCut, second how much of
// expectedCut.
final List<(double, double)> cutAdjusts = List.generate(n, (ix) {
final double expectedRoundCut =
roundedCorners[ix].expectedRoundCut + roundedCorners[(ix + 1) % n].expectedRoundCut;
final double expectedCut =
roundedCorners[ix].expectedCut + roundedCorners[(ix + 1) % n].expectedCut;
final Point vtx = vertices[ix];
final Point nextVtx = vertices[(ix + 1) % n];
final double sideSize = (vtx - nextVtx).distance;
// Check expectedRoundCut first, and ensure we fulfill rounding needs
// first for both corners before using space for smoothing.
if (expectedRoundCut > sideSize) {
// Not enough room for fully rounding, see how much we can actually do.
return (sideSize / expectedRoundCut, 0.0);
} else if (expectedCut > sideSize) {
// We can do full rounding, but not full smoothing.
return (1.0, (sideSize - expectedRoundCut) / (expectedCut - expectedRoundCut));
} else {
// There is enough room for rounding & smoothing.
return (1.0, 1.0);
}
});
// Create and store list of beziers for each [potentially] rounded corner.
for (var i = 0; i < n; i++) {
// allowedCuts[0] is for the side from the previous corner to this one,
// allowedCuts[1] is for the side from this corner to the next one.
final allowedCuts = List<double>.filled(2, 0);
for (var delta = 0; delta <= 1; delta++) {
final (double roundCutRatio, double cutRatio) = cutAdjusts[(i + n - 1 + delta) % n];
allowedCuts[delta] =
roundedCorners[i].expectedRoundCut * roundCutRatio +
(roundedCorners[i].expectedCut - roundedCorners[i].expectedRoundCut) * cutRatio;
}
corners.add(roundedCorners[i].getCubics(allowedCuts[0], allowedCuts[1]));
}
// Finally, store the calculated cubics. This includes all of the rounded
// corners from above, along with new cubics representing the edges between
// those corners.
final tempFeatures = <PolygonFeature>[];
for (var i = 0; i < n; i++) {
final Point currVertex = vertices[i];
final Point prevVertex = vertices[(i + n - 1) % n];
final Point nextVertex = vertices[(i + 1) % n];
final bool cvx = convex(prevVertex, currVertex, nextVertex);
tempFeatures
..add(CornerFeature(corners[i], convex: cvx))
..add(
EdgeFeature([
CubicBezier.straightLine(corners[i].last.anchor1, corners[(i + 1) % n].first.anchor0),
]),
);
}
return RoundedPolygonInternals.fromFeatures(
tempFeatures,
center: center ?? calculateCenter(vertices),
);
}