Search in sources :

Example 1 with RubyFloat

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

the class MathToolModule method norm_strict.

/**
 * Identical to p5map(value, low, high, 0, 1) but 'clamped'. Numbers outside
 * of the range are clamped to 0 and 1,
 *
 * @param context ThreadContext
 * @param recv IRubyObject
 * @param args array of args must be be numeric
 * @return mapped value RubyFloat
 */
@JRubyMethod(name = "norm_strict", rest = true, module = true)
public static IRubyObject norm_strict(ThreadContext context, IRubyObject recv, IRubyObject[] args) {
    Ruby ruby = context.runtime;
    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();
    double max = Math.max(start, stop);
    double min = Math.min(start, stop);
    if (value < min) {
        return mapMt(context, min, start, stop, 0, 1.0);
    }
    if (value > max) {
        return mapMt(context, max, start, stop, 0, 1.0);
    }
    return mapMt(context, value, start, stop, 0, 1.0);
}
Also used : Ruby(org.jruby.Ruby) RubyFloat(org.jruby.RubyFloat) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 2 with RubyFloat

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

the class Vec2 method from_angle.

/**
 * Example of a regular ruby class method Use Math rather than RadLut
 * here!!!
 *
 * @param context ThreadContext
 * @param klazz IRubyObject
 * @param scalar input angle in radians
 * @return new Vec2 object (ruby)
 */
@JRubyMethod(name = "from_angle", meta = true)
public static IRubyObject from_angle(ThreadContext context, IRubyObject klazz, IRubyObject scalar) {
    Ruby runtime = context.runtime;
    double angle = (scalar instanceof RubyFloat) ? ((RubyFloat) scalar).getValue() : ((RubyFixnum) scalar).getDoubleValue();
    return Vec2.rbNew(context, klazz, new IRubyObject[] { runtime.newFloat(Math.cos(angle)), runtime.newFloat(Math.sin(angle)) });
}
Also used : Ruby(org.jruby.Ruby) RubyFloat(org.jruby.RubyFloat) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 3 with RubyFloat

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

the class Vec2 method lerp_bang.

/**
 * @param context ThreadContext
 * @param args IRubyObject[]
 * @return this IRubyObject
 */
@JRubyMethod(name = "lerp!", rest = true)
public IRubyObject lerp_bang(ThreadContext context, IRubyObject[] args) {
    Ruby runtime = context.runtime;
    if (args.length != 2) {
        throw runtime.newSyntaxError("Check syntax");
    }
    Vec2 vec = (Vec2) args[0].toJava(Vec2.class);
    double scalar = (args[1] instanceof RubyFloat) ? ((RubyFloat) args[1]).getValue() : ((RubyFixnum) args[1]).getDoubleValue();
    assert (scalar >= 0 && scalar < 1.0) : "Lerp value " + scalar + " out of range 0..1.0";
    jx += (vec.jx - jx) * scalar;
    jy += (vec.jy - jy) * scalar;
    return this;
}
Also used : Ruby(org.jruby.Ruby) RubyFloat(org.jruby.RubyFloat) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 4 with RubyFloat

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

the class Vec2 method op_mul.

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

Example 5 with RubyFloat

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

the class MathToolModule method mapOneD.

/**
 * @param context ThreadContext
 * @param recv IRubyObject
 * @param args array of RubyRange (must be be numeric)
 * @return mapped value RubyFloat
 */
@JRubyMethod(name = "map1d", rest = true, module = true)
public static IRubyObject mapOneD(ThreadContext context, IRubyObject recv, IRubyObject[] args) {
    double value = args[0] instanceof RubyFloat ? ((RubyFloat) args[0]).getValue() : ((RubyFixnum) args[0]).getDoubleValue();
    RubyRange r1 = (RubyRange) args[1];
    RubyRange r2 = (RubyRange) args[2];
    double first1 = r1.first(context) instanceof RubyFloat ? ((RubyFloat) r1.first(context)).getValue() : ((RubyFixnum) r1.first(context)).getDoubleValue();
    double first2 = r2.first(context) instanceof RubyFloat ? ((RubyFloat) r2.first(context)).getValue() : ((RubyFixnum) r2.first(context)).getDoubleValue();
    double last1 = r1.last(context) instanceof RubyFloat ? ((RubyFloat) r1.last(context)).getValue() : ((RubyFixnum) r1.last(context)).getDoubleValue();
    double last2 = r2.last(context) instanceof RubyFloat ? ((RubyFloat) r2.last(context)).getValue() : ((RubyFixnum) r2.last(context)).getDoubleValue();
    return mapMt(context, value, first1, last1, first2, last2);
}
Also used : RubyRange(org.jruby.RubyRange) 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