0

我正在查看这个问题以获取有关为给定范围实施约束/换行的想法。

从中我可以确定以下内容

[n1, n2)

float Wrap(float x, float lo, float hi)
{
    return x % Math.Abs(lo - hi);
}

仅适用于正数,因此建议这样做

float Constrain(float x, float lo, float hi)
{
    float t = (x - lo) % (hi - lo);

    return t < 0 ? t + hi : t + lo;
}

我仍然不确定如何从上面的代码中获得以下范围约束并需要一些帮助?

[n1, n2]

(n1, n2]

4

1 回答 1

2

(n1, n2]

float constrainExclusiveInclusive(float x, float lo, float hi){
    float result = Constrain(x, lo, hi);
    if (result == lo){result = hi;}
    return result;
}

[n1, n2]

float constrainInclusiveInclusive(float x, float lo, float hi){
    float result = Constrain(x, lo, hi);
    if (result == lo){
        //somehow decide whether you want x to wrap to lo or hi
        if (moonIsInTheSeventhHouse()){
            result = lo;
        }
        else{
            result = hi;
        }
    }
    return result;
}

(n1, n2)

float constrainExclusiveExclusive(float x, float lo, float hi){
    float result = Constrain(x, lo, hi);
    if (result == lo){
        throw new ArgumentException("No answer exists for the given inputs");
    }
    return result;
}
于 2012-07-19T12:49:37.270 回答