Skip to content

Commit fc18607

Browse files
committed
Split up lenny use to_curve_vertex
1 parent e730fa1 commit fc18607

File tree

10 files changed

+156
-18
lines changed

10 files changed

+156
-18
lines changed

contributed/lenny_explorer.rb

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
# lenny_explorer.rb
1+
# A simple lenny explorer (see Vec2D library folder for more examples)
22
# After 'The Explorer' by Leander Herzog, and a toxiclibs version by Karsten
33
# Schmidt. This is a pure JRubyArt version by Martin Prout
44
load_library :lenny
5-
attr_reader :path
5+
attr_reader :path, :renderer
66

77
def settings
88
size(600, 600)
@@ -11,29 +11,19 @@ def settings
1111
def setup
1212
sketch_title 'Lenny Explorer'
1313
no_fill
14-
# @path = Path.new(
15-
# Boundary.new(
16-
# Rect.new(
17-
# Vec2D.new(10, 10),
18-
# Vec2D.new(width - 20, height - 20)
19-
# )
20-
# ),
21-
# 10,
22-
# 0.03,
23-
# 3_000
24-
# )
2514
@path = Path.new(
2615
Boundary.new(Circle.new(Vec2D.new(width / 2, height / 2), 250)),
2716
10,
2817
0.03,
29-
3_000
18+
5_000
3019
)
20+
@renderer = GfxRender.new(g)
3121
end
3222

3323
def draw
3424
background(255)
3525
50.times { path.grow }
36-
path.render(g)
26+
path.render(g, renderer)
3727
end
3828

3929
def mouse_pressed

contributed/library/lenny/lenny.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
require_relative 'lib/rect'
21
require_relative 'lib/circle'
32
require_relative 'lib/boundary'
43
require_relative 'lib/path'

contributed/library/lenny/lib/path.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ def search
3535
@searches += 1
3636
end
3737

38-
def render(gfx)
38+
def render(gfx, renderer)
3939
gfx.begin_shape
40-
path.map { |vec| gfx.curve_vertex(vec.x, vec.y) }
40+
path.map { |vec| vec.to_curve_vertex(renderer) }
4141
gfx.end_shape
4242
end
4343

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# A simple lenny explorer (see Vec2D library folder for more examples)
2+
# After 'The Explorer' by Leander Herzog, and a toxiclibs version by Karsten
3+
# Schmidt. This is a pure JRubyArt version by Martin Prout
4+
load_library :lenny
5+
attr_reader :path, :renderer
6+
7+
def settings
8+
size(600, 600)
9+
end
10+
11+
def setup
12+
sketch_title 'Lenny Explorer'
13+
no_fill
14+
# @path = Path.new(
15+
# Boundary.new(Circle.new(Vec2D.new(width / 2, height / 2), 250)),
16+
# 10,
17+
# 0.03,
18+
# 5_000
19+
# )
20+
@path = Path.new(
21+
Boundary.new(
22+
Rect.new(
23+
Vec2D.new(10, 10),
24+
Vec2D.new(width - 20, height - 20)
25+
)
26+
),
27+
10,
28+
0.03,
29+
5_000
30+
)
31+
@renderer = GfxRender.new(g)
32+
end
33+
34+
def draw
35+
background(255)
36+
50.times { path.grow }
37+
path.render(g, renderer)
38+
end
39+
40+
def mouse_pressed
41+
save_frame data_path('lenny.png')
42+
end
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
require_relative 'lib/rect'
2+
require_relative 'lib/circle'
3+
require_relative 'lib/boundary'
4+
require_relative 'lib/path'
5+
require_relative 'lib/line2d'
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Duck typing Boundary
2+
class Boundary
3+
attr_reader :bounds
4+
def initialize(bounds)
5+
@bounds = bounds
6+
end
7+
8+
def contains?(vec)
9+
bounds.contains?(vec)
10+
end
11+
12+
def centroid
13+
bounds.centroid
14+
end
15+
end
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# For use with Boundary
2+
class Circle
3+
attr_reader :centroid, :radius
4+
def initialize(center, radius)
5+
@centroid = center
6+
@radius = radius
7+
end
8+
9+
def contains?(vec)
10+
centroid.dist(vec) < radius
11+
end
12+
end
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Use to detect whether instances of line intersect
2+
class Line2D
3+
attr_reader :a, :b
4+
def initialize(a, b)
5+
@a = a
6+
@b = b
7+
end
8+
9+
def intersecting?(line)
10+
denom = (line.b.y - line.a.y) * (b.x - a.x) - (line.b.x - line.a.x) * (b.y - a.y)
11+
na = (line.b.x - line.a.x) * (a.y - line.a.y) - (line.b.y - line.a.y) * (a.x - line.a.x)
12+
nb = (b.x - a.x) * (a.y - line.a.y) - (b.y - a.y) * (a.x - line.a.x)
13+
return false if denom.zero?
14+
15+
ua = na / denom
16+
ub = nb / denom
17+
(ua >= 0.0 && ua <= 1.0 && ub >= 0.0 && ub <= 1.0)
18+
end
19+
end
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Stores and manipulates the path
2+
class Path
3+
attr_reader :path, :last, :bounds, :cut, :theta, :delta, :speed, :searches
4+
5+
def initialize(bounds, speed, delta, history)
6+
@bounds = bounds
7+
@speed = speed
8+
@delta = delta
9+
@theta = 0
10+
@path = (0..history).map { bounds.centroid.copy }
11+
@searches = 0
12+
@last = Vec2D.new(path.first)
13+
end
14+
15+
def grow
16+
@delta = rand(-0.2..0.2) if rand < 0.1
17+
if !intersecting?
18+
move
19+
else
20+
search
21+
end
22+
end
23+
24+
def move
25+
@last = path.first
26+
path.pop
27+
@theta += delta
28+
path.unshift last + Vec2D.new(speed * Math.cos(theta), speed * Math.sin(theta))
29+
@searches = 0
30+
end
31+
32+
def search
33+
@theta += delta
34+
path[0] = last + Vec2D.new(speed * Math.cos(theta), speed * Math.sin(theta))
35+
@searches += 1
36+
end
37+
38+
def render(gfx, renderer)
39+
gfx.begin_shape
40+
path.map { |vec| vec.to_curve_vertex(renderer) }
41+
gfx.end_shape
42+
end
43+
44+
def intersecting?
45+
return true unless bounds.contains?(path.first)
46+
47+
if searches < 100
48+
a = Line2D.new(path[0], path[1])
49+
(3...path.length).each do |i|
50+
b = Line2D.new(path[i], path[i - 1])
51+
return true if a.intersecting?(b)
52+
end
53+
end
54+
false
55+
end
56+
end

contributed/library/lenny/lib/rect.rb renamed to processing_app/library/vecmath/vec2d/library/lenny/lib/rect.rb

File renamed without changes.

0 commit comments

Comments
 (0)