Search in sources :

Example 11 with RubyFloat

use of org.jruby.RubyFloat in project propane by ruby-processing.

the class Vec2 method rotate.

/**
 * @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);
}
Also used : IRubyObject(org.jruby.runtime.builtin.IRubyObject) Ruby(org.jruby.Ruby) RubyFloat(org.jruby.RubyFloat) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 12 with RubyFloat

use of org.jruby.RubyFloat in project propane by ruby-processing.

the class Vec3 method op_div.

/**
 * @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) });
}
Also used : Ruby(org.jruby.Ruby) RubyFloat(org.jruby.RubyFloat) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 13 with RubyFloat

use of org.jruby.RubyFloat in project propane by ruby-processing.

the class MathToolModule method constrainValue.

/**
 * Provides processing constrain method as a ruby module method
 *
 * @param context ThreadContext
 * @param recv IRubyObject
 * @param args array of args must be be numeric
 * @return original or limit values
 */
@JRubyMethod(name = "constrain", rest = true, module = true)
public static IRubyObject constrainValue(ThreadContext context, IRubyObject recv, IRubyObject[] args) {
    RubyFloat value = args[0].convertToFloat();
    RubyFloat start = args[1].convertToFloat();
    RubyFloat stop = args[2].convertToFloat();
    if (value.op_ge(context, start).isTrue() && value.op_le(context, stop).isTrue()) {
        return args[0];
    } else if (value.op_ge(context, start).isTrue()) {
        return args[2];
    } else {
        return args[1];
    }
}
Also used : RubyFloat(org.jruby.RubyFloat) JRubyMethod(org.jruby.anno.JRubyMethod)

Aggregations

RubyFloat (org.jruby.RubyFloat)13 JRubyMethod (org.jruby.anno.JRubyMethod)13 Ruby (org.jruby.Ruby)9 RubyRange (org.jruby.RubyRange)2 JRender (monkstone.vecmath.JRender)1 Vec2 (monkstone.vecmath.vec2.Vec2)1 RubyFixnum (org.jruby.RubyFixnum)1 IRubyObject (org.jruby.runtime.builtin.IRubyObject)1