# Clamping (graphics)

﻿
Clamping (graphics)

In computer graphics, clamping is the process of limiting a position to an area. Unlike wrapping, clamping merely moves the point to the nearest available value.
To put clamping into perspective, pseudocode for clamping is:

``` function clamp(X, Min, Max:Real):Real;
if X > Max then
X := Max;
if X < Min then
X := Min;
return X;
```

In JavaScript:

``` function clamp(value, min, max) {
return Math.min(Math.max(value, min), max);
};
```

A faster[1] one in JavaScript using ternary operators:

``` function clamp(value, min, max) {
return value < min ? min : value > max ? max : value;
}
```

C and C++: (int can be changed into any other datatype):

``` int clamp(int X, int Min, int Max)
{
if( X > Max )
X = Max;
else if( X < Min )
X = Min;

return X;
}
```

or

``` int clamp(int X, int Min, int Max) {
return ( X > Max ) ? Max : ( X < Min ) ? Min : X;
}
```

or

``` int clamp(int X, int Min, int Max)
{
if ( X > Max ) return Max;
if ( X < Min ) return Min;

return X;
}
```

or, using templates to cover all data types (C++ only)

``` template<typename T>
T clamp(T Value, T Min, T Max)
{
return (Value < Min)? Min : (Value > Max)? Max : Value;
}
```

## Uses

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

Can also mean clamped to a certain value. For example, in OpenGL, the glClearColor takes a GLclampf value which is a gl float value 'clamped' to the range [0,1].

## References

1. ^ "jsPerf - clamp functions". jsPerf - clamp functions.

Wikimedia Foundation. 2010.

### Look at other dictionaries:

• Texel (graphics) — A texel, or texture element (also texture pixel ) is the fundamental unit of texture space [Andrew Glassner, An Introduction to Ray Tracing , San Francisco: Morgan ndash;Kaufmann, 1989] , used in computer graphics. Textures are represented by… …   Wikipedia

• Wrapping (graphics) — In computer graphics, wrapping is the process of limiting a position to an area. A good example of wrapping is wallpaper, a single pattern repeated indefinitely over a wall. Wrapping is used in 3D computer graphics to repeat a texture over a… …   Wikipedia

• Clamp — may refer to: Clamp (tool), a device used to hold an object in a fixed position Wheel clamp, a device used with road vehicles to prevent theft or enforce parking restrictions Riser clamp, a device used to support vertical piping Storage clamp, an …   Wikipedia

• OpenGL — Original author(s) Silicon Graphics Developer(s) Khronos Group Stable release 4.2 …   Wikipedia

• Compact Disc — CD redirects here. For other uses, see CD (disambiguation). Not to be confused with DVD. Compact disc The readable surface of a Compact Disc incl …   Wikipedia

• OpenGL — Desarrollador Khronos Group www.opengl.org Información general Diseñador Silicon Graphics …   Wikipedia Español

• printing — /prin ting/, n. 1. the art, process, or business of producing books, newspapers, etc., by impression from movable types, plates, etc. 2. the act of a person or thing that prints. 3. words, symbols, etc., in printed form. 4. printed material. 5.… …   Universalium

• Texture mapping — Texture maps redirects here. For the 2003 ambient album, see Texture Maps: The Lost Pieces Vol. 3. 1 = 3D model without textures 2 = 3D model with textures Texture mapping is a method for adding detail, surface texture (a bitmap or raster image) …   Wikipedia

• Numerical control — CNC redirects here. For other uses, see CNC (disambiguation). A CNC Turning Center …   Wikipedia

• Kawasaki KX250 — The KX 250 is a two stroke dirt bike. It has been ridden to several AMA Motocross wins, including a number of titles. Change Log 1998 1999 Liquid Cooled Engine with Kawasaki Integrated Power Valve System * KIPS system controls exhaust port height …   Wikipedia

We are using cookies for the best presentation of our site. Continuing to use this site, you agree with this.