Pythagoras tree: Difference between revisions

Content added Content deleted
m (→‎Drawing in Flutter: mini refactoring)
(→‎Drawing in Flutter: refactoring)
Line 476: Line 476:


@override
@override
Widget build(BuildContext context) {
Widget build(BuildContext context) => FittedBox(
child: CustomPaint(painter: TreePainter(), size: const Size(2400, 1600)));
return FittedBox(
child: CustomPaint(
size: const Size(2360, 1580),
painter: TreePainter(),
),
);
}
}
}


Line 489: Line 483:
@override
@override
void paint(Canvas canvas, Size size) {
void paint(Canvas canvas, Size size) {
canvas.translate(size.width / 2, size.height);
canvas.drawColor(Colors.white, BlendMode.src);
final stroke = Paint()
final stroke = Paint()
..style = PaintingStyle.stroke
..style = PaintingStyle.stroke
..strokeWidth = 0.9
..strokeWidth = 0.9
..color = Colors.white;
..color = Colors.white;

final fill = Paint()..style = PaintingStyle.fill;
final fill = Paint()..style = PaintingStyle.fill;
canvas.drawColor(Colors.white, BlendMode.src);
var basis = [const (Offset(-200, 0), Offset(200, 0))];

const halfBase = Offset(200, 0);
var basis = [(size.bottomCenter(-halfBase), size.bottomCenter(halfBase))];
for (var lvl = 0; lvl < 12; lvl++) {
for (var lvl = 0; lvl < 12; lvl++) {
final path = Path();
final path = Path();
Line 503: Line 497:
basis = [];
basis = [];
for (var (a, b) in basis0) {
for (var (a, b) in basis0) {
final v = Offset(b.dx - a.dx, b.dy - a.dy);
final v = Offset((b - a).dy, (a - b).dx);
final [c, d, w] = [a, b, v].map((p) => Offset(p.dx + v.dy, p.dy - v.dx)).toList();
final (c, d) = (a + v, b + v);
final e = Offset(c.dx + w.dx / 2, c.dy + w.dy / 2);
final e = (c + d + v) / 2;
basis.addAll([(c, e), (e, d)]);
basis.addAll([(c, e), (e, d)]);
path.addPolygon([a, c, e, d, c, d, b], true);
path.addPolygon([a, c, e, d, c, d, b], true);
}
}
rg(int step) => (80 + (lvl - 2) * step) & 255;
rg(int step) => (80 + (lvl - 2) * step) & 255;
canvas
fill.color = Color.fromARGB(255, rg(20), rg(30), 18);
canvas.drawPath(path, fill);
..drawPath(path, fill..color = Color.fromARGB(255, rg(20), rg(30), 18))
canvas.drawPath(path, stroke);
..drawPath(path, stroke);
}
}
}
}