Skip to content

Commit 25cb105

Browse files
committed
colors
1 parent f887408 commit 25cb105

File tree

1 file changed

+25
-16
lines changed

1 file changed

+25
-16
lines changed

demo/recursive_pentagon.rb

+25-16
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,23 @@
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'
44

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
510
def setup
611
sketch_title 'Recursive Pentagons'
712
@strut_factor = 0.2
813
@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
1016
no_loop
1117
end
1218

1319
def draw
20+
background 0
1421
translate(width / 2, height / 2)
1522
angle = TWO_PI / 5
1623
radius = width / 2
@@ -28,13 +35,13 @@ def settings
2835
end
2936
end
3037

31-
RecursivePentagon.new
38+
RecursivePentagons.new
3239

3340
# Here we include Processing::Proxy to mimic vanilla processing inner class
3441
# access.
3542
class Pentagon
3643
include Processing::Proxy
37-
attr_reader :points ,:branches, :level, :midpoints, :innerpoints
44+
attr_reader :points, :branches, :level, :midpoints, :innerpoints
3845

3946
def initialize(points, levels)
4047
@points = points
@@ -66,25 +73,27 @@ def initialize(points, levels)
6673
# add the final innermost pentagon
6774
branches << Pentagon.new(innerpoints, level - 1)
6875
end
76+
6977
# This is a simple helper function that takes in two points (as Vec2D) and
7078
# returns the midpoint between them as Vec2D.
7179
def midpoint(point1, point2)
7280
(point2 + point1) * 0.5
7381
end
82+
7483
# This draws the fractal. If this is on level 0, we just draw the
7584
# pentagon formed by the points. When not level 0, iterate through the
7685
# six branches and tell them to draw themselves.
7786
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)
8893
end
94+
end_shape CLOSE
95+
return if level.zero?
96+
97+
branches.each(&:draw)
8998
end
9099
end

0 commit comments

Comments
 (0)