Skip to content

Commit a2b9dc9

Browse files
committed
implement JRybyArt grid method in java, because we can
1 parent b9f8d9b commit a2b9dc9

File tree

3 files changed

+56
-10
lines changed

3 files changed

+56
-10
lines changed

lib/jruby_art/helper_methods.rb

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,8 @@ def perspektiv(
3737

3838
# A nice method to run a given block for a grid.
3939
# Lifted from action_coding/Nodebox.
40-
def grid(cols, rows, col_size = 1, row_size = 1)
41-
(0...cols * rows).map do |i|
42-
x = col_size * (i % cols)
43-
y = row_size * i.div(cols)
44-
yield x, y
45-
end
46-
end
40+
# def grid(cols, rows, col_size = 1, row_size = 1) { |x, y| block_stuff }
41+
# NB: now implemented in java
4742

4843
# lerp_color takes three or four arguments, in Java that's two
4944
# different methods, one regular and one static, so:
@@ -181,7 +176,7 @@ def key_pressed?
181176

182177
private
183178

184-
FIXNUM_COL = -> (x) { x.is_a?(Integer) }
179+
FIXNUM_COL = -> (x) { x.is_a?(Integer) }
185180
STRING_COL = -> (x) { x.is_a?(String) }
186181
FLOAT_COL = -> (x) { x.is_a?(Float) }
187182
# parse single argument color int/double/String

src/monkstone/MathToolModule.java

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
/**
22
* The purpose of this tool is to allow JRubyArt users to use an alternative
3-
* to processing.org map, lerp and norm methods in their sketches
4-
* Copyright (c) 2015-16 Martin Prout. This tool is free software; you can
3+
* to processing.org map, lerp and norm methods in their sketches and to implement
4+
* JRubyArt convenenience method grid(width, height, stepW, stepH) { |x, y| do stuff }
5+
* Copyright (c) 2015-17 Martin Prout. This tool is free software; you can
56
* redistribute it and/or modify it under the terms of the GNU Lesser General
67
* Public License as published by the Free Software Foundation; either version
78
* 2.1 of the License, or (at your option) any later version.
@@ -17,6 +18,7 @@
1718
import org.jruby.RubyRange;
1819
import org.jruby.anno.JRubyMethod;
1920
import org.jruby.anno.JRubyModule;
21+
import org.jruby.runtime.Block;
2022
import org.jruby.runtime.ThreadContext;
2123
import org.jruby.runtime.builtin.IRubyObject;
2224

@@ -201,4 +203,35 @@ public static IRubyObject constrainValue(ThreadContext context, IRubyObject recv
201203
return args[1];
202204
}
203205
}
206+
207+
/**
208+
* Provides JRubyArt grid method as a ruby module method
209+
*
210+
* @param context ThreadContext
211+
* @param recv IRubyObject
212+
* @param args array of args should be Fixnum
213+
* @param block { |x, y| `do something` }
214+
* @return nil
215+
*/
216+
@JRubyMethod(name = "grid", rest = true, module = true)
217+
public static IRubyObject createGrid(ThreadContext context, IRubyObject recv, IRubyObject[] args, Block block) {
218+
int row = (Integer) args[0].toJava(Integer.class);
219+
int column = (Integer) args[1].toJava(Integer.class);
220+
int rowStep = 1;
221+
int colStep = 1;
222+
if (args.length == 4){
223+
rowStep = (Integer) args[2].toJava(Integer.class);
224+
colStep = (Integer) args[3].toJava(Integer.class);
225+
}
226+
if (block.isGiven()) {
227+
for (int x = 0; x < row / rowStep; x++){
228+
for (int y = 0; y < column / colStep; y++){
229+
block.yieldSpecific(context, context.runtime.newFixnum(x * rowStep), context.runtime.newFixnum(y * colStep));
230+
231+
}
232+
}
233+
}
234+
return context.nil;
235+
236+
}
204237
}

test/math_tool_test.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,22 @@ def test_constrain
7676
assert_in_delta(constrain(xf[1], startf, lastf), 200.0, delta = 0.00001, msg = 'constrain to 0..200')
7777
assert_in_delta(constrain(xf[2], startf, lastf), 0.0, delta = 0.00001, msg = 'constrain to 0..200')
7878
end
79+
80+
def test_grid_one
81+
array = []
82+
grid(100, 100) { |x, y| array << [x, y] }
83+
assert array[0].include?(0)
84+
assert array[98].include?(0)
85+
assert_equal array.length, 10_000
86+
assert array[50].include?(50)
87+
end
88+
89+
def test_grid_ten
90+
array = []
91+
grid(100, 100, 10, 10) { |x, y| array << [x, y] }
92+
assert array[0].include?(0)
93+
assert array[9].include?(90)
94+
assert_equal array.length, 100
95+
assert array[5].include?(50)
96+
end
7997
end

0 commit comments

Comments
 (0)