Here is the Vec3D version of set_mag that takes an optional block
Using the Vec3D name was a simple as Vec3D = Processing::Vec2::Vec2
/** * Call yield if block given, do nothing if yield == false * else set_mag to given scalar * @param context * @param scalar double value to set * @param block should return a boolean (optional) * @return */ @JRubyMethod(name = "set_mag") public IRubyObject set_mag(ThreadContext context, IRubyObject scalar, Block block) { if (block.isGiven()) { if (!(boolean) block.yield(context, scalar).toJava(Boolean.class)) { return this; } } double new_mag = (Double) scalar.toJava(Double.class); double current = FastMath.sqrt(FastMath.pow(jx, 2) + FastMath.pow(jy, 2) + FastMath.pow(jz, 2)); jx *= new_mag / current; jy *= new_mag / current; jz *= new_mag / current; return this; }
See the full code here.
No comments:
Post a Comment