Skip to content

Commit e61dcc3

Browse files
committed
Change vec2 cross product to wedge product operator.
1 parent 370c783 commit e61dcc3

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

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

Lines changed: 15 additions & 6 deletions
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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -261,22 +261,22 @@ def test_cross_area # NB: the sign might be negative
261261
a = Vec2D.new(200, 0)
262262
b = Vec2D.new(0, 200)
263263
# Expected result is an area, twice that of the triangle created by the vectors
264-
assert_equal(a.cross(b).abs, 40_000.0, 'Failed area test using 2D vector cross product')
264+
assert_equal((a ^ b).abs, 40_000.0, 'Failed area test using 2D vector cross product')
265265
end
266266

267267
def test_cross_non_zero # Could be used to calculate area of triangle
268268
a = Vec2D.new(40, 40)
269269
b = Vec2D.new(40, 140)
270270
c = Vec2D.new(140, 40)
271-
assert_equal((a - b).cross(b - c).abs / 2, 5_000.0, 'Failed area calculation using 2D vector cross product')
271+
assert_equal(((a - b) ^ (b - c)).abs / 2, 5_000.0, 'Failed area calculation using 2D vector cross product')
272272
end
273273

274274
def test_cross_zero # where a, b, c are collinear area == 0
275275
a = Vec2D.new(0, 0)
276276
b = Vec2D.new(100, 100)
277277
c = Vec2D.new(200, 200)
278278
# see http://mathworld.wolfram.com/Collinear.html for details
279-
assert((a - b).cross(b - c).zero?, 'Failed collinearity test using 2D vector cross product')
279+
assert(((a - b) ^ (b - c)).zero?, 'Failed collinearity test using 2D vector cross product')
280280
end
281281

282282
def test_equals3

0 commit comments

Comments
 (0)