This article has multiple issues. Please help improve it or discuss these issues on the talk page. (Learn how and when to remove these template messages) This article does not cite any sources. Please help improve this article by adding citations to reliable sources. Unsourced material may be challenged and removed.Find sources: "Wrapping" graphics – news · newspapers · books · scholar · JSTOR (December 2009) (Learn how and when to remove this message) This article provides insufficient context for those unfamiliar with the subject. Please help improve the article by providing more context for the reader. (June 2017) (Learn how and when to remove this message) (Learn how and when to remove this message)

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 polygon, eliminating the need for large textures or multiple polygons.

To wrap a position x to an area of width w, calculate the value .

Implementation

For computational purposes the wrapped value x' of x can be expressed as

where is the highest value in the range, and is the lowest value in the range.

Pseudocode for wrapping of a value to a range other than 0–1 is

function wrap(X, Min, Max: Real): Real;
    X := X - Int((X - Min) / (Max - Min)) * (Max - Min);
    if X < 0 then // This corrects the problem caused by using Int instead of Floor
        X := X + Max - Min;
    return X;

Pseudocode for wrapping of a value to a range of 0–1 is

function wrap(X: Real): Real;
    X := X - Int(X);
    if X < 0 then
        X := X + 1;
    return X;

Pseudocode for wrapping of a value to a range of 0–1 without branching is,

function wrap(X: Real): Real;
    return ((X mod 1.0) + 1.0) mod 1.0;

See also text wrapping