cables-gl / cables_docs

cables documentation docs.cables.gl
https://cables.gl/docs/docs
45 stars 16 forks source link

BUG ArrayNumberRamp #887

Open franckaubry opened 2 weeks ago

franckaubry commented 2 weeks ago

BUG:

When having two positive numbers, the values are being shifted

image

Possible fix:

const
    inStart = op.inFloat("Start Value", 0),
    inEnd = op.inFloat("End Value", 1),
    inNum = op.inInt("Entries", 10),
    outArr = op.outArray("Result");

inStart.onChange =
    inEnd.onChange =
    inNum.onChange = update;

update();

function update()
{
    const start = Math.min(inStart.get(), inEnd.get());
    const end = Math.max(inStart.get(), inEnd.get());

    const num = Math.floor(Math.max(0, inNum.get()));

    const arr = [];

    if (num == 0) return outArr.setRef([]);

    // Corrected distance calculation
    const dist = end - start;
    const step = dist / (num - 1);
    arr.length = num;

    for (let i = 0; i < num; i++)
    {
        arr[i] = i * step + start;
    }

    outArr.setRef(arr);
}

image

OP functionality alternative:

Also it seems a bit "restrictive" to let the output always start with the smallest number. image

Wouldn't it make more sense to just have a simple calculation like: Start Value = Actual start value of array End Value = Actual end value of the array

Then the user can choose what the start/end value can be, no matter which is smaller/larger? image

Possible fix:

const
    inStart = op.inFloat("Start Value", 0),
    inEnd = op.inFloat("End Value", 1),
    inNum = op.inInt("Entries", 10),
    outArr = op.outArray("Result");

inStart.onChange =
    inEnd.onChange =
    inNum.onChange = update;

update();

function update()
{
    const start = inStart.get();
    const end = inEnd.get();

    const num = Math.floor(Math.max(0, inNum.get()));

    if (num === 0) {
        outArr.setRef([]);
        return;
    }

    const arr = [];

    if (num === 1) {
        arr.push(start); // Or choose to push 'end' if preferred
        outArr.setRef(arr);
        return;
    }

    const dist = end - start;
    const step = dist / (num - 1);

    for (let i = 0; i < num; i++)
    {
        arr[i] = start + i * step;
    }

    outArr.setRef(arr);
}

Patch with new codes: https://cables.gl/edit/oi8nGs

pandrr commented 2 weeks ago

thank you !yes makes total sense, this is how the op should work...

i creaated a new version: https://github.com/cables-gl/cables/tree/develop/src/ops/base/Ops.Array.ArrayNumberRamp_v2

do you want to create a pull request with your bottom code ? just paste it into this js file