Jump to content

Clamp (function): Difference between revisions

From Wikipedia, the free encyclopedia
m J. Geerink moved page Clamping (graphics) to Clamping (function) over redirect: Perform requested move, see talk page: It is (regrettably, according to some) the name of a (mathematical? numerical?) function which has applications in computer graphics, but is in no way limited to that field. I have chosen (function) over (computer science) because; it is slightly shorter, and, while being a bit more vague, it acknowledges that it isn't really a computer (science) specific "thing", jus...
Tags: Mobile edit Mobile web edit
m J. Geerink moved page Clamping (function) to Clamp (function): Fix grammar
Tags: Mobile edit Mobile web edit
 
(No difference)

Latest revision as of 09:43, 25 December 2024

In computer science, clamping, or clipping is the process of limiting a value to a range between a minimum and a maximum value. Unlike wrapping, clamping merely moves the point to the nearest available value.

Y = clamp(X, 1, 3)
X Y
0 1
1 1
2 2
3 3
4 3

In Python, clamping can be defined as follows:

def clamp(x, minimum, maximum):
    if x < minimum:
        return minimum
    if x > maximum:
        return maximum
    return x

This is equivalent to max(minimum, min(x, maximum)) for languages that support the functions min and max.

Uses

[edit]

Several programming languages and libraries provide functions for fast and vectorized clamping. In Python, the pandas library offers the Series.clip[1] and DataFrame.clip[2] methods. The NumPy library offers the clip[3] function. In the Wolfram Language, it is implemented as Clip[x, {minimum, maximum}].[4]

In OpenGL, the glClearColor function takes four GLfloat values which are then 'clamped' to the range .[5]

One of the many uses of clamping in computer graphics is the placing of a detail inside a polygon—for example, a bullet hole on a wall. It can also be used with wrapping to create a variety of effects.

References

[edit]
  1. ^ "Pandas Series.clip method documentation". Retrieved 2023-10-15.
  2. ^ "Pandas DataFrame.clip method documentation". Retrieved 2023-10-15.
  3. ^ "NumPy clip function documentation". Retrieved 2023-10-15.
  4. ^ "Wolfram Language Clip funcion documentation". Retrieved 2023-10-15.
  5. ^ "OpenGL 4 Reference Pages". www.khronos.org. Retrieved 2018-10-31.