1

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))
9
  • 1
    Explain further what the function should do.
    – anon
    Jun 9, 2010 at 19:38
  • What do you mean by clamping a vector? Clamping each entry of the vector? Clamping the vector itself to a sphere in R^n?
    – user168715
    Jun 9, 2010 at 19:39
  • Clamping a vector to a minimum and a maximum
    – jmasterx
    Jun 9, 2010 at 19:39
  • 1
    @user146780: Maximum and minimum what? Number of elements? Value of all elements contained whithin? Memory allocated? Jun 9, 2010 at 19:40
  • t and d are both scalars here. Why do you need to clamp a vector? Just use the code Chris posted below, on the scalar t/d with lb = 0 and ub = 1.
    – user168715
    Jun 9, 2010 at 19:41

4 Answers 4

5

I think that once you state clearly what you mean you'll find that most of the work is done for you...

I'm guessing you want to limit the length of the vector quantity (rather than a vector data structure) to lie within a specified range without changing its direction, no?

So:

if (v.length > max)
   v.setlength(max)
else if (v.length < min)
   v. setlength(min)

where the implementation of length() and setlength() depend on how you have stored your vector.


If your vector is stored in (angle,magnitude) form this is nearly trivial. If stored in Cartesian form (ie. (x,y,z) ) you get length from the Pythagorian theorem and setlength should scale each commponent by a factor of desired_length/current_length.

4
clamp(vec, lb, ub) == min(max(vec, lb), ub)

edit

min and max are usually primitive operations on vectors. For example, if you're using SSE vectors, there are _mm_min_ps and _mm_max_ps intrinsics that turn into MINPS and MAXPS instructions on x86.

4
  • What are the min and max functions?
    – jmasterx
    Jun 9, 2010 at 19:40
  • 1
    @user146780: That's what everyone's asking you in the comments to your question!
    – Cascabel
    Jun 9, 2010 at 19:41
  • min and max are defined in the c++ library in <algorithm> Jun 9, 2010 at 19:43
  • And they assume bool operator<(T, T) exists. Typically they don't exist for a vector. Is (-1,0,0) < (0.5,0,0) ?
    – MSalters
    Jun 10, 2010 at 9:15
0

The easiest answer is when you consider a vector in a spherical coordinate system: {R, φ, θ}. φ and θ are already limited to [-π,+π] and [-½π, +½π] anyway. Hence, you only need to clamp the distance from the origin R.

-3

Well, I'd assuming you'd want to clamp each of the coordinates individually. So...

void clamp(const Vector3 &v, const Vector3 &min, const Vector3 &max)
{
    v.x = clamp(v.x, min.x, max.x);
    v.y = clamp(v.y, min.y, max.y);
    v.z = clamp(v.z, min.z, max.z);
}

int clamp(int value, int min, int max)
{
    if (value < min)
    {
        return min;
    }
    else if (value > max)
    {
        return max;
    }

    return value;
}

Hope that helps.

3
  • 4
    Clamping coordinates independently may change the direction of the vector, which is not how I interpreted the request. Jun 9, 2010 at 19:45
  • I was thinking of a vector in the context of 3d graphics. A clamp function could be useful there in ensuring a point remains within a cube. I didn't really know what he was asking. Jun 9, 2010 at 19:53
  • 2
    Even if you wanted to make sure that the vector remains in a cube (or a sphere) there's still the question whether you want to keep the direction.
    – MSalters
    Jun 10, 2010 at 9:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.