Pythagoras tree: Difference between revisions

→‎{{header|Dart}}: added Drawing in Flutter
m (→‎Rust: shortened the link)
(→‎{{header|Dart}}: added Drawing in Flutter)
Line 439:
 
=={{header|Dart}}==
===Creating an SVG file===
{{trans|Rust}}
Dart version: ^3.0.0
Line 463 ⟶ 464:
final attrs = 'viewBox="$x $y ${-x - x} ${-y}" stroke="white" xmlns="http://www.w3.org/2000/svg"';
File('Pythagor_tree.svg').writeAsString('<svg $attrs>\n$groups\n</svg>');
}
</syntaxhighlight>
===Drawing in Flutter===
[https://dartpad.dev/?id=c5a5f23a36c2707b7d57e2fd1359ebd3 View output / play with the code - online in DartPad]
<syntaxhighlight lang="dart">import 'package:flutter/material.dart';
 
void main() => runApp(const MainApp());
 
class MainApp extends StatelessWidget {
const MainApp({super.key});
 
@override
Widget build(BuildContext context) {
return FittedBox(
child: Container(
color: Colors.white,
width: 2360,
height: 1580,
child: CustomPaint(painter: TreePainter()),
),
);
}
}
 
class TreePainter extends CustomPainter {
@override
void paint(Canvas canvas, Size size) {
canvas.translate(size.width / 2, size.height);
final stroke = Paint()
..style = PaintingStyle.stroke
..strokeWidth = 0.9
..color = Colors.white;
 
final fill = Paint()..style = PaintingStyle.fill;
var base = [const (Offset(-200, 0), Offset(200, 0))];
for (var lvl = 0; lvl < 12; lvl++) {
final path = Path();
final base0 = base;
base = [];
for (var (a, b) in base0) {
final v = Offset(b.dx - a.dx, b.dy - a.dy);
final [c, d, w] = [a, b, v].map((p) => Offset(p.dx + v.dy, p.dy - v.dx)).toList();
final e = Offset(c.dx + w.dx / 2, c.dy + w.dy / 2);
base.addAll([(c, e), (e, d)]);
path.addPolygon([a, c, e, d, c, d, b], true);
}
rg(int step) => (80 + (lvl - 2) * step) & 255;
fill.color = Color.fromARGB(255, rg(20), rg(30), 18);
canvas.drawPath(path, fill);
canvas.drawPath(path, stroke);
}
}
 
@override
bool shouldRepaint(CustomPainter oldDelegate) => true;
}
</syntaxhighlight>
106

edits