Clamp function

Maybe, what’s the definition?

They said it earlier on.

yup, all I did was rearrange the inputs.

Yeah that isn’t it, there’s no mathematical equivalent to it that I can find making it difficult to explain

It would help if you gave us 3 examples, when the value is less than the minimum, when the value is inside the range, and when the value is above the maximum.

The clamp function you're describing can be written as the following, correct?

If a value is less than a minimum, return the minimum.
If the value is greater than a maximum, return the maximum.
Otherwise, return the value.

Ego-lay's block does that: it returns the larger out of the minimum and the value and then the smaller of that and the maximum.

No it’s not a constrain function, it’s mapping between 2 values

Do you want something like this?

Ya that’s exactly what I need but with only 1 axis

FIgure out how wide your input domain is. (Max minus min.)

Take the input value you're interested in and subtract the min of the domain. Divide by the width, and you have a number between 0 and 1 that tells you what fraction of the way your value is.

Figure out how wide the output range is. (f(max) minus f(min))

Now take your fraction of the width of the range, add f(min), and you're all set.

Is that what you need?

This?


Yeah I can see why I had no clue where to start

The block you’re proposing has three terms. The first and third term are equal, and annihilate each other. So effectively only the second term is used. It boils down to:

clamp (value, minimum, maximum) = minimum · (value - minimum) / (maximum - minimum)

… and I don’t have a clue as to what that represents.

Long story short: @ego-lay_atman-bay’s formula is correct, unless min > max.

Yeah I didn’t do a good job at showing it… that definitely let to confusion

If, however, you are looking to rescale a value from a range (a .. b) to another range (c .. d), you will need a whole different formula:

untitled script pic 138

… and of course it’s the same as @bh suggested.

That’s what I’m pretty sure bh’s does but it’s definitely a lot and you’d be able to find a better way to do it with snaps mechanics

What @bh suggested is simple enough.

I wouldn’t call this simple..

Neither would I. But then I was referring to posts #18 (@bh) and #22 (@qw23).

That's because you aren't taking advantage of procedural abstraction. So, for example, you have all those calls to MAX and MIN. Instead, do this:

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.