Skip to content

Commit 44e99bf

Browse files
committed
simplify rect
1 parent fc18607 commit 44e99bf

File tree

5 files changed

+48
-49
lines changed

5 files changed

+48
-49
lines changed

contributed/lenny_explorer.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,13 @@ def setup
2323
def draw
2424
background(255)
2525
50.times { path.grow }
26-
path.render(g, renderer)
26+
draw_path(path.points)
27+
end
28+
29+
def draw_path(points)
30+
begin_shape
31+
points.map { |vec| vec.to_curve_vertex(renderer) }
32+
end_shape
2733
end
2834

2935
def mouse_pressed

contributed/library/lenny/lib/path.rb

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
# Stores and manipulates the path
1+
# Stores and manipulates the points
22
class Path
3-
attr_reader :path, :last, :bounds, :cut, :theta, :delta, :speed, :searches
3+
attr_reader :points, :last, :bounds, :cut, :theta, :delta, :speed, :searches
44

55
def initialize(bounds, speed, delta, history)
66
@bounds = bounds
77
@speed = speed
88
@delta = delta
99
@theta = 0
10-
@path = (0..history).map { bounds.centroid.copy }
10+
@points = (0..history).map { bounds.centroid.copy }
1111
@searches = 0
12-
@last = Vec2D.new(path.first)
12+
@last = Vec2D.new(points.first)
1313
end
1414

1515
def grow
@@ -22,33 +22,27 @@ def grow
2222
end
2323

2424
def move
25-
@last = path.first
26-
path.pop
25+
@last = points.first
26+
points.pop
2727
@theta += delta
28-
path.unshift last + Vec2D.new(speed * Math.cos(theta), speed * Math.sin(theta))
28+
points.unshift last + Vec2D.new(speed * Math.cos(theta), speed * Math.sin(theta))
2929
@searches = 0
3030
end
3131

3232
def search
3333
@theta += delta
34-
path[0] = last + Vec2D.new(speed * Math.cos(theta), speed * Math.sin(theta))
34+
points[0] = last + Vec2D.new(speed * Math.cos(theta), speed * Math.sin(theta))
3535
@searches += 1
3636
end
3737

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-
4438
def intersecting?
45-
return true unless bounds.contains?(path.first)
39+
return true unless bounds.contains?(points.first)
4640

4741
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)
42+
one = Line2D.new(points[0], points[1])
43+
(3...points.length).each do |idx|
44+
two = Line2D.new(points[idx], points[idx - 1])
45+
return true if one.intersecting?(two)
5246
end
5347
end
5448
false

processing_app/library/vecmath/vec2d/lenny_explorer.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,13 @@ def setup
3434
def draw
3535
background(255)
3636
50.times { path.grow }
37-
path.render(g, renderer)
37+
draw_path path.points
38+
end
39+
40+
def draw_path(points)
41+
begin_shape
42+
points.map { |vec| vec.to_curve_vertex(renderer) }
43+
end_shape
3844
end
3945

4046
def mouse_pressed

processing_app/library/vecmath/vec2d/library/lenny/lib/path.rb

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
# Stores and manipulates the path
22
class Path
3-
attr_reader :path, :last, :bounds, :cut, :theta, :delta, :speed, :searches
3+
attr_reader :points, :last, :bounds, :cut, :theta, :delta, :speed, :searches
44

55
def initialize(bounds, speed, delta, history)
66
@bounds = bounds
77
@speed = speed
88
@delta = delta
99
@theta = 0
10-
@path = (0..history).map { bounds.centroid.copy }
10+
@points = (0..history).map { bounds.centroid.copy }
1111
@searches = 0
12-
@last = Vec2D.new(path.first)
12+
@last = Vec2D.new(points.first)
1313
end
1414

1515
def grow
@@ -22,32 +22,26 @@ def grow
2222
end
2323

2424
def move
25-
@last = path.first
26-
path.pop
25+
@last = points.first
26+
points.pop
2727
@theta += delta
28-
path.unshift last + Vec2D.new(speed * Math.cos(theta), speed * Math.sin(theta))
28+
points.unshift last + Vec2D.new(speed * Math.cos(theta), speed * Math.sin(theta))
2929
@searches = 0
3030
end
3131

3232
def search
3333
@theta += delta
34-
path[0] = last + Vec2D.new(speed * Math.cos(theta), speed * Math.sin(theta))
34+
points[0] = last + Vec2D.new(speed * Math.cos(theta), speed * Math.sin(theta))
3535
@searches += 1
3636
end
3737

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-
4438
def intersecting?
45-
return true unless bounds.contains?(path.first)
39+
return true unless bounds.contains?(points.first)
4640

4741
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])
42+
a = Line2D.new(points[0], points[1])
43+
(3...points.length).each do |i|
44+
b = Line2D.new(points[i], points[i - 1])
5145
return true if a.intersecting?(b)
5246
end
5347
end
Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
11
# For use with Boundary
22
class Rect
3-
attr_reader :vec1, :vec2
4-
def initialize(vec1, vec2)
5-
@vec1 = vec1
6-
@vec2 = vec2
3+
attr_reader :lbt, :rtp
4+
def initialize(lbt, rtp)
5+
@lbt = lbt
6+
@rtp = rtp
77
end
88

99
def centroid
10-
(vec1 + vec2) / 2
10+
(lbt + rtp) / 2
1111
end
1212

1313
def contains?(vec)
14-
xminmax = [vec1.x, vec2.x].minmax
15-
yminmax = [vec1.y, vec2.y].minmax
16-
return false if vec.x < xminmax[0]
17-
return false if vec.x > xminmax[1]
18-
return false if vec.y < yminmax[0]
14+
otherx = vec.x
15+
othery = vec.y
16+
return false if otherx < lbt.x || otherx > rtp.x
17+
return false if othery < lbt.y
1918

20-
vec.y < yminmax[1]
19+
othery < rtp.y
2120
end
2221
end

0 commit comments

Comments
 (0)