Skip to content

Commit 60f1977

Browse files
committed
solid text sketch
1 parent 5ead02e commit 60f1977

File tree

5 files changed

+140
-0
lines changed

5 files changed

+140
-0
lines changed

contributed/pentaflake.rb

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# frozen_string_literal: true
2+
3+
THETA = Math::PI * 2 / 5
4+
SCALE_FACTOR = (3 - Math.sqrt(5)) / 2
5+
MARGIN = 20
6+
7+
attr_reader :pentagons, :renderer
8+
def settings
9+
size(400, 400)
10+
end
11+
12+
def setup
13+
sketch_title 'Pentaflake'
14+
radius = width / 2 - 2 * MARGIN
15+
center = Vec2D.new(radius - 2 * MARGIN, 3 * MARGIN)
16+
pentaflake = Pentaflake.new(center, radius, 5)
17+
@pentagons = pentaflake.pentagons
18+
@renderer = GfxRender.new(g)
19+
end
20+
21+
def draw
22+
background(255)
23+
stroke(0)
24+
pentagons.each { |penta| draw_pentagon(penta) }
25+
no_loop
26+
end
27+
28+
def draw_pentagon(pent)
29+
points = pent.vertices
30+
begin_shape
31+
points.each { |pnt| pnt.to_vertex(renderer) }
32+
end_shape(CLOSE)
33+
end
34+
35+
class Pentagon
36+
attr_reader :center, :radius
37+
38+
def initialize(center, radius)
39+
@center = center
40+
@radius = radius
41+
end
42+
43+
def vertices
44+
(0..4).map do |idx|
45+
center + Vec2D.new(radius * Math.sin(THETA * idx), radius * Math.cos(THETA * idx))
46+
end
47+
end
48+
end
49+
50+
class Pentaflake
51+
attr_reader :pentagons
52+
53+
def initialize(center, radius, depth)
54+
@pentagons = []
55+
create_pentagons(center, radius, depth)
56+
end
57+
58+
def create_pentagons(center, radius, depth)
59+
if depth.zero?
60+
pentagons << Pentagon.new(center, radius)
61+
else
62+
radius *= SCALE_FACTOR
63+
distance = radius * Math.sin(THETA) * 2
64+
(0..4).each do |idx|
65+
x = center.x + Math.cos(idx * THETA) * distance
66+
y = center.y + Math.sin(idx * THETA) * distance
67+
center = Vec2D.new(x, y)
68+
create_pentagons(center, radius, depth - 1)
69+
end
70+
end
71+
end
72+
end
73+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
require 'geomerative'
2+
3+
attr_reader :em
4+
5+
def settings
6+
size(600, 400, P3D)
7+
smooth
8+
end
9+
10+
def setup
11+
sketch_title 'Solid Text'
12+
RG.init(self)
13+
grp = RG.get_text('Depth!', data_path('FreeSans.ttf'), 50, CENTER)
14+
RG.polygonizer = RCommand::UNIFORMLENGTH
15+
RG.polygonizer_length = 1
16+
@em = RExtrudedMesh.new(grp)
17+
end
18+
19+
def draw
20+
background(100)
21+
lights
22+
translate(width / 2, height / 2, 200)
23+
rotate_y(millis / 2000.0)
24+
fill(255, 100, 0)
25+
no_stroke
26+
em.draw
27+
end
28+
29+
# Custom extrusion class, including Proxy to access App methods
30+
class RExtrudedMesh
31+
include Processing::Proxy
32+
attr_reader :mesh, :points_array, :depth
33+
34+
def initialize(grp, dpth = 10)
35+
@depth = dpth
36+
@mesh = grp.to_mesh
37+
@points_array = grp.points_in_paths
38+
end
39+
40+
def draw_face(strips, depth)
41+
strips.each do |strip|
42+
begin_shape(TRIANGLE_STRIP)
43+
strip.vertices.each do |point|
44+
vertex(point.x, point.y, depth / 2.0)
45+
end
46+
end_shape(CLOSE)
47+
end
48+
end
49+
50+
def draw_sides(points_array, depth)
51+
points_array.each do |points|
52+
begin_shape(QUAD_STRIP)
53+
points.each do |point|
54+
vertex(point.x, point.y, depth / 2.0)
55+
vertex(point.x, point.y, -depth / 2.0)
56+
end
57+
end_shape(CLOSE)
58+
end
59+
end
60+
61+
def draw
62+
strips = mesh.strips
63+
draw_face(strips, depth)
64+
draw_face(strips, depth * -1)
65+
draw_sides(points_array, depth)
66+
end
67+
end
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)