Skip to content

Commit 842c6c1

Browse files
committed
Favor op_wedge ^ over cross for Vec2D
1 parent 32c338b commit 842c6c1

File tree

3 files changed

+19
-10
lines changed

3 files changed

+19
-10
lines changed

pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ DO NOT MODIFY - GENERATED CODE
1111
<modelVersion>4.0.0</modelVersion>
1212
<groupId>ruby-processing</groupId>
1313
<artifactId>picrate</artifactId>
14-
<version>2.5.1</version>
14+
<version>2.5.2</version>
1515
<name>picrate</name>
1616
<description>An integrated processing-core (somewhat hacked), with additional java code for a jruby version of processing.</description>
1717
<url>http://maven.apache.org</url>

src/main/java/monkstone/vecmath/vec2/Vec2.java

+15-6
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
*
2222
* fastAtan2 algorithm from https://github.com/libgdx/libgdx (Apache 2.0 license)
2323
*/
24+
import java.util.logging.Logger;
2425
import org.jruby.Ruby;
2526
import org.jruby.RubyArray;
2627
import org.jruby.RubyClass;
@@ -228,19 +229,27 @@ public IRubyObject dist(ThreadContext context, IRubyObject other) {
228229
return runtime.newFloat(result);
229230
}
230231

232+
@Deprecated
233+
@JRubyMethod(name = "cross", required = 1)
234+
public IRubyObject cross(ThreadContext context, IRubyObject other) {
235+
Logger log = Logger.getGlobal();
236+
log.warning("prefer ^ operator");
237+
return op_wedge(context, other);
238+
}
239+
231240
/**
232241
*
233242
* @param context ThreadContext
234243
* @param other IRubyObject
235-
* @return cross product IRubyObject
244+
* @return wedge product IRubyObject
236245
*/
237-
@JRubyMethod(name = "cross", required = 1)
246+
@JRubyMethod(name = "^", required = 1)
238247

239-
public IRubyObject cross(ThreadContext context, IRubyObject other) {
248+
public IRubyObject op_wedge(ThreadContext context, IRubyObject other) {
240249
Vec2 b = null;
241250
Ruby runtime = context.runtime;
242251
if (other instanceof Vec2) {
243-
b = (Vec2) other.toJava(Vec2.class);
252+
b = other.toJava(Vec2.class);
244253
} else {
245254
throw runtime.newTypeError("argument should be Vec2D");
246255
}
@@ -401,7 +410,7 @@ public IRubyObject mag(ThreadContext context) {
401410
public IRubyObject set_mag(ThreadContext context, IRubyObject scalar, Block block) {
402411
double new_mag = scalar.toJava(Double.class);
403412
if (block.isGiven() && !block.yield(context, scalar).toJava(Boolean.class)) {
404-
return this;
413+
return this;
405414
}
406415
double current = 0;
407416
if (Math.abs(jx) > EPSILON && Math.abs(jy) > EPSILON) {
@@ -789,7 +798,7 @@ public IRubyObject op_equal(ThreadContext context, IRubyObject other) {
789798
double diff = jx - v.jx;
790799
if ((diff < 0 ? -diff : diff) > Vec2.EPSILON) {
791800
return runtime.newBoolean(false);
792-
}
801+
}
793802
diff = jy - v.jy;
794803
return runtime.newBoolean((diff < 0 ? -diff : diff) < Vec2.EPSILON);
795804
}

test/vecmath_spec_test.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -270,22 +270,22 @@ def test_cross_area # NB: the sign might be negative
270270
a = Vec2D.new(200, 0)
271271
b = Vec2D.new(0, 200)
272272
# Expected result is an area, twice that of the triangle created by the vectors
273-
assert_equal(a.cross(b).abs, 40_000.0, 'Failed area test using 2D vector cross product')
273+
assert_equal((a ^ b).abs, 40_000.0, 'Failed area test using 2D vector cross product')
274274
end
275275

276276
def test_cross_non_zero # Could be used to calculate area of triangle
277277
a = Vec2D.new(40, 40)
278278
b = Vec2D.new(40, 140)
279279
c = Vec2D.new(140, 40)
280-
assert_equal((a - b).cross(b - c).abs / 2, 5_000.0, 'Failed area calculation using 2D vector cross product')
280+
assert_equal(((a - b) ^ (b - c)).abs / 2, 5_000.0, 'Failed area calculation using 2D vector cross product')
281281
end
282282

283283
def test_cross_zero # where a, b, c are collinear area == 0
284284
a = Vec2D.new(0, 0)
285285
b = Vec2D.new(100, 100)
286286
c = Vec2D.new(200, 200)
287287
# see http://mathworld.wolfram.com/Collinear.html for details
288-
assert((a - b).cross(b - c).zero?, 'Failed collinearity test using 2D vector cross product')
288+
assert(((a - b) ^ (b - c)).zero?, 'Failed collinearity test using 2D vector cross product')
289289
end
290290

291291
def test_equals3

0 commit comments

Comments
 (0)