Skip to content

Commit 10e7690

Browse files
committed
hash key
1 parent 173b536 commit 10e7690

File tree

2 files changed

+17
-21
lines changed

2 files changed

+17
-21
lines changed

demo/fire.rb

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@
1313
# jruby fire.rb
1414
class Fire < Processing::App
1515
load_library :palette
16-
16+
1717
def settings
1818
size 320, 240
1919
end
20-
20+
2121
def setup
2222
sketch_title 'Fire'
2323
frame_rate 30
@@ -28,12 +28,12 @@ def setup
2828
@height = height / @scale
2929
@intensity = 2
3030
end
31-
31+
3232
def draw
3333
background 0
3434
update_fire
3535
end
36-
36+
3737
def update_fire
3838
random_line @height - 1
3939
(0..@height - 2).each do |y|
@@ -53,25 +53,25 @@ def update_fire
5353
end
5454
end
5555
end
56-
56+
5757
def fire_data(x, y)
5858
@fire[offset(x, y)]
5959
end
60-
60+
6161
def set_fire_data(x, y, value)
6262
@fire[offset(x, y)] = value.to_i
6363
end
64-
64+
6565
def random_offset
6666
rand(0..@palette.size)
6767
end
68-
68+
6969
def random_line(y)
7070
(0...@width).each do |x|
7171
@fire[offset(x, y)] = random_offset
7272
end
7373
end
74-
74+
7575
def offset(x, y)
7676
(y * @width) + x
7777
end

library/vecmath/vec3d/terrain.rb

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,6 @@
11
#!/usr/bin/env jruby
22
require 'picrate'
33

4-
# Alternative to using array or string as key
5-
Key = Struct.new(:x, :y) do
6-
def eql?(key)
7-
return false unless key.y == y
8-
key.x == x
9-
end
10-
end
11-
124
class Terrain < Processing::App
135
WIDTH = 1400
146
HEIGHT = 1100
@@ -20,7 +12,6 @@ def settings
2012
end
2113

2214
def setup
23-
sketch_title 'Terreno' # sketch_title doesn't work with P3D on PI
2415
@columns = WIDTH / SCL
2516
@rows = HEIGHT / SCL
2617
@terrain = {}
@@ -34,7 +25,7 @@ def draw
3425
(0..rows).each do |y|
3526
xoff = 0
3627
(0..columns).each do |x|
37-
terrain[Key.new(x, y)] = Vec3D.new(x * SCL, y * SCL, map1d(noise(xoff, yoff), 0..1.0, -65..65))
28+
terrain[hash_key(x, y)] = Vec3D.new(x * SCL, y * SCL, map1d(noise(xoff, yoff), 0..1.0, -65..65))
3829
xoff += 0.2
3930
end
4031
yoff += 0.2
@@ -47,15 +38,20 @@ def draw
4738
(0...rows).each do |y|
4839
begin_shape(TRIANGLE_STRIP)
4940
(0..columns).each do |x|
50-
terrain[Key.new(x, y)].to_vertex(renderer)
51-
terrain[Key.new(x, y.succ)].to_vertex(renderer)
41+
terrain[hash_key(x, y)].to_vertex(renderer)
42+
terrain[hash_key(x, y.succ)].to_vertex(renderer)
5243
end
5344
end_shape(CLOSE)
5445
end
5546
end
5647

5748
private
5849

50+
# HACK should be safe here
51+
def hash_key(x, y)
52+
WIDTH * y + x
53+
end
54+
5955
def renderer
6056
@renderer ||= AppRender.new(self)
6157
end

0 commit comments

Comments
 (0)