EzDevInfo.com

Clamp.js

Clamps an HTML element by adding ellipsis to it if the content inside is too long. Introducing Clamp.js — Joe Schmitt introducing clamp.js. javascript for when experimental, undocumented css properties just aren't getting it done. — joe schmitt

Problem with my clamp macro

I have a problem with my clamp macro, when when my value is over 10 and my high is over 17 it stops working. Any idea?

#define CLAMP(value, low, high) (((value)<(low))?(low):(((value)>(high))?(high):(value)))

Source: (StackOverflow)

Problems limiting object rotation with Mathf.Clamp()

I am working on a game that rotates an object on the z axis. I need to limit the total rotation to 80 degrees. I tried the following code, but it doesn't work. minAngle = -40.0f and maxAngle = 40.0f

Vector3 pos = transform.position;
pos.z = Mathf.Clamp(pos.z, minAngle, maxAngle);
transform.position = pos;

Source: (StackOverflow)

Advertisements

Where can I find the "clamp" function in .NET?

I would like to clamp a value x to a range [a, b]:

x = (x < a) ? a : ((x > b) ? b : x);

This is quite basic. But I do not see a function "clamp" in the class library - at least not in System.Math.

(For the unaware to "clamp" a value is to make sure that it lies between some maximum and minimum values. If it’s greater than the max value, then it’s replaced by the max, etc.)


Source: (StackOverflow)

Does java have a clamp function?

Suppose I have a value, I usually do this to "clamp" it to a range, here the range [0..1]. That is if it is below the range start, increase it to the range start, it above the range end, reduce it to the range end.

clampedValue = Math.max(0, Math.min(1, value));

Is there any built in function for clamping to a range?


Source: (StackOverflow)

Clamping floating numbers in Python?

Is there a built-in function for this in Python 2.6?

Something like:

clamp(myValue, min, max)

Source: (StackOverflow)

Is there a limit/clamp function in ruby

I wrote the following code, which keeps x within the range [a,b] (if x < a, x = a; if x > b, x = b):

x = [a, [x, b].min].max

As it is quiet a basic and useful function, I was wondering if there is a native method to do that in ruby.

Update As there is apparently no method to do it, what would be the shortest/more readable way to do it? I found

x = [a,x,b].sort[1]

so far, but am not sure if it is more readable.


Source: (StackOverflow)

How to clamp an integer to some range? (in Python)

I have the following code:

new_index = index + offset
if new_index < 0:
    new_index = 0
if new_index >= len(mylist):
    new_index = len(mylist) - 1
return mylist[new_index]

Basically, I calculate a new index and use that to find some element from a list. In order to make sure the index is inside the bounds of the list, I needed to write those 2 if statements spread into 4 lines. That's quite verbose, a bit ugly... Dare I say, it's quite un-pythonic.

Is there any other simpler and more compact solution? (and more pythonic)

Yes, i know I can use if else in one line, but it is not readable:

new_index = 0 if new_index < 0 else len(mylist) - 1 if new_index >= len(mylist) else new_index

I also know I can chain max() and min() together. It's more compact, but I feel it's kinda obscure, more difficult to find bugs if I type it wrong. In other words, I don't find it very straightforward.

new_index = max(0, min(new_index, len(mylist)-1))

Source: (StackOverflow)

Fastest way to clamp a real (fixed/floating point) value?

Is there a more efficient way to clamp real numbers than using if statements or ternary operators? I want to do this both for doubles and for a 32-bit fixpoint implementation (16.16). I'm not asking for code that can handle both cases; they will be handled in separate functions.

Obviously, I can do something like:

double clampedA;
double a = calculate();
clampedA = a > MY_MAX ? MY_MAX : a;
clampedA = a < MY_MIN ? MY_MIN : a;

or

double a = calculate();
double clampedA = a;
if(clampedA > MY_MAX)
    clampedA = MY_MAX;
else if(clampedA < MY_MIN)
    clampedA = MY_MIN;

The fixpoint version would use functions/macros for comparisons.

This is done in a performance-critical part of the code, so I'm looking for an as efficient way to do it as possible (which I suspect would involve bit-manipulation)

EDIT: It has to be standard/portable C, platform-specific functionality is not of any interest here. Also, MY_MIN and MY_MAX are the same type as the value I want clamped (doubles in the examples above).


Source: (StackOverflow)

Clip values between a minimum and maximum allowed value in R

In Mathematica there is the command Clip[x, {min, max}] which gives x for min<=x<=max, min for x<min and and max for x>max, see

http://reference.wolfram.com/mathematica/ref/Clip.html

What would be the equivalent command for this in R? Ideally it should be a function that is listable, and that would either take a single value, or vector or a matrix as an argument? I'm sure it must be easy, but I just couldn't find it anywhere... (and my own function I wrote to do this is a bit slow)

cheers, Tom


Source: (StackOverflow)

Most efficient way to clamp values in an OpenCv Mat

I have an OpenCv Mat that I'm going to use for per-pixel remapping, called remap, that has CV_32FC2 elements.

Some of these elements might be outside of the allowed range for the remap. So I need to clamp them between Point2f(0, 0) and Point2f(w, h). What is the shortest, or most efficient, way of accomplishing this with OpenCv 2.x?

Here's one solution:

void clamp(Mat& mat, Point2f lowerBound, Point2f upperBound) {
    vector<Mat> matc;
    split(mat, matc);
    min(max(matc[0], lowerBound.x), upperBound.x, matc[0]);
    min(max(matc[1], lowerBound.y), upperBound.y, matc[1]);
    merge(matc, mat);   
}

But I'm not sure if it's the shortest, or if split/merge is efficient.


Source: (StackOverflow)

forcing the columns of a matrix within different limits

I have a matrix named l having size 20X3. What I wanted to do was this : Suppose I have this limits:

l1_max=20; l1_min=0.5;
l2_max=20; l2_min=0.5;
mu_max=20; mu_min=0.5;

I wanted to force all the elements of the matrix l within the limits. The values of 1st column within l1_max & l1_min. The values of 2nd column within l2_max & l2_min. The values of 3rd column within mu_max & mu_min.

What I did was like this:

for k=1:20
    if l(k,1)>l1_max 
        l(k,1) = l1_max;
    elseif l(k,1)<l1_min
        l(k,1) = l1_min;
    end

    if l(k,2)>l2_max 
        l(k,2) = l2_max;
    elseif l(k,2)<l2_min
        l(k,2) = l2_min;
    end

    if l(k,3)>mu_max 
        l(k,3) = mu_max;
    elseif l(k,3)<mu_min
        l(k,3) = mu_min;
    end
end

Can it be done in a better way ?


Source: (StackOverflow)

Mathhelper.Clamp not exact?

I'm running into an issue while using Mathhelper.Clamp in a game I'm working on. Here's a snippet of the code that's causing me problems:

if (background.position.Y == 0) 
{
    player.position.Y = MathHelper.Clamp(player.position.Y, 0, viewport.Height / 2);
}
if (Math.Abs(background.position.Y) == background.sprite.Height - viewport.Height / 2)
{
    player.position.Y = MathHelper.Clamp(player.position.Y, viewport.Height / 2, viewport.Height - player.sprite.Height);
}
if (player.position.Y == viewport.Height / 2) 
{
    background.position.Y = MathHelper.Clamp(background.position.Y, -(background.sprite.Height - viewport.Height / 2), 0);
}

Essentially, what I'm hoping to accomplish is (starting at x,0) let the player sprite move down until it reaches the middle of the screen, then the background will move instead, until it reaches the bottom of the background minus half the height of the screen, then the player sprite will move again until it reaches the bottom of the screen. (I have code to make this happen) - To me, this seems like pretty typical camera functionality for a 2D sidescroller.

However, the problem I'm having is that the positions aren't clamping exactly at the numbers they should be, which means that the code that relies on them being so specifically placed is breaking as well. To be more specific, it looks like one extra frame worth of movement is being allowed beyond the clamp point.

Can anyone tell me if there's a reason why this shouldn't work, or what I can do to make it work? Thanks in advance!


Source: (StackOverflow)

How to properly clamp beckmann distribution

I am trying to implement a Microfacet BRDF shading model (similar to the Cook-Torrance model) and I am having some trouble with the Beckmann Distribution defined in this paper: https://www.cs.cornell.edu/~srm/publications/EGSR07-btdf.pdf

enter image description here

Where M is a microfacet normal, N is the macrofacet normal and ab is a "hardness" parameter between [0, 1].

My issue is that this distribution often returns obscenely large values, especially when ab is very small.

For instance, the Beckmann distribution is used to calculate the probability of generating a microfacet normal M per this equation :

enter image description here

A probability has to be between the range [0,1], so how is it possible to get a value within this range using the function above if the Beckmann distribution gives me values that are 1000000000+ in size?

So there a proper way to clamp the distribution? Or am I misunderstanding it or the probability function? I had tried simply clamping it to 1 if the value exceeded 1 but this didn't really give me the results I was looking for.


Source: (StackOverflow)

Clamping a vector to a minimum and maximum?

I came accross this: t = Clamp(t/d, 0, 1) but I'm not sure how to perform this operation on a vector. What are the steps to clamp a vector if one was writing their own vector implementation?

Thanks

clamp clamping a vector to a minimum and a maximum

ex:

pc = # the point you are coloring now
p0 = # start point
p1 = # end point
v = p1 - p0
d = Length(v)
v = Normalize(v) # or Scale(v, 1/d)

v0 = pc - p0

t = Dot(v0, v)
t = Clamp(t/d, 0, 1)

color = (start_color * t) + (end_color * (1 - t))

Source: (StackOverflow)

Does a "clamp" number function exist in PHP?

I wrote a function to "clamp" numbers in PHP, but I wonder if this function exists natively in the language.

I read PHP.net documentation in the math section, but I couldn't find it.

Basically what my function does is that it accepts a variable, an array of possible values, and a default value, this is my function's signature:

function clamp_number($value, $possible_values, $default_value)

If $value does not match any of the $possible_values then it defaults to $default_value

I think my function would be way faster if PHP already provides it natively because I'm using quite often in my program.

Anyways if this question does not belong to SO, feel free to vote to close it.


Source: (StackOverflow)