@@ -28,6 +28,8 @@ def settings
2828 end
2929end
3030
31+ SimpleTurtle = Struct . new ( :x , :y , :angle )
32+
3133# class SnakeKolam
3234class SnakeKolam
3335 include Propane ::Proxy
@@ -55,15 +57,15 @@ def setup_grammar
5557 end
5658
5759 def render # NB not using affine transforms here
58- turtle = [ xpos , ypos , 0 ]
59- production . each do |element |
60+ turtle = SimpleTurtle . new ( xpos , ypos , 0 )
61+ production . scan ( /./ ) do |element |
6062 case element
6163 when 'F'
6264 turtle = draw_line ( turtle , draw_length )
6365 when '+'
64- turtle [ ANGLE ] += DELTA
66+ turtle . angle += DELTA
6567 when '-'
66- turtle [ ANGLE ] -= DELTA
68+ turtle . angle -= DELTA
6769 when 'X'
6870 else
6971 puts "Character '#{ element } ' is not in grammar"
@@ -90,10 +92,12 @@ def create_grammar(gen)
9092 ######################################################
9193
9294 def draw_line ( turtle , length )
93- new_xpos = turtle [ XPOS ] + length * DegLut . cos ( turtle [ ANGLE ] )
94- new_ypos = turtle [ YPOS ] + length * DegLut . sin ( turtle [ ANGLE ] )
95- line ( turtle [ XPOS ] , turtle [ YPOS ] , new_xpos , new_ypos )
96- [ new_xpos , new_ypos , turtle [ ANGLE ] ]
95+ x_temp = turtle . x
96+ y_temp = turtle . y
97+ turtle . x += draw_length * DegLut . cos ( turtle . angle )
98+ turtle . y += draw_length * DegLut . sin ( turtle . angle )
99+ line ( x_temp , y_temp , turtle . x , turtle . y )
100+ turtle
97101 end
98102end
99103
0 commit comments