Cohen–Sutherland Algorithm
   HOME

TheInfoList



OR:

In
computer graphics Computer graphics deals with generating images with the aid of computers. Today, computer graphics is a core technology in digital photography, film, video games, cell phone and computer displays, and many specialized applications. A great de ...
, the Cohen–Sutherland algorithm is an
algorithm In mathematics and computer science, an algorithm () is a finite sequence of rigorous instructions, typically used to solve a class of specific problems or to perform a computation. Algorithms are used as specifications for performing ...
used for
line clipping In computer graphics, line clipping is the process of removing (clipping) lines or portions of lines outside an area of interest (a viewport or view volume). Typically, any part of a line which is outside of the viewing area is removed. There a ...
. The algorithm divides a two-dimensional space into 9 regions and then efficiently determines the lines and portions of lines that are visible in the central region of interest (the
viewport A viewport is a polygon viewing region in computer graphics. In computer graphics theory, there are two region-like notions of relevance when rendering some objects to an image. In textbook terminology, the '' world coordinate window'' is the area ...
). The algorithm was developed in 1967 during
flight simulator A flight simulator is a device that artificially re-creates aircraft flight and the environment in which it flies, for pilot training, design, or other purposes. It includes replicating the equations that govern how aircraft fly, how they rea ...
work by Danny Cohen and Ivan Sutherland.''Principles of Interactive Computer Graphics'', p. 124, 252, by
Bob Sproull Robert Fletcher "Bob" Sproull (born c. 1945) is an American computer scientist, who worked for Oracle Corporation where he was director of Oracle Labs in Burlington, Massachusetts. He is currently an adjunct professor at the College of Informa ...
and William M. Newman, 1973, McGraw–Hill Education, International edition, .


The algorithm

The algorithm includes, excludes or partially includes the line based on whether: * Both endpoints are in the viewport region (bitwise OR of endpoints = 0000): trivial accept. * Both endpoints share at least one non-visible region, which implies that the line does not cross the visible region. (bitwise AND of endpoints ≠ 0000): trivial reject. * Both endpoints are in different regions: in case of this nontrivial situation the algorithm finds one of the two points that is outside the viewport region (there will be at least one point outside). The intersection of the outpoint and extended viewport border is then calculated (i.e. with the parametric equation for the line), and this new point replaces the outpoint. The algorithm repeats until a trivial accept or reject occurs. The numbers in the figure below are called outcodes. An outcode is computed for each of the two points in the line. The outcode will have 4 bits for two-dimensional clipping, or 6 bits in the three-dimensional case. The first bit is set to 1 if the point is above the viewport. The bits in the 2D outcode represent: top, bottom, right, left. For example, the outcode 1010 represents a point that is top-right of the viewport. : Note that the outcodes for endpoints ''must'' be recalculated on each iteration after the clipping occurs. The Cohen–Sutherland algorithm can be used only on a rectangular
clip window This is a glossary of terms relating to computer graphics Computer graphics deals with generating images with the aid of computers. Today, computer graphics is a core technology in digital photography, film, video games, cell phone and comp ...
.


Example C/C++ implementation

typedef int OutCode; const int INSIDE = 0; // 0000 const int LEFT = 1; // 0001 const int RIGHT = 2; // 0010 const int BOTTOM = 4; // 0100 const int TOP = 8; // 1000 // Compute the bit code for a point (x, y) using the clip rectangle // bounded diagonally by (xmin, ymin), and (xmax, ymax) // ASSUME THAT xmax, xmin, ymax and ymin are global constants. OutCode ComputeOutCode(double x, double y) // Cohen–Sutherland clipping algorithm clips a line from // P0 = (x0, y0) to P1 = (x1, y1) against a rectangle with // diagonal from (xmin, ymin) to (xmax, ymax). bool CohenSutherlandLineClip(double& x0, double& y0, double& x1, double& y1)


Notes


See also

Algorithms used for the same purpose: * Liang–Barsky algorithm * Cyrus–Beck algorithm * Nicholl–Lee–Nicholl algorithm * Fast clipping


References

* James D. Foley.
Computer graphics: principles and practice
'. Addison-Wesley Professional, 1996. p. 113.


External links


JavaScript polyline clipping library using Cohen-Sutherland algorithm



Delphi implementation

Stata implementation
{{DEFAULTSORT:Cohen-Sutherland algorithm Line clipping algorithms