-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpentaflake.rb
More file actions
84 lines (71 loc) · 1.73 KB
/
Copy pathpentaflake.rb
File metadata and controls
84 lines (71 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# frozen_string_literal: true
require 'picrate'
THETA = Math::PI * 2 / 5
SCALE_FACTOR = (3 - Math.sqrt(5)) / 2
MARGIN = 40
class PentagonSketch < Processing::App
attr_reader :center, :pentagons, :radius
def settings
size(800, 800)
end
def setup
sketch_title 'Pentagon'
radius = width / 2 - 2 * MARGIN
center = Vec2D.new(radius - 2 * MARGIN, radius - 6 * MARGIN)
pentaflake = Pentaflake.new(center, radius, 4)
@pentagons = pentaflake.pentagons
end
def draw
background(255)
stroke(0)
pentagons.each do |penta|
draw_pentagon(penta)
end
no_loop
end
def draw_pentagon(pent)
points = pent.vertices
begin_shape
points.each do |pnt|
pnt.to_vertex(renderer)
end
end_shape(CLOSE)
end
def renderer
@renderer ||= GfxRender.new(g)
end
end
PentagonSketch.new
class Pentaflake
attr_reader :pentagons
def initialize(center, radius, depth)
@pentagons = []
create_pentagons(center, radius, depth)
end
def create_pentagons(center, radius, depth)
if depth.zero?
pentagons << Pentagon.new(center, radius)
else
radius *= SCALE_FACTOR
distance = radius * Math.sin(THETA) * 2
(0..4).each do |idx|
x = center.x + Math.cos(idx * THETA) * radius * Math.sin(THETA) * 2
y = center.y + Math.sin(idx * THETA) * radius * Math.sin(THETA) * 2
center = Vec2D.new(x, y)
create_pentagons(center, radius, depth - 1)
end
end
end
end
class Pentagon
attr_reader :center, :radius
def initialize(center, radius)
@center = center
@radius = radius
end
def vertices
(0..4).map do |idx|
center + Vec2D.new(radius * Math.sin(THETA * idx), radius * Math.cos(THETA * idx))
end
end
end