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