|
1 | 1 | <?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 #%> |
4 | 4 | <?xml version='1.0' encoding='utf-8' standalone='no'?>
|
5 | 5 | <!DOCTYPE svg PUBLIC '-//W3C//DTD SVG 1.1//EN'
|
6 | 6 | 'http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd'>
|
7 | 7 | <svg width='100%' height='100%' version='1.1'
|
8 | 8 | xmlns='http://www.w3.org/2000/svg'>
|
9 |
| - |
10 | 9 | <%#----------
|
11 |
| - @scale = 6/10 |
| 10 | + @scale = 0.66 |
12 | 11 | @PI = 3.1415926535
|
| 12 | + @eps = 2.0 |
13 | 13 |
|
14 | 14 | 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); |
21 | 24 | end
|
22 | 25 | end
|
23 | 26 |
|
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) |
29 | 30 | #-----------%>
|
30 |
| - |
31 | 31 | </svg>
|
0 commit comments