I want to create a superellipse shape in Flutter for a widget.
I found an article about creating superellipses written in python and java, but i can't quite get the code to work.
class SuperEllipse extends ShapeBorder {
final BorderSide side;
final double n;
SuperEllipse({
@required this.n,
this.side = BorderSide.none,
}) : assert(side != null);
@override
EdgeInsetsGeometry get dimensions => EdgeInsets.all(side.width);
@override
ShapeBorder scale(double t) {
return SuperEllipse(
side: side.scale(t),
n: n,
);
}
@override
Path getInnerPath(Rect rect, {TextDirection textDirection}) {
return _superEllipsePath(rect, n);
}
@override
Path getOuterPath(Rect rect, {TextDirection textDirection}) {
return _superEllipsePath(rect, n);
}
static Path _superEllipsePath(Rect rect, double n) {
final int a = 200;
List<double> points = [a + 1.0];
Path path = new Path();
path.moveTo(a.toDouble(), 0);
// Calculate first quadrant.
for (int x = a; x >= 0; x--) {
points[x] = pow(pow(a, n) - pow(x, n), 1 / n);
path.lineTo(x.toDouble(), -points[x]);
}
// Mirror to other quadrants.
for (int x = 0; x <= a; x++) {
path.lineTo(x.toDouble(), points[x]);
}
for (int x = a; x >= 0; x--) {
path.lineTo(-x.toDouble(), points[x]);
}
for (int x = 0; x <= a; x++) {
path.lineTo(-x.toDouble(), -points[x]);
}
return path;
}
@override
void paint(Canvas canvas, Rect rect, {TextDirection textDirection}) {
Path path = getOuterPath(rect.deflate(side.width / 2.0), textDirection: textDirection);
canvas.drawPath(path, side.toPaint());
}
}
I want to return the correct shape, but instead I get an Exception: Invalid value: Only valid value is 0: 200.
For some reason the variable a
isn't allowed to be 200? I don't know why, and changing it to 0 doesn't produce any errors, but then there is no shape either.
Does anyone know if there is a better way of doing this?