Skip to content

Commit d9a7f3a

Browse files
committed
solid text sketch
1 parent 3180ea1 commit d9a7f3a

File tree

2 files changed

+160
-0
lines changed

2 files changed

+160
-0
lines changed

contributed/pentaflake.rb

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

0 commit comments

Comments
 (0)