Download:
child 1:06d076da4395
0:74b863d85894
Anton Shestakov <av6@dwimlabs.net>, Sat, 12 Mar 2016 22:32:57 +0800
coffee: add first iteration port

1 файлов изменено, 118 вставок(+), 0 удалений(-) [+]
sierpinski.coffee file | annotate | diff | comparison | revisions
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/sierpinski.coffee Sat Mar 12 22:32:57 2016 +0800
@@ -0,0 +1,118 @@
+class L
+ constructor: (@$el, @width, @height, @order) ->
+ @init()
+ @
+
+ init: ->
+ @pointer = 0
+ @rule = @a
+ @rows = []
+
+ fillrow = (len) ->
+ str = []
+ str.push ' ' for [1..len]
+ str.join ''
+
+ @rows.push fillrow @width for [1..@height]
+
+ for [1..@order]
+ @rule = @rule
+ .replace /a/g, 'x'
+ .replace /b/g, 'y'
+ .replace /x/g, @a
+ .replace /y/g, @b
+
+ return
+
+ putc: (x, y, c) ->
+ replacec = (str, pos, c) ->
+ str.substr(0, pos) + c + str.substr(pos + c.length)
+
+ if 0 <= x < @width and 0 <= y < @height
+ @rows[y] = replacec @rows[y], x, c
+
+ return
+
+ line: ->
+ switch @angle
+ when 0
+ @putc @x, @y, '__'
+ @x += 2
+
+ when 60
+ @putc @x, @y, '/'
+ @x++
+ @y--
+
+ when 120
+ @x--
+ @putc @x, @y, '\\'
+ @y--
+
+ when 180
+ @x -= 2
+ @putc @x, @y, '__'
+
+ when 240
+ @x--
+ @y++
+ @putc @x, @y, '/'
+
+ when 300
+ @y++
+ @putc @x, @y, '\\'
+ @x++
+
+ return
+
+ step: (visible) ->
+ if @pointer >= @rule.length
+ return false
+
+ redraw = false
+
+ switch @rule[@pointer]
+ when 'a', 'b'
+ @line()
+ redraw = true
+
+ when '-'
+ @angle = (@angle + 60 + 360) % 360
+
+ when '+'
+ @angle = (@angle - 60 + 360) % 360
+
+ @pointer++
+
+ if visible
+ if redraw
+ @draw()
+ else
+ @step visible
+
+ true
+
+ draw: ->
+ @$el.text @rows.join '\n'
+
+
+class window.Sierpinski extends L
+ a: 'b-a-b'
+ b: 'a+b+a'
+
+ init: ->
+ @x = @width - 2
+ @y = @height - 2
+ @angle = 180
+ super
+
+
+class window.Flowsnake extends L
+ a: 'a-b--b+a++aa+b-'
+ b: '+a-bb--b-a++a+b'
+
+ init: ->
+ @x = @width / 2
+ @y = @height
+ @angle = 0
+ super