Pythagoras tree: Difference between revisions

m
imported>Thebeez
(Added uBasic/4tH version)
m (→‎{{header|Dart}}: cosmetics)
 
(5 intermediate revisions by 3 users not shown)
Line 453:
final (a, b) = pp;
final v = Point((b - a).y, (a - b).x);
final ([c, d), e] = [a, b, (a + b + v,) b* 0.5].map((p) => p + v).toList();
final e = (c + d + v) * 0.5;
basis.addAll([(c, e), (e, d)]);
return '<polygon points="${[a, c, e, d, c, d, b].expand((p) => [p.x, p.y]).join(' ')}"/>';
Line 491 ⟶ 490:
for (var (a, b) in basis0) {
final v = Offset((b - a).dy, (a - b).dx);
final ([c, d), e] = [a, b, (a + b + v,) b/ 2].map((p) => p + v).toList();
final e = (c + d + v) / 2;
basis.addAll([(c, e), (e, d)]);
path.addPolygon([a, c, e, d, c, d, b], true);
Line 1,041 ⟶ 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,098 ⟶ 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,358 ⟶ 2,356:
img.write(file => 'pythagoras_tree.png')</syntaxhighlight>
Output image: [https://github.com/trizen/rc/blob/master/img/pythagoras-tree-sidef.png Pythagoras tree]
 
=={{header|Wren}}==
{{trans|Kotlin}}
{{libheader|DOME}}
{{libheader|Wren-polygon}}
<syntaxhighlight lang="ecmascript">import "graphics" for Canvas, Color
import "dome" for Window
import "./polygon" for Polygon
 
var DepthLimit = 7
var Hue = 0.15
 
class PythagorasTree {
construct new(width, height) {
Window.title = "Pythagoras Tree"
Window.resize(width, height)
Canvas.resize(width, height)
}
 
init() {
Canvas.cls(Color.white)
drawTree(275, 500, 375, 500, 0)
}
 
drawTree(x1, y1, x2, y2, depth) {
if (depth == DepthLimit) return
var dx = x2 - x1
var dy = y1 - y2
 
var x3 = x2 - dy
var y3 = y2 - dx
var x4 = x1 - dy
var y4 = y1 - dx
var x5 = x4 + 0.5 * (dx - dy)
var y5 = y4 - 0.5 * (dx + dy)
 
// draw a square
var col = Color.hsv((Hue + depth * 0.02) * 360, 1, 1)
var square = Polygon.quick([[x1, y1], [x2, y2], [x3, y3], [x4, y4]])
square.drawfill(col)
square.draw(Color.lightgray)
 
// draw a triangle
col = Color.hsv((Hue + depth * 0.035) * 360, 1, 1)
var triangle = Polygon.quick([[x3, y3], [x4, y4], [x5, y5]])
triangle.drawfill(col)
triangle.draw(Color.lightgray)
 
drawTree(x4, y4, x5, y5, depth + 1)
drawTree(x5, y5, x3, y3, depth + 1)
}
 
update() {}
 
draw(alpha) {}
}
 
var Game = PythagorasTree.new(640, 640)</syntaxhighlight>
 
=={{header|XPL0}}==
<syntaxhighlight lang "XPL0">proc DrawTree(X1, Y1, X2, Y2, Depth);
int X1, Y1, X2, Y2, Depth;
int X3, Y3, X4, Y4, X5, Y5, DX, DY, Color;
[if Depth < 7 then
[DX:= X2 - X1; DY:= Y1 - Y2;
X3:= X2 - DY; Y3:= Y2 - DX;
X4:= X1 - DY; Y4:= Y1 - DX;
X5:= X4 + (DX-DY)/2; Y5:= Y4 - (DX+DY)/2;
Color:= $2A + Depth;
Move(X1, Y1);
Line(X2, Y2, Color); Line(X3, Y3, Color);
Line(X4, Y4, Color); Line(X1, Y1, Color);
DrawTree(X4, Y4, X5, Y5, Depth+1);
DrawTree(X5, Y5, X3, Y3, Depth+1);
];
];
 
def ScrW=320, ScrH=200;
[SetVid($13);
DrawTree(9*ScrW/20, 3*ScrH/4, 11*ScrW/20, 3*ScrH/4, 0);
]</syntaxhighlight>
{{out}}
[[File:PythtreeXPL0.gif]]
 
=={{header|uBasic/4tH}}==
Line 2,563 ⟶ 2,478:
EndIf
Return</syntaxhighlight>
=={{header|Wren}}==
{{trans|Kotlin}}
{{libheader|DOME}}
{{libheader|Wren-polygon}}
<syntaxhighlight lang="wren">import "graphics" for Canvas, Color
import "dome" for Window
import "./polygon" for Polygon
 
var DepthLimit = 7
var Hue = 0.15
 
class PythagorasTree {
construct new(width, height) {
Window.title = "Pythagoras Tree"
Window.resize(width, height)
Canvas.resize(width, height)
}
 
init() {
Canvas.cls(Color.white)
drawTree(275, 500, 375, 500, 0)
}
 
drawTree(x1, y1, x2, y2, depth) {
if (depth == DepthLimit) return
var dx = x2 - x1
var dy = y1 - y2
 
var x3 = x2 - dy
var y3 = y2 - dx
var x4 = x1 - dy
var y4 = y1 - dx
var x5 = x4 + 0.5 * (dx - dy)
var y5 = y4 - 0.5 * (dx + dy)
 
// draw a square
var col = Color.hsv((Hue + depth * 0.02) * 360, 1, 1)
var square = Polygon.quick([[x1, y1], [x2, y2], [x3, y3], [x4, y4]])
square.drawfill(col)
square.draw(Color.lightgray)
 
// draw a triangle
col = Color.hsv((Hue + depth * 0.035) * 360, 1, 1)
var triangle = Polygon.quick([[x3, y3], [x4, y4], [x5, y5]])
triangle.drawfill(col)
triangle.draw(Color.lightgray)
 
drawTree(x4, y4, x5, y5, depth + 1)
drawTree(x5, y5, x3, y3, depth + 1)
}
 
update() {}
 
draw(alpha) {}
}
 
var Game = PythagorasTree.new(640, 640)</syntaxhighlight>
 
=={{header|XPL0}}==
<syntaxhighlight lang "XPL0">proc DrawTree(X1, Y1, X2, Y2, Depth);
int X1, Y1, X2, Y2, Depth;
int X3, Y3, X4, Y4, X5, Y5, DX, DY, Color;
[if Depth < 7 then
[DX:= X2 - X1; DY:= Y1 - Y2;
X3:= X2 - DY; Y3:= Y2 - DX;
X4:= X1 - DY; Y4:= Y1 - DX;
X5:= X4 + (DX-DY)/2; Y5:= Y4 - (DX+DY)/2;
Color:= $2A + Depth;
Move(X1, Y1);
Line(X2, Y2, Color); Line(X3, Y3, Color);
Line(X4, Y4, Color); Line(X1, Y1, Color);
DrawTree(X4, Y4, X5, Y5, Depth+1);
DrawTree(X5, Y5, X3, Y3, Depth+1);
];
];
 
def ScrW=320, ScrH=200;
[SetVid($13);
DrawTree(9*ScrW/20, 3*ScrH/4, 11*ScrW/20, 3*ScrH/4, 0);
]</syntaxhighlight>
{{out}}
[[File:PythtreeXPL0.gif]]
 
=={{header|Yabasic}}==
106

edits