Clamping (function)
   HOME

TheInfoList



OR:

In
computer science Computer science is the study of computation, information, and automation. Computer science spans Theoretical computer science, theoretical disciplines (such as algorithms, theory of computation, and information theory) to Applied science, ...
, clamping, or clipping is the process of limiting a value to a range between a minimum and a maximum value. Unlike
wrapping Wrapping may refer to: *Buddy wrapping, the act of bandaging a damaged (particularly a fractured) finger or toe together with a healthy one *Overwrap, a wrapping of items in a package of a wrapping over packaging *Wrapping (text), a software feat ...
, clamping merely moves the point to the nearest available value. In
Python Python may refer to: Snakes * Pythonidae, a family of nonvenomous snakes found in Africa, Asia, and Australia ** ''Python'' (genus), a genus of Pythonidae found in Africa and Asia * Python (mythology), a mythical serpent Computing * Python (prog ...
, 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 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 Pediatric autoimmune neuropsychiatric disorders associated with streptococcal infections (PANDAS) is a controversial hypothetical diagnosis for a subset of children with rapid onset of obsessive-compulsive disorder (OCD) or tic disorders. Sy ...
library offers the Series.clip and DataFrame.clip methods. The
NumPy NumPy (pronounced ) is a library for the Python programming language, adding support for large, multi-dimensional arrays and matrices, along with a large collection of high-level mathematical functions to operate on these arrays. The predeces ...
library offers the clip function. In the
Wolfram Language The Wolfram Language ( ) is a proprietary, very high-level multi-paradigm programming language developed by Wolfram Research. It emphasizes symbolic computation, functional programming, and rule-based programming and can employ arbitrary stru ...
, it is implemented as . In
OpenGL OpenGL (Open Graphics Library) is a Language-independent specification, cross-language, cross-platform application programming interface (API) for rendering 2D computer graphics, 2D and 3D computer graphics, 3D vector graphics. The API is typic ...
, the glClearColor function takes four GLfloat values which are then 'clamped' to the range ,1/math>. One of the many uses of clamping in
computer graphics Computer graphics deals with generating images and art with the aid of computers. Computer graphics is a core technology in digital photography, film, video games, digital art, cell phone and computer displays, and many specialized applications. ...
is the placing of a detail inside a polygon—for example, a bullet hole on a wall. It can also be used with
wrapping Wrapping may refer to: *Buddy wrapping, the act of bandaging a damaged (particularly a fractured) finger or toe together with a healthy one *Overwrap, a wrapping of items in a package of a wrapping over packaging *Wrapping (text), a software feat ...
to create a variety of effects. In CSS, clamp() can help to implement responsive typography or responsive designs generally. Although spreadsheets like Excel, Open Office Calc, or Google Sheets don't provide a clamping function directly, the same effect can be achieved by using functions like MAX & MIN together, by MEDIAN, or with cell function macros. When attempting to do a clamp where the input is an array, other methods must be used.


References


Further reading

* Computer graphics algorithms Articles with example Python (programming language) code {{Compu-graphics-stub