Pythagoras tree: Difference between revisions

m
m (→‎Drawing in Flutter: mini refactoring)
m (→‎{{header|Dart}}: cosmetics)
 
(9 intermediate revisions by 3 users not shown)
Line 446:
 
void main() {
var basis = [(Point(x: -200.0, y: 0.0), Point(x: 200.0, y: 0.0))];
final groups = Iterable.generate(12, (lvl) {
final basis0 = basis;
Line 452:
final lvlPolygons = basis0.map((pp) {
final (a, b) = pp;
final v = Point((x: b.x - a).xy, y: b.y(a - ab).yx);
final [c, d, we] = [a, b, (a + b + v) * 0.5].map((p) => (x: p.x + v.y, y: p.y - v.x)).toList();
final e = (x: c.x + w.x / 2, y: c.y + w.y / 2);
basis.addAll([(c, e), (e, d)]);
return '<polygon points="${[a, c, e, d, c, d, b].expand((p) => [p.x, p.y]).join(' ')}"/>';
Line 470 ⟶ 469:
<syntaxhighlight lang="dart">import 'package:flutter/material.dart';
 
void main() => runApp(const MainAppFittedBox());
child: CustomPaint(painter: TreePainter(), size: const Size(2400, 1600))));
 
class MainApp extends StatelessWidget {
const MainApp({super.key});
 
@override
Widget build(BuildContext context) {
return FittedBox(
child: CustomPaint(
size: const Size(2360, 1580),
painter: TreePainter(),
),
);
}
}
 
class TreePainter extends CustomPainter {
@override
void paint(Canvas canvas, Size size) {
canvas.translate(size.width / 2, size.height);
canvas.drawColor(Colors.white, BlendMode.src);
final stroke = Paint()
..style = PaintingStyle.stroke
..strokeWidth = 0.9
..color = Colors.white;
final fill = Paint()..style = PaintingStyle.fill;
canvas.drawColor(Colors.white, BlendMode.src);
 
const halfBase = Offset(200, 0);
final fill = Paint()..style = PaintingStyle.fill;
var basis = [const (Offsetsize.bottomCenter(-200, 0halfBase), Offsetsize.bottomCenter(200, 0halfBase))];
for (var lvl = 0; lvl < 12; lvl++) {
final path = Path();
Line 503 ⟶ 489:
basis = [];
for (var (a, b) in basis0) {
final v = Offset((b.dx - a).dxdy, b.dy(a - ab).dydx);
final [c, d, we] = [a, b, (a + b + v) / 2].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);
basis.addAll([(c, e), (e, d)]);
path.addPolygon([a, c, e, d, c, d, b], true);
}
rg(int step) => (80 + (lvl - 2) * step) & 255;
canvas
fill.color = Color.fromARGB(255, rg(20), rg(30), 18);
canvas ..drawPath(path, fill..color = Color.fromARGB(255, rg(20), rg(30), 18));
canvas ..drawPath(path, stroke);
}
}
Line 1,054 ⟶ 1,039:
</html></syntaxhighlight>
==={{trans|Rust}}===
[[File:PythagorTree-scrinPythagorTreeJS.pngsvg|400px]]<br>
Run this script from the browser console (F12) or from the <script> tag of an html document.
<syntaxhighlight lang="javascript">let base = [[{ x: -200, y: 0 }, { x: 200, y: 0 }]];
Line 2,111 ⟶ 2,096:
=={{header|Rust}}==
Creates a '''[https://gist.githubusercontent.com/vvshard/833bd69acfa9160350cdbc9b57bbefe4/raw/pythagoras_tree.svg svg file]''' (12 levels) <br>
[[File:PythagorTree-scrinPythagoras_tree.pngsvg]]
 
'''[dependencies]'''<br>
Line 2,372 ⟶ 2,357:
Output image: [https://github.com/trizen/rc/blob/master/img/pythagoras-tree-sidef.png Pythagoras tree]
 
=={{header|uBasic/4tH}}==
{{trans|BASIC256}}
<syntaxhighlight lang="qbasic">Dim @o(5) ' 0 = SVG file, 1 = color, 2 = fillcolor, 3 = pixel, 4 = text
 
' === Begin Program ===
 
w = 800 : h = w * 11 / 16
v = w / 2 : d = w / 12
 
Proc _SVGopen ("pythtree.svg") ' open the SVG file
Proc _Canvas (w, h) ' set the canvas size
Proc _Background (FUNC(_Color ("White")))
' we have a white background
Proc _Pythagoras_tree (v - d, h - 10, v + d, h - 10, 0)
Proc _SVGclose
End
 
_Pythagoras_tree
Param (5)
Local (8)
 
If e@ > 10 Then Return
 
f@ = c@ - a@ : g@ = b@ - d@
h@ = c@ - g@ : i@ = d@ - f@
j@ = a@ - g@ : k@ = b@ - f@
l@ = j@ + (f@ - g@) / 2
m@ = k@ - (f@ + g@) / 2
 
Proc _SetColor (FUNC(_RGBtoColor (0, e@*25, 0)))
' draw the box
Proc _Line (b@, a@, d@, c@) : Proc _Line (d@, c@, i@, h@)
Proc _Line (i@, h@, k@, j@) : Proc _Line (k@, j@, b@, a@)
 
Proc _Pythagoras_tree (j@, k@, l@, m@, e@ +1)
Proc _Pythagoras_tree (l@, m@, h@, i@, e@ +1)
Return
 
' === End Program ===
 
_RGBtoColor Param (3) : Return (a@ * 65536 + b@ * 256 + c@)
_SetColor Param (1) : @o(1) = a@ : Return
_SVGclose Write @o(0), "</svg>" : Close @o(0) : Return
_color_ Param (1) : Proc _PrintRGB (a@) : Write @o(0), "\q />" : Return
 
_PrintRGB ' print an RBG color in hex
Param (1)
Radix 16
 
If a@ < 0 Then
Write @o(0), "none";
Else
Write @o(0), Show(Str ("#!######", a@));
EndIf
 
Radix 10
Return
 
_Background ' set the background color
Param (1)
 
Write @o(0), "<rect width=\q100%\q height=\q100%\q fill=\q";
Proc _color_ (a@)
Return
 
_Color ' retrieve color code from its name
Param (1)
Local (1)
Radix 16
 
if Comp(a@, "black") = 0 Then
b@ = 000000
else if Comp(a@, "blue") = 0 Then
b@ = 0000ff
else if Comp(a@, "green") = 0 Then
b@ = 00ff00
else if Comp(a@, "cyan") = 0 Then
b@ = 00ffff
else if Comp(a@, "red") = 0 Then
b@ = 0ff0000
else if Comp(a@, "magenta") = 0 Then
b@ = 0ff00ff
else if Comp(a@, "yellow") = 0 Then
b@ = 0ffff00
else if Comp(a@, "white") = 0 Then
b@ = 0ffffff
else if Comp(a@, "none") = 0 Then
b@ = Info ("nil")
else Print "Invalid color" : Raise 1
fi : fi : fi : fi : fi : fi : fi : fi : fi
 
Radix 10
Return (b@)
 
_Line ' draw an SVG line from x1,y1 to x2,y2
Param (4)
 
Write @o(0), "<line x1=\q";d@;"\q y1=\q";c@;
Write @o(0), "\q x2=\q";b@;"\q y2=\q";a@;"\q stroke=\q";
Proc _color_ (@o(1))
Return
 
_Canvas ' set up a canvas x wide and y high
Param (2)
 
Write @o(0), "<svg width=\q";a@;"\q height=\q";b@;"\q viewBox=\q0 0 ";a@;" ";b@;
Write @o(0), "\q xmlns=\qhttp://www.w3.org/2000/svg\q ";
Write @o(0), "xmlns:xlink=\qhttp://www.w3.org/1999/xlink\q>"
Return
 
_SVGopen ' open an SVG file by name
Param (1)
 
If Set (@o(0), Open (a@, "w")) < 0 Then
Print "Cannot open \q";Show (a@);"\q" : Raise 1
Else
Write @o(0), "<?xml version=\q1.0\q encoding=\qUTF-8\q standalone=\qno\q?>"
Write @o(0), "<!DOCTYPE svg PUBLIC \q-//W3C//DTD SVG 1.1//EN\q ";
Write @o(0), "\qhttp://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\q>"
EndIf
Return</syntaxhighlight>
=={{header|Wren}}==
{{trans|Kotlin}}
{{libheader|DOME}}
{{libheader|Wren-polygon}}
<syntaxhighlight lang="ecmascriptwren">import "graphics" for Canvas, Color
import "dome" for Window
import "./polygon" for Polygon
106

edits