Debian Codegraph @

texmf-dist/doc/latex/luatikz/example/sierpinski.lua

raw
1 tikz.within( '*' )
2
3 local s60 = math.sin( 60 / 180 * math.pi )
4 local c60 = math.cos( 60 / 180 * math.pi )
5
6 -- creates a table object having ptop, pleft and pright as points
7 -- and a "draw yourself" function
8 function equilateralTriangle( ptop, len )
9 return {
10 ptop = ptop,
11 pleft = ptop + p{ -len/2, -len*s60 },
12 pright = ptop + p{ len/2, -len*s60 },
13 draw =
14 function( self )
15 draw{
16 fill = black,
17 draw = none,
18 polyline{ self.ptop, self.pleft, self.pright, 'cycle' }
19 }
20 end
21 }
22 end
23
24 -- one step into the fractal
25 -- ptop: top point of triangle
26 -- len: current length
27 -- level: current fractal level
28 function drawFractal( ptop, len, level )
29 if level == 1
30 then
31 local t = equilateralTriangle( ptop, len )
32 t:draw( )
33 else
34 local ttop = equilateralTriangle( ptop, len / 2 )
35 drawFractal( ttop.ptop, len / 2, level - 1 )
36 drawFractal( ttop.pleft, len / 2, level - 1 )
37 drawFractal( ttop.pright, len / 2, level - 1 )
38 end
39 end
40
41 drawFractal( p{ 0, 0 }, 8, 6 )