Pythagoras tree: Difference between revisions

m
m (→‎{{trans|Rust}}: mini refactoring)
m (→‎{{header|Dart}}: cosmetics)
 
(25 intermediate revisions by 5 users not shown)
Line 206:
End
</syntaxhighlight>
 
==={{header|IS-BASIC}}===
<syntaxhighlight lang="is-basic">100 PROGRAM "Pythagor.bas"
110 OPTION ANGLE DEGREES
120 LET SQ2=SQR(2)
130 SET VIDEO MODE 1:SET VIDEO COLOUR 0:SET VIDEO X 42:SET VIDEO Y 25
140 OPEN #101:"video:"
150 SET PALETTE 0,141
160 DISPLAY #101:AT 1 FROM 1 TO 25
170 PLOT 580,20;ANGLE 90;
180 CALL BROCCOLI(225,10)
190 DO
200 LOOP WHILE INKEY$=""
210 TEXT
220 DEF BROCCOLI(X,Y)
230 IF X<Y THEN EXIT DEF
240 CALL SQUARE(X)
250 PLOT FORWARD X,LEFT 45,
260 CALL BROCCOLI(X/SQ2,Y)
270 PLOT RIGHT 90,FORWARD X/SQ2,
280 CALL BROCCOLI(X/SQ2,Y)
290 PLOT BACK X/SQ2,LEFT 45,BACK X,
300 END DEF
310 DEF SQUARE(X)
320 FOR I=1 TO 4
330 PLOT FORWARD X;RIGHT 90;
340 NEXT
350 END DEF</syntaxhighlight>
 
=={{header|C}}==
Line 409 ⟶ 437:
return 0;
}</syntaxhighlight>
 
=={{header|Dart}}==
===Creating an SVG file===
{{trans|Rust}}
Dart version: ^3.0.0
<syntaxhighlight lang="dart">import 'dart:math';
import 'dart:io';
 
void main() {
var basis = [(Point(-200.0, 0.0), Point(200.0, 0.0))];
final groups = Iterable.generate(12, (lvl) {
final basis0 = basis;
basis = [];
final lvlPolygons = basis0.map((pp) {
final (a, b) = pp;
final v = Point((b - a).y, (a - b).x);
final [c, d, e] = [a, b, (a + b + v) * 0.5].map((p) => p + v).toList();
basis.addAll([(c, e), (e, d)]);
return '<polygon points="${[a, c, e, d, c, d, b].expand((p) => [p.x, p.y]).join(' ')}"/>';
}).join('\n');
rg(int step) => ((80 + (lvl - 2) * step) & 255).toRadixString(16).padLeft(2, '0');
return '<g fill="#${rg(20)}${rg(30)}18">\n$lvlPolygons\n</g>';
}).join('\n');
final (x, y) = basis.fold((0.0, 0.0), (p, pp) => (min(p.$1, pp.$1.x), min(p.$2, pp.$1.y)));
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(FittedBox(
child: CustomPaint(painter: TreePainter(), size: const Size(2400, 1600))));
 
class TreePainter extends CustomPainter {
@override
void paint(Canvas canvas, Size size) {
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);
var basis = [(size.bottomCenter(-halfBase), size.bottomCenter(halfBase))];
for (var lvl = 0; lvl < 12; lvl++) {
final path = Path();
final basis0 = basis;
basis = [];
for (var (a, b) in basis0) {
final v = Offset((b - a).dy, (a - b).dx);
final [c, d, e] = [a, b, (a + b + v) / 2].map((p) => p + v).toList();
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
..drawPath(path, fill..color = Color.fromARGB(255, rg(20), rg(30), 18))
..drawPath(path, stroke);
}
}
 
@override
bool shouldRepaint(CustomPainter oldDelegate) => true;
}
</syntaxhighlight>
 
=={{header|EasyLang}}==
 
[https://easylang.dev/show/#cod=ZZDLCoMwFET3fsUs24qp8QEW2i8pXal9gDQiLpK/79zEgKGBBO7Mua/Mi+mxLuMIq+E0bAVXYRjn9Q0FlQH4PLf4ik5insHiJmjBrCg5SixQMH+TbB2onOYmOZGcl2ykGqF0QjWh1p5qhWoolarFCQeOIBnHmCK+S/1i5/dmMktNU67v71c6Q3cU4hKzmdzLfHFPfoN7cG42Z/3HP7lzOSUHiUz41p0ReubQCeCtUCUBVKYyTzQaukR7kbfMfg== Run it]
[https://easylang.online/apps/_pythagoras-tree.html Run it]
 
<syntaxhighlight lang="text">
Line 428 ⟶ 524:
polygon [ x1 y1 x2 y2 x3 y3 x4 y4 ]
polygon [ x3 y3 x4 y4 x5 y5 ]
call tree x4 y4 x5 y5 depth + 1
call tree x5 y5 x3 y3 depth + 1
.
.
call tree 41 10 59 10 0
</syntaxhighlight>
 
Line 728 ⟶ 824:
where mkLine = Data2D [Style Lines, Color Black,Title ""] []
close lst = lst ++ [head lst]</syntaxhighlight>
 
=={{header|IS-BASIC}}==
<syntaxhighlight lang="is-basic">100 PROGRAM "Pythagor.bas"
110 OPTION ANGLE DEGREES
120 LET SQ2=SQR(2)
130 SET VIDEO MODE 1:SET VIDEO COLOUR 0:SET VIDEO X 42:SET VIDEO Y 25
140 OPEN #101:"video:"
150 SET PALETTE 0,141
160 DISPLAY #101:AT 1 FROM 1 TO 25
170 PLOT 580,20;ANGLE 90;
180 CALL BROCCOLI(225,10)
190 DO
200 LOOP WHILE INKEY$=""
210 TEXT
220 DEF BROCCOLI(X,Y)
230 IF X<Y THEN EXIT DEF
240 CALL SQUARE(X)
250 PLOT FORWARD X,LEFT 45,
260 CALL BROCCOLI(X/SQ2,Y)
270 PLOT RIGHT 90,FORWARD X/SQ2,
280 CALL BROCCOLI(X/SQ2,Y)
290 PLOT BACK X/SQ2,LEFT 45,BACK X,
300 END DEF
310 DEF SQUARE(X)
320 FOR I=1 TO 4
330 PLOT FORWARD X;RIGHT 90;
340 NEXT
350 END DEF</syntaxhighlight>
 
=={{header|J}}==
Line 971 ⟶ 1,039:
</html></syntaxhighlight>
==={{trans|Rust}}===
[[File:PythagorTreeJS.svg|400px]]<br>
<syntaxhighlight lang="javascript">let base = [[[-200, 0], [200, 0]]];
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 }]];
const doc = [...Array(12)].reduce((doc_a, _, lvl) => {
const rg = step => `0${(80 + (lvl - 2) * step).toString(16)}`.slice(-2);
return doc_a + base.splice(0).reduce((ga, [a, b]) => {
const vw = [(kx, ky) => (kx * (b[0].x - a[0],.x) + ky * (b[1].y - a[1]].y)) / 2;
const [c, de, wd] = [a2, b3, v2].map(p(j, i) => [p[0]({ x: a.x + v[1]w(i, p[1]j), y: a.y + w(-j, v[0]]i) }));
const e = [c[0] + w[0] / 2, c[1] + w[1] / 2];
base.push([c, e], [e, d]);
return ga + '\n' + `<polygon points="${[a, c, e, d, c, d, b].map(p => [p.x, p.y])}"/>\n`;
}, `<g fill="#${rg(20)}${rg(30)}18">\n`) + '\n</g>\n';
}, '<svg xmlns="http://www.w3.org/2000/svg" width="1200" stroke="white">\n') + '</svg>';
 
const [{ x, y] } = base.flat().reduce((a, p) => a({ x: Math.mapmin((xya.x, ip.x), =>y: Math.min(xya.y, p[i].y) }));
const svg = doc.replace('<svg ', `<svg viewBox="${[x, y, -x - x, -y]}" `);
document.documentElement.innerHTML = svg, ''; // display svg in the browser window</syntaxhighlight>
 
if (globalThis.global) { // if the script is run from node.js - save the svg to a file
require('node:fs').writeFileSync('Pythagor_tree.svg', svg);
} else { // if is run from the browser console or from the <script> tag of an html document
// - display svg in the browser window
document.documentElement.innerHTML = svg, '';
}</syntaxhighlight>
 
=={{header|jq}}==
Line 2,032 ⟶ 2,095:
</syntaxhighlight>
=={{header|Rust}}==
Creates a '''[https://staticgist.mirahezegithubusercontent.orgcom/rosettacodewikivvshard/1833bd69acfa9160350cdbc9b57bbefe4/18raw/Pythagoras_treepythagoras_tree.svg '''svg file]'''] (12 levels): <br>
[[File:Pythagoras treePythagoras_tree.svg]]
 
'''[dependencies]'''<br>
Line 2,294 ⟶ 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