Skip to content

Commit 49838b9

Browse files
committed
colored the fractal tree
1 parent 8ca5f43 commit 49838b9

File tree

1 file changed

+16
-16
lines changed

1 file changed

+16
-16
lines changed
Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
11
<?rbi?>
2-
<%# Fractal tree example %>
3-
<%# % nqp rubyish.nqp rubyish-examples/fractal-tree.rbi > fractal.svg %>
2+
<%# Fractal tree example #%>
3+
<%# % nqp rubyish.nqp rubyish-examples/fractal-tree.rbi > fractal.svg #%>
44
<?xml version='1.0' encoding='utf-8' standalone='no'?>
55
<!DOCTYPE svg PUBLIC '-//W3C//DTD SVG 1.1//EN'
66
'http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd'>
77
<svg width='100%' height='100%' version='1.1'
88
xmlns='http://www.w3.org/2000/svg'>
9-
109
<%#----------
11-
@scale = 6/10
10+
@scale = 0.66
1211
@PI = 3.1415926535
12+
@eps = 2.0
1313
1414
def tree(x, y, len, angle)
15-
if len >= 1.0 then
16-
x2 = x + len * nqp::cos_n(angle)
17-
y2 = y + len * nqp::sin_n(angle)
18-
puts "<line x1='#{x}' y1='#{y}' x2='#{x2}' y2='#{y2}' style='stroke:rgb(0,0,0);stroke-width:1'/>";
19-
tree(x2, y2, len*@scale, angle + @PI/5);
20-
tree(x2, y2, len*@scale, angle - @PI/5);
15+
if len >= @eps then
16+
x2 = x + len * nqp::cos_n(angle)
17+
y2 = y + len * nqp::sin_n(angle)
18+
sw = len > 20? len / 10 : 2;
19+
g = len < @eps*2? 210: 20;
20+
puts " <line x1='#{x}' y1='#{y}' x2='#{x2}' y2='#{y2}' style='stroke:rgb(130,#{g},80);stroke-width:#{sw}'/>" \
21+
if x >= 0.0 and y >= 0.0
22+
tree(x2, y2, len*@scale, angle + 0.2*@PI);
23+
tree(x2, y2, len*@scale, angle - 0.2*@PI);
2124
end
2225
end
2326

24-
width = 1000
25-
height = 1000
26-
length = 400
27-
28-
tree(width/2, height, length, 3*@PI/2)
27+
width = 1000
28+
height = 850
29+
tree(x=width/2, y=height, length=300, angle=1.5*@PI)
2930
#-----------%>
30-
3131
</svg>

0 commit comments

Comments
 (0)