/** * * @param context ThreadContext * @param scalar IRubyObject * @return this Vec2 object rotated */ @JRubyMethod(name = "rotate!") public IRubyObject rotate_bang(ThreadContext context, IRubyObject scalar) { double theta = (scalar instanceof RubyFloat) ? ((RubyFloat) scalar).getValue() : ((RubyFixnum) scalar).getDoubleValue(); double x = (jx * Math.cos(theta) - jy * Math.sin(theta)); double y = (jx * Math.sin(theta) + jy * Math.cos(theta)); jx = x; jy = y; return this; }
/** * * @param context ThreadContext * @param recv self IRubyObject * @param args floats as in processing map function * @return mapped value RubyFloat */ @JRubyMethod(name = "p5map", rest = true, module = true) public static IRubyObject mapProcessing(ThreadContext context, IRubyObject recv, IRubyObject[] args) { double value = (args[0] instanceof RubyFloat) ? ((RubyFloat) args[0]).getValue() : ((RubyFixnum) args[0]).getDoubleValue(); double first1 = (args[1] instanceof RubyFloat) ? ((RubyFloat) args[1]).getValue() : ((RubyFixnum) args[1]).getDoubleValue(); double first2 = (args[3] instanceof RubyFloat) ? ((RubyFloat) args[3]).getValue() : ((RubyFixnum) args[3]).getDoubleValue(); double last1 = (args[2] instanceof RubyFloat) ? ((RubyFloat) args[2]).getValue() : ((RubyFixnum) args[2]).getDoubleValue(); double last2 = (args[4] instanceof RubyFloat) ? ((RubyFloat) args[4]).getValue() : ((RubyFixnum) args[4]).getDoubleValue(); return mapMt(context, value, first1, last1, first2, last2); }
@JIT public static double unboxFloat(IRubyObject val) { if (val instanceof RubyFloat) { return ((RubyFloat)val).getValue(); } else { return ((RubyFixnum)val).getDoubleValue(); } }
@JIT public static double unboxFloat(IRubyObject val) { if (val instanceof RubyFloat) { return ((RubyFloat)val).getValue(); } else { return ((RubyFixnum)val).getDoubleValue(); } }
/** * Identical to p5map(value, low, high, 0, 1). Numbers outside of the range * are not clamped to 0 and 1, because out-of-range values are often * intentional and useful. * * @param context ThreadContext * @param recv IRubyObject * @param args array of args must be be numeric * @return mapped value RubyFloat */ @JRubyMethod(name = "norm", rest = true, module = true) public static IRubyObject normP(ThreadContext context, IRubyObject recv, IRubyObject[] args) { double value = (args[0] instanceof RubyFloat) ? ((RubyFloat) args[0]).getValue() : ((RubyFixnum) args[0]).getDoubleValue(); double start = (args[1] instanceof RubyFloat) ? ((RubyFloat) args[1]).getValue() : ((RubyFixnum) args[1]).getDoubleValue(); double stop = (args[2] instanceof RubyFloat) ? ((RubyFloat) args[2]).getValue() : ((RubyFixnum) args[2]).getDoubleValue(); return mapMt(context, value, start, stop, 0, 1.0); }
/** * * @param context ThreadContext * @param other IRubyObject * @return z IRubyObject */ @JRubyMethod(name = "z=") public IRubyObject setZ(ThreadContext context, IRubyObject other) { if (other instanceof RubyFloat) { jz = ((RubyFloat) other).getValue(); } else { jz = ((RubyFixnum) other).getDoubleValue(); } return other; }
/** * * @param context ThreadContext * @param other IRubyObject * @return x IRubyObject */ @JRubyMethod(name = "x=") public IRubyObject setX(ThreadContext context, IRubyObject other) { if (other instanceof RubyFloat) { jx = ((RubyFloat) other).getValue(); } else { jx = ((RubyFixnum) other).getDoubleValue(); } return other; }
/** * * @param context ThreadContext * @param other IRubyObject * @return y IRubyObject */ @JRubyMethod(name = "y=") public IRubyObject setY(ThreadContext context, IRubyObject other) { if (other instanceof RubyFloat) { jy = ((RubyFloat) other).getValue(); } else { jy = ((RubyFixnum) other).getDoubleValue(); } return other; }
/** * * @param context ThreadContext * @param other IRubyObject * @return y IRubyObject */ @JRubyMethod(name = "y=") public IRubyObject setY(ThreadContext context, IRubyObject other) { if (other instanceof RubyFloat) { jy = ((RubyFloat) other).getValue(); } else { jy = ((RubyFixnum) other).getDoubleValue(); } return other; }
/** * * @param context ThreadContext * @param other IRubyObject * @return x IRubyObject */ @JRubyMethod(name = "x=") public IRubyObject setX(ThreadContext context, IRubyObject other) { if (other instanceof RubyFloat) { jx = ((RubyFloat) other).getValue(); } else { jx = ((RubyFixnum) other).getDoubleValue(); } return other; }
static RubyFloat new_float(final Ruby runtime, RubyInteger num) { if (num instanceof RubyBignum) { return RubyFloat.newFloat(runtime, RubyBignum.big2dbl((RubyBignum) num)); } return RubyFloat.newFloat(runtime, ((RubyFixnum) num).getDoubleValue()); }
static RubyFloat new_float(final Ruby runtime, RubyInteger num) { if (num instanceof RubyBignum) { return RubyFloat.newFloat(runtime, RubyBignum.big2dbl((RubyBignum) num)); } return RubyFloat.newFloat(runtime, ((RubyFixnum) num).getDoubleValue()); }
/** * * @param context ThreadContext * @param scalar IRubyObject * @return a new Vec2 object rotated */ @JRubyMethod(name = "rotate") public IRubyObject rotate(ThreadContext context, IRubyObject scalar) { Ruby runtime = context.runtime; double theta = (scalar instanceof RubyFloat) ? ((RubyFloat) scalar).getValue() : ((RubyFixnum) scalar).getDoubleValue(); IRubyObject[] ary = new IRubyObject[]{ runtime.newFloat(jx * Math.cos(theta) - jy * Math.sin(theta)), runtime.newFloat(jx * Math.sin(theta) + jy * Math.cos(theta))}; return Vec2.rbNew(context, this.getMetaClass(), ary); }
private static long getTimeoutFromArg(IRubyObject timeArg, Ruby runtime) { long timeout = 0; if (timeArg instanceof RubyFloat) { timeout = Math.round(((RubyFloat) timeArg).getDoubleValue() * 1000); } else if (timeArg instanceof RubyFixnum) { timeout = Math.round(((RubyFixnum) timeArg).getDoubleValue() * 1000); } else { // TODO: MRI also can hadle Bignum here throw runtime.newTypeError("can't convert " + timeArg.getMetaClass().getName() + " into time interval"); } if (timeout < 0) { throw runtime.newArgumentError("negative timeout given"); } return timeout; }
private static long getTimeoutFromArg(IRubyObject timeArg, Ruby runtime) { long timeout = 0; if (timeArg instanceof RubyFloat) { timeout = Math.round(((RubyFloat) timeArg).getDoubleValue() * 1000); } else if (timeArg instanceof RubyFixnum) { timeout = Math.round(((RubyFixnum) timeArg).getDoubleValue() * 1000); } else { // TODO: MRI also can hadle Bignum here throw runtime.newTypeError("can't convert " + timeArg.getMetaClass().getName() + " into time interval"); } if (timeout < 0) { throw runtime.newArgumentError("negative timeout given"); } return timeout; }
void init(ThreadContext context, IRubyObject... args) { if (args.length == 2) { jx = (args[0] instanceof RubyFloat) ? ((RubyFloat) args[0]).getValue() : ((RubyFixnum) args[0]).getDoubleValue(); jy = (args[1] instanceof RubyFloat) ? ((RubyFloat) args[1]).getValue() : ((RubyFixnum) args[1]).getDoubleValue(); } // allow ruby ducktyping in constructor if (args.length == 1) { if (!(args[0].respondsTo("x"))) { throw context.runtime.newTypeError(args[0].getType() + " doesn't respond_to :x & :y"); } jx = ((args[0].callMethod(context, "x")) instanceof RubyFloat) ? ((RubyFloat) args[0].callMethod(context, "x")).getValue() : ((RubyFixnum) args[0].callMethod(context, "x")).getDoubleValue(); jy = ((args[0].callMethod(context, "y")) instanceof RubyFloat) ? ((RubyFloat) args[0].callMethod(context, "y")).getValue() : ((RubyFixnum) args[0].callMethod(context, "y")).getDoubleValue(); } }
/** * * @param context ThreadContext * @param other IRubyObject scalar * @return new Vec2D object (ruby) */ @JRubyMethod(name = "/", required = 1) public IRubyObject op_div(ThreadContext context, IRubyObject other) { Ruby runtime = context.runtime; double scalar = (other instanceof RubyFloat) ? ((RubyFloat) other).getValue() : ((RubyFixnum) other).getDoubleValue(); if (Math.abs(scalar) < Vec2.EPSILON) { return this; } return Vec2.rbNew(context, this.getMetaClass(), new IRubyObject[]{ runtime.newFloat(jx / scalar), runtime.newFloat(jy / scalar)}); }
/** * * @param context ThreadContext * @param scalar IRubyObject * @return new Vec3 object (ruby) */ @JRubyMethod(name = "/", required = 1) public IRubyObject op_div(ThreadContext context, IRubyObject scalar) { Ruby runtime = context.runtime; double divisor = (scalar instanceof RubyFloat) ? ((RubyFloat) scalar).getValue() : ((RubyFixnum) scalar).getDoubleValue(); if (Math.abs(divisor) < Vec3.EPSILON) { return this; } return Vec3.rbNew(context, this.getMetaClass(), new IRubyObject[]{ runtime.newFloat(jx / divisor), runtime.newFloat(jy / divisor), runtime.newFloat(jz / divisor)}); }
/** * * @param context ThreadContext * @param other IRubyObject scalar * @return new Vec2D object (ruby) */ @JRubyMethod(name = "*") public IRubyObject op_mul(ThreadContext context, IRubyObject other) { Ruby runtime = context.runtime; double scalar = (other instanceof RubyFloat) ? ((RubyFloat) other).getValue() : ((RubyFixnum) other).getDoubleValue(); return Vec2.rbNew(context, this.getMetaClass(), new IRubyObject[]{runtime.newFloat(jx * scalar), runtime.newFloat(jy * scalar)}); }
/** * * @param context ThreadContext * @param scalar IRubyObject * @return new Vec3 object (ruby) */ @JRubyMethod(name = "*", required = 1) public IRubyObject op_mul(ThreadContext context, IRubyObject scalar) { Ruby runtime = context.runtime; double multi = (scalar instanceof RubyFloat) ? ((RubyFloat) scalar).getValue() : ((RubyFixnum) scalar).getDoubleValue(); return Vec3.rbNew(context, this.getMetaClass(), new IRubyObject[]{ runtime.newFloat(jx * multi), runtime.newFloat(jy * multi), runtime.newFloat(jz * multi)}); }