Misplaced Pages

Clamp (function)

Article snapshot taken from Wikipedia with creative commons attribution-sharealike license. Give it a read and then ask your questions in the chat. We can research this topic together.
(Redirected from Clamping (graphics)) Limiting a position to an area
This article needs additional citations for verification. Please help improve this article by adding citations to reliable sources. Unsourced material may be challenged and removed.
Find sources: "Clamp" function – news · newspapers · books · scholar · JSTOR (December 2009) (Learn how and when to remove this message)

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

Several programming languages and libraries provide functions for fast and vectorized clamping. In Python, the pandas library offers the Series.clip and DataFrame.clip methods. The NumPy library offers the clip function. In the Wolfram Language, it is implemented as Clip.

In OpenGL, the glClearColor function takes four GLfloat values which are then 'clamped' to the range [ 0 , 1 ] {\displaystyle } .

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

  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 function documentation". Retrieved 2023-10-15.
  5. "OpenGL 4 Reference Pages". www.khronos.org. Retrieved 2018-10-31.


Stub icon

This computer graphics–related article is a stub. You can help Misplaced Pages by expanding it.

Categories:
Clamp (function) Add topic