1+ # frozen_string_literal: true
2+
13############################
24# Simple lsystem grammar
35############################
@@ -22,96 +24,3 @@ def generate(gen)
2224 prod
2325 end
2426end
25-
26- Turtle = Struct . new ( :x , :y , :angle , :color )
27-
28- #############################
29- # PenroseColored class
30- #############################
31- class PenroseColored
32- include Propane ::Proxy
33-
34- attr_reader :axiom , :grammar , :start_length , :theta , :production ,
35- :draw_length , :repeats , :xpos , :ypos
36-
37- DELTA = 36 # degrees
38- RED = 70 <<24 |200 <<16 |0 <<8 |0 # using bit operations to set color int
39- BLUE = 70 <<24 |0 <<16 |0 <<8 |200
40-
41- def initialize ( xpos , ypos ) # Note use of abbreviated grammar
42- @axiom = '[X]2+[X]2+[X]2+[X]2+[X]' # nos, used to indicate repeats
43- @grammar = Grammar . new (
44- axiom ,
45- 'F' => '' , # a so called deletion rule
46- 'W' => 'YBF2+ZRF4-XBF[-YBF4-WRF]2+' ,
47- 'X' => '+YBF2-ZRF[3-WRF2-XBF]+' ,
48- 'Y' => '-WRF2+XBF[3+YBF2+ZRF]-' ,
49- 'Z' => '2-YBF4+WRF[+ZRF4+XBF]2-XBF' )
50- @start_length = 1000.0
51- @theta = 0
52- @xpos = xpos
53- @ypos = ypos
54- @production = axiom . split ( '' )
55- @draw_length = start_length
56- end
57-
58- ##############################################################################
59- # Not strictly in the spirit of either processing in my render
60- # function I have ignored the processing translate/rotate functions in favour
61- # of the direct calculation of the new x and y positions, thus avoiding such
62- # affine transformations.
63- ##############################################################################
64-
65- def render
66- repeats = 1
67- ignored = %w( W X Y Z )
68- repeated = %w( 1 2 3 4 )
69- pen = Turtle . new ( xpos , ypos , theta , :R ) # simple Struct for pen, symbol :R = red
70- stack = [ ] # simple array for stack
71- production . scan ( /./ ) do |element |
72- case element
73- when 'F'
74- pen = draw_line ( pen , draw_length )
75- when '+'
76- pen . angle += DELTA * repeats
77- repeats = 1
78- when '-'
79- pen . angle -= DELTA * repeats
80- repeats = 1
81- when '['
82- stack << pen . dup # push a copy current pen to stack
83- when ']'
84- pen = stack . pop # assign current pen to instance off the stack
85- when 'R' , 'B'
86- pen . color = element . to_sym # set pen color as symbol
87- when *ignored
88- when *repeated
89- repeats = element . to_i
90- else puts format ( 'Character %s not in grammar' , element )
91- end
92- end
93- end
94- #####################################################
95- # create grammar from axiom and # rules (adjust scale)
96- #####################################################
97-
98- def create_grammar ( gen )
99- @draw_length *= 0.5 **gen
100- @production = grammar . generate gen
101- end
102-
103- private
104-
105- ####################################################################
106- # draws line using current pen position, color and length parameters
107- # returns a pen corresponding to the new position
108- ###################################################################
109-
110- def draw_line ( pen , length )
111- stroke ( pen . color == :R ? RED : BLUE )
112- new_xpos = pen . x - length * DegLut . cos ( pen . angle )
113- new_ypos = pen . y - length * DegLut . sin ( pen . angle )
114- line ( pen . x , pen . y , new_xpos , new_ypos ) # draw line
115- Turtle . new ( new_xpos , new_ypos , pen . angle , pen . color ) # return pen @ new pos
116- end
117- end
0 commit comments