Skip to content

Commit ead3479

Browse files
committed
another post
1 parent d503278 commit ead3479

File tree

2 files changed

+101
-1
lines changed

2 files changed

+101
-1
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
---
2+
layout: post
3+
title: "Welcome to Vecmath Gem for Visor"
4+
date: 2019-03-01 10:58:08 +0000
5+
categories: vecmath update
6+
---
7+
8+
# Example Usage
9+
10+
```ruby
11+
# ===== Default : Default
12+
GEM_HOME = '/home/tux/.gem/ruby/2.6.0'
13+
require 'vecmath'
14+
require 'forwardable'
15+
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
34+
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+
```

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
layout: post
33
title: "Welcome to Vecmath Gem for Visor"
4-
date: 2019-03-01 10:58:08 +0000
4+
date: 2019-03-01 09:58:08 +0000
55
categories: vecmath update
66
---
77

0 commit comments

Comments
 (0)