1
- require 'picrate' # After an openprocessing sketch by C.Andrews
2
- class RecursivePentagons < Processing :: App
3
- attr_reader :strut_factor , :renderer
1
+ # frozen_string_literal: true
2
+
3
+ require 'picrate'
4
4
5
+ # After an openprocessing sketch by C.Andrews
6
+ class RecursivePentagons < Processing ::App
7
+ load_library :color_group
8
+ attr_reader :strut_factor , :renderer , :cols
9
+ PALETTE = %w[ #fff0a5 #ffd500 #594a00 #9999ff #000059 ] . freeze
5
10
def setup
6
11
sketch_title 'Recursive Pentagons'
7
12
@strut_factor = 0.2
8
13
@renderer = AppRender . new self # so we can send Vec2D :to_vertex
9
- background ( 255 )
14
+ group = ColorGroup . from_web_array ( PALETTE . to_java ( :string ) )
15
+ @cols = group . colors
10
16
no_loop
11
17
end
12
18
13
19
def draw
20
+ background 0
14
21
translate ( width / 2 , height / 2 )
15
22
angle = TWO_PI / 5
16
23
radius = width / 2
@@ -28,13 +35,13 @@ def settings
28
35
end
29
36
end
30
37
31
- RecursivePentagon . new
38
+ RecursivePentagons . new
32
39
33
40
# Here we include Processing::Proxy to mimic vanilla processing inner class
34
41
# access.
35
42
class Pentagon
36
43
include Processing ::Proxy
37
- attr_reader :points , :branches , :level , :midpoints , :innerpoints
44
+ attr_reader :points , :branches , :level , :midpoints , :innerpoints
38
45
39
46
def initialize ( points , levels )
40
47
@points = points
@@ -66,25 +73,27 @@ def initialize(points, levels)
66
73
# add the final innermost pentagon
67
74
branches << Pentagon . new ( innerpoints , level - 1 )
68
75
end
76
+
69
77
# This is a simple helper function that takes in two points (as Vec2D) and
70
78
# returns the midpoint between them as Vec2D.
71
79
def midpoint ( point1 , point2 )
72
80
( point2 + point1 ) * 0.5
73
81
end
82
+
74
83
# This draws the fractal. If this is on level 0, we just draw the
75
84
# pentagon formed by the points. When not level 0, iterate through the
76
85
# six branches and tell them to draw themselves.
77
86
def draw
78
- if level . zero?
79
- no_fill
80
- begin_shape
81
- points . each do |point |
82
- point . to_vertex ( renderer )
83
- end
84
- points [ 0 ] . to_vertex ( renderer )
85
- end_shape
86
- else
87
- branches . each ( &:draw )
87
+ no_fill
88
+ begin_shape
89
+ stroke_weight level
90
+ stroke cols [ level ]
91
+ points . each do |point |
92
+ point . to_vertex ( renderer )
88
93
end
94
+ end_shape CLOSE
95
+ return if level . zero?
96
+
97
+ branches . each ( &:draw )
89
98
end
90
99
end
0 commit comments