Skip to content

Commit 3a507a2

Browse files
committed
More like it
1 parent 6259818 commit 3a507a2

File tree

2 files changed

+160
-90
lines changed

2 files changed

+160
-90
lines changed
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
---
2+
layout: post
3+
title: "Using Vec3D"
4+
date: 2019-03-01 10:60:08 +0000
5+
categories: vecmath update
6+
---
7+
8+
# Example Usage
9+
10+
```ruby
11+
GEM_HOME = '/home/tux/.gem/ruby/2.6.0'
12+
require 'vecmath'
13+
14+
color_mode(RGB, 1)
15+
16+
def draw
17+
background(0)
18+
@renderer ||= AppRender.new(graphics)
19+
# Move the origin so that the scene is centered on the screen.
20+
translate(width / 2, height / 2, 0.0)
21+
# Set up the lighting.
22+
setup_lights
23+
# Rotate the local coordinate system.
24+
smooth_rotation(5.0, 6.7, 7.3)
25+
# Draw the inner object.
26+
no_stroke
27+
fill(smooth_colour(10.0, 12.0, 7.0))
28+
draw_icosahedron(5, 60.0, false)
29+
# Rotate the local coordinate system again.
30+
smooth_rotation(4.5, 3.7, 7.3)
31+
# Draw the outer object.
32+
stroke(0.2)
33+
fill(smooth_colour(6.0, 9.2, 0.7))
34+
draw_icosahedron(5, 200.0, true)
35+
end
36+
37+
def setup_lights
38+
ambient_light(0.025, 0.025, 0.025)
39+
directional_light(0.2, 0.2, 0.2, -1, -1, -1)
40+
spot_light(1.0, 1.0, 1.0, -200, 0, 300, 1, 0, -1, PI / 4, 20)
41+
end
42+
43+
# Generate a vector whose components change smoothly over time in the range
44+
# (0..1). Each component uses a Math.sin function to map the current time in
45+
# milliseconds in the range (0..1).A 'speed' factor is specified for each
46+
# component.
47+
def smooth_vector(s1, s2, s3)
48+
mills = millis * 0.00003 ## Lazydogs factor
49+
# mills = millis * 0.0000001 ## worked for me a bit slower!!
50+
x = 0.5 * sin(mills * s1) + 0.5
51+
y = 0.5 * sin(mills * s2) + 0.5
52+
z = 0.5 * sin(mills * s3) + 0.5
53+
Vec3D.new(x, y, z)
54+
end
55+
56+
# Generate a colour which smoothly changes over time.
57+
# The speed of each component is controlled by the parameters s1, s2 and s3.
58+
def smooth_colour(s1, s2, s3)
59+
v = smooth_vector(s1, s2, s3)
60+
color(v.x, v.y, v.z)
61+
end
62+
63+
# Rotate the current coordinate system.
64+
# Uses smooth_vector to smoothly animate the rotation.
65+
def smooth_rotation(s1, s2, s3)
66+
r1 = smooth_vector(s1, s2, s3)
67+
rotate_x(2.0 * Math::PI * r1.x)
68+
rotate_y(2.0 * Math::PI * r1.y)
69+
rotate_x(2.0 * Math::PI * r1.z)
70+
end
71+
72+
# Draw an icosahedron defined by a radius r and recursive depth d.
73+
# Geometry data will be saved into dst. If spherical is true then the
74+
# icosahedron is projected onto the sphere with radius r.
75+
def draw_icosahedron(depth, r, spherical)
76+
# Calculate the vertex data for an icosahedron inscribed by a sphere radius
77+
# 'r'. Use 4 Golden Ratio rectangles as the basis.
78+
gr = (1.0 + Math.sqrt(5.0)) / 2.0
79+
h = r / Math.sqrt(1.0 + gr * gr)
80+
v = [
81+
Vec3D.new(0, -h, h * gr),
82+
Vec3D.new(0, -h, -h * gr),
83+
Vec3D.new(0, h, -h * gr),
84+
Vec3D.new(0, h, h * gr),
85+
Vec3D.new(h, -h * gr, 0),
86+
Vec3D.new(h, h * gr, 0),
87+
Vec3D.new(-h, h * gr, 0),
88+
Vec3D.new(-h, -h * gr, 0),
89+
Vec3D.new(-h * gr, 0, h),
90+
Vec3D.new(-h * gr, 0, -h),
91+
Vec3D.new(h * gr, 0, -h),
92+
Vec3D.new(h * gr, 0, h)
93+
]
94+
# Draw the 20 triangular faces of the icosahedron.
95+
r = 0.0 unless spherical
96+
begin_shape(TRIANGLES)
97+
draw_triangle(depth, r, v[0], v[7], v[4])
98+
draw_triangle(depth, r, v[0], v[4], v[11])
99+
draw_triangle(depth, r, v[0], v[11], v[3])
100+
draw_triangle(depth, r, v[0], v[3], v[8])
101+
draw_triangle(depth, r, v[0], v[8], v[7])
102+
draw_triangle(depth, r, v[1], v[4], v[7])
103+
draw_triangle(depth, r, v[1], v[10], v[4])
104+
draw_triangle(depth, r, v[10], v[11], v[4])
105+
draw_triangle(depth, r, v[11], v[5], v[10])
106+
draw_triangle(depth, r, v[5], v[3], v[11])
107+
draw_triangle(depth, r, v[3], v[6], v[5])
108+
draw_triangle(depth, r, v[6], v[8], v[3])
109+
draw_triangle(depth, r, v[8], v[9], v[6])
110+
draw_triangle(depth, r, v[9], v[7], v[8])
111+
draw_triangle(depth, r, v[7], v[1], v[9])
112+
draw_triangle(depth, r, v[2], v[1], v[9])
113+
draw_triangle(depth, r, v[2], v[10], v[1])
114+
draw_triangle(depth, r, v[2], v[5], v[10])
115+
draw_triangle(depth, r, v[2], v[6], v[5])
116+
draw_triangle(depth, r, v[2], v[9], v[6])
117+
end_shape
118+
end
119+
120+
##
121+
# Draw a triangle either immediately or subdivide it first.
122+
# If depth is 1 then draw the triangle otherwise subdivide first.
123+
#
124+
def draw_triangle(depth, r, p1, p2, p3)
125+
if depth == 1
126+
p1.to_vertex(@renderer)
127+
p2.to_vertex(@renderer)
128+
p3.to_vertex(@renderer)
129+
else
130+
# Calculate the mid points of this triangle.
131+
v1 = (p1 + p2) * 0.5
132+
v2 = (p2 + p3) * 0.5
133+
v3 = (p3 + p1) * 0.5
134+
unless r == 0.0
135+
# Project the vertices out onto the sphere with radius r.
136+
v1.normalize!
137+
v1 *= r
138+
v2.normalize!
139+
v2 *= r
140+
v3.normalize!
141+
v3 *= r
142+
end
143+
## Generate the next level of detail
144+
depth -= 1
145+
draw_triangle(depth, r, p1, v1, v3)
146+
draw_triangle(depth, r, v1, p2, v2)
147+
draw_triangle(depth, r, v2, p3, v3)
148+
# Uncomment out the next line to include the central part of the triangle.
149+
# draw_triangle(depth, r, v1, v2, v3)
150+
end
151+
end
152+
153+
```

docs/_posts/2019-03-01-welcome-to-vecmath-gem.md

Lines changed: 7 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -4,97 +4,14 @@ title: "Welcome to Vecmath Gem for Visor"
44
date: 2019-03-01 09:58:08 +0000
55
categories: vecmath update
66
---
7+
# Background
78

8-
# Example Usage
9+
The vecmath Vec2D and Vec3D classes were originally build for JRubyArt and provide more ruby-like vector classes than the somewhat suspect vanilla processing PVector class (that doesn't know whether it's 2D or 3D). Further the AppRender class enables direct writing of vector to vertex
910

10-
```ruby
11-
# ===== Default : Default
12-
GEM_HOME = '/home/tux/.gem/ruby/2.6.0'
13-
require 'vecmath'
14-
require 'forwardable'
11+
# Building the Gem
1512

16-
# Here we use the JRubyArt Vec2D class, and AppRender class (for vec.to_vertex)
17-
# Further we use the power of ruby (metaprogramming) to make Branch enumerable
18-
# and use Forwardable to define which enumerable methods we want to use.
19-
visor_class :Branch do
20-
include Enumerable
21-
extend Forwardable
22-
def_delegators(:@children, :<<, :each, :length)
23-
# variance angle for growth direction per time step
24-
THETA = Math::PI / 6
25-
# max segments per branch
26-
MAX_LEN = 100
27-
# max recursion limit
28-
MAX_GEN = 3
29-
# branch chance per time step
30-
BRANCH_CHANCE = 0.05
31-
# branch angle variance
32-
BRANCH_THETA = Math::PI / 3
33-
attr_reader :position, :dir, :path, :children, :xbound, :speed, :ybound, :app
13+
All you need is `mvn` to build the gem, and a ruby installation
3414

35-
def initialize(pos, dir, speed)
36-
@renderer ||= AppRender.new(graphics)
37-
@position = pos
38-
@dir = dir
39-
@speed = speed
40-
@path = []
41-
@children = []
42-
@xbound = Boundary.new(0, width)
43-
@ybound = Boundary.new(0, height)
44-
path << pos
45-
end
46-
47-
def run
48-
grow
49-
display
50-
end
51-
52-
private
53-
54-
# NB: use of both rotate! (changes original) rotate (returns a copy) of Vec2D
55-
def grow
56-
check_bounds(position + (dir * speed)) if path.length < MAX_LEN
57-
@position += (dir * speed)
58-
dir.rotate!(rand(-0.5..0.5) * THETA)
59-
path << position
60-
if (length < MAX_GEN) && (rand < BRANCH_CHANCE)
61-
branch_dir = dir.rotate(rand(-0.5..0.5) * BRANCH_THETA)
62-
self << Branch.new(position.copy, branch_dir, speed * 0.99)
63-
end
64-
each(&:grow)
65-
end
66-
67-
def display
68-
begin_shape
69-
stroke(255)
70-
no_fill
71-
path.each { |vec| vec.to_vertex(@renderer) }
72-
end_shape
73-
each(&:display)
74-
end
75-
76-
def check_bounds(pos)
77-
dir.x *= -1 if xbound.exclude? pos.x
78-
dir.y *= -1 if ybound.exclude? pos.y
79-
end
80-
end
81-
82-
# we are looking for excluded values
83-
Boundary = Struct.new(:lower, :upper) do
84-
def exclude?(val)
85-
!(lower...upper).cover? val
86-
end
87-
end
88-
89-
@root = Branch.new(
90-
Vec2D.new(0, height / 2),
91-
Vec2D.new(1, 0),
92-
10.0
93-
)
94-
95-
def draw
96-
background(0)
97-
root.run
98-
end
99-
100-
```
15+
1. clone Repo
16+
2. cd Repo
17+
3. rake

0 commit comments

Comments
 (0)