Unit 2 Cg&a
Unit 2 Cg&a
The Cohen-Sutherland line clipping algorithm quickly detects and dispenses with two
common and trivial cases. To clip a line, we need to consider only its endpoints. If both
endpoints of a line lie inside the window, the entire line lies inside the window. It is trivially
accepted and needs no clipping. On the other hand, if both endpoints of a line lie entirely to
one side of the window, the line must lie entirely outside of the window. It is trivially
rejected and needs to be neither clipped nor displayed.
As you proceed around the window, extending each edge and defining an inside half-space
and an outside half-space, nine regions are created - the eight "outside" regions and the one
"inside"region. Each of the nine regions associated with the window is assigned a 4-bit code
to identify the region. Each bit in the code is set to either a 1(true) or a 0(false). If the region
is to the left of the window, the first bit of the code is set to 1. If the region is to the top of
the window, the second bit of the code is set to 1. If to the right, the third bit is set, and if to
the bottom, the fourth bit is set. The 4 bits in the code then identify each of the nine regions
as shown below.
For any endpoint ( x , y ) of a line, the code can be determined that identifies which region
the endpoint lies. The code's bits are set according to the following conditions:
The sequence for reading the codes' bits is LRBT (Left, Right, Bottom, Top).
Once the codes for each endpoint of a line are determined, the logical AND operation of the
codes determines if the line is completely outside of the window. If the logical AND of the
endpoint codes is not zero, the line can be trivally rejected. For example, if an endpoint had a
code of 1001 while the other endpoint had a code of 1010, the logical AND would be 1000
which indicates the line segment lies outside of the window. On the other hand, if the
endpoints had codes of 1001 and 0110, the logical AND would be 0000, and the line could
not be trivally rejected.
The logical OR of the endpoint codes determines if the line is completely inside the window.
If the logical OR is zero, the line can be trivally accepted. For example, if the endpoint codes
are 0000 and 0000, the logical OR is 0000 - the line can be trivally accepted. If the endpoint
codes are 0000 and 0110, the logical OR is 0110 and the line can not be trivally accepted.
Algorithm
The Cohen-Sutherland algorithm uses a divide-and-conquer strategy. The line segment's
endpoints are tested to see if the line can be trivally accepted or rejected. If the line cannot be
trivally accepted or rejected, an intersection of the line with a window edge is determined and
the trivial reject/accept test is repeated. This process is continued until the line is accepted.
To perform the trivial acceptance and rejection tests, we extend the edges of the window to
divide the plane of the window into the nine regions. Each end point of the line segment is
then assigned the code of the region in which it lies.
If both codes are 0000,(bitwise OR of the codes yields 0000 ) line lies
completely inside the window: pass the endpoints to the draw routine.
If both codes have a 1 in the same bit position (bitwise AND of the codes
is not 0000), the line lies outside the window. It can be trivially rejected.
3. If a line cannot be trivially accepted or rejected, at least one of the two endpoints must
lie outside the window and the line segment crosses a window edge. This line must
be clipped at the window edge before being passed to the drawing routine.
4. Examine one of the endpoints, say . Read 's 4-bit code in
order: Left-to-Right, Bottom-to-Top.
5. When a set bit (1) is found, compute the intersection I of the corresponding window
edge with the line from to . Replace with I and repeat the algorithm.
Midpoint Subdivision Algorithm:-
Midpoint subdivision algorithm is an extension of the Cyrus Beck algorithm. This algorithm
is mainly used to compute visible areas of lines that are present in the view port are of the
sector or the image. It follows the principle of the bisection method and works similarly to
the Cyrus Beck algorithm by bisecting the line in to equal halves. But unlike the Cyrus Beck
algorithm, which only bisects the line once, Midpoint Subdivision Algorithm bisects the line
numerous times.
Also the Sutherland Cohen subdivision line clipping algorithm requires the calculation of the
intersection of the line with the window edge. These calculations can be avoided by
repetitively subdividing the line at its midpoint.
Like other algorithm, initially the line is tested for visibility. If line is completely visible it is
drawn and if it is completely invisible it is rejected. If line is partially visible then it is
subdivided in two equal parts. The visibility tests are then applied to each half. This
subdivision process is repeated until we get completely visible and completely invisible line
segments. This is illustrated in figure (k) below
As shown in the figure (k), line P1 P2 is partially visible. It is subdivided in two equal Parts
P1 P3 and P3 P2 (see Fig. k (b)). Both the line segments are tested for visibility and found to
be partially visible. Both line segments are then subdivided in two equal parts to get
midpoints P4 and P5 (see Fig. k (c)). It is observed that line segments P1 P4 and P5 P2 are
completely invisible and hence rejected. However, line segment P3 P5 is completely visible
and hence drawn. The remaining line segment P4 P3 is still partially visible. It is then
subdivided to get midpoint P6. It is observed that P6 P3 is completely visible whereas P4 P6
is partially visible. Thus P6 P3 line segment is drawn and P4 P6 line segment is further
subdivided into equal parts to get midpoint P7. Now, it is observed that line segment P4 P7 is
completely invisible and line segment P7 P6 is completely visible (see Fig. k (f)), and there is
no further partially visible segment.