Computer Graphics Second Module Second
Computer Graphics Second Module Second
ywmax
(xw,yw)
ywmin
xwmin xwmax
yvmax
(xv,yv)
yvmin
xvmin xvmax
yv - yvmin = yw - ywmin
------------- --------------
yvmax - yvmin ywmax - ywmin
Solving these expressions for the viewport position (xv, yv), we have
sx = xvmax - xvmin
--------------
xwmax - xwmin
sx =
yvmax - yvmin
--------------
ywmax - ywmin
Equations A can also be derived with a set of transformations that converts the
window area into the viewport area. This conversion is performed with the following
sequence of transformations:
Relative proportions of objects are maintained if the scaling factors are the same
(sx = sy). Otherwise, world objects will be stretched or contracted in either the x or y
direction when displayed on the output device.
2.4 CLIPPING
Many graphics application programs give the users the impression of looking
through a window at a very large picture. Figure shows the use of this effect in a
program for viewing different portions of a large architectural plan at different scales.
Viewing an architectural plan through windows of different sizes
portion of the picture and partly outside. We cannot display each of these lines in its
entirety.
Occurrence of wraparound in drawing a partially invisible triangle. The correct
way to select visible information for display is to use clipping, a process which divides
each element of the picture in to its visible and invisible portions, allowing the
invisible portion to be discarded. Clipping can be applied to a variety of different types
of picture elements such as pointer, lines, curves, text character and polygons.
Before clipping
D
F
H
F
G
E
I
E
After clipping
F
G
CLIPPING ENDPOINTS
Before discussing clipping lines, consider clipping of individual points. If the x
coordinate boundaries of the clip rectangle are at xmin and xmax and the y coordinate
boundaries are at ymin, and ymax, the following conditions must be satisfied for a point
at (x, y) to be inside the clip rectangle.
xmin <= x <= xmax
ymin <= y <= ymax
(xmax,ymax)
(xmin, ymin)
If any of the above conditions do not hold, the point is outside the clip rectangle.
From the above figure, these lines that are partly invisible are divided by the
screen boundary in to one or more invisible portions but in to only one visible
segment. Visible segment of a straight line can be determined by computing its 2
endpoints. The Cohen- Sutherland algorithm is designed to find these end points very
rapidly but also to reject even more rapidly any line that is clearly invisible. This
makes it a very good algorithm for clipping pictures that are much larger than the
screen.
depends on the fact that every line is entirely on the screen. Or can be divided so that
one part can be rejected.
We are extending the edges of the screen so that they divide the space occupied
by the unclipped picture in to 9 regions. Each of these regions has a 4 bit code.
B
A
D
0001 0000 0010
C
F
E H
0100 0110
0101 G
If the logical AND of the 2 codes is not zero, then the line will be entirely off
the screen. For example, for line GH, the logical AND of the 2 end points is 0100. it is
not equal to zero, so the line is completely out of the screen.
If the 4 bit codes for both endpoints are zero, the line lies entirely on the screen.
For example, for line CD, the 4 bit codes for both endpoints C, D are 0000. So the line
lies entirely inside the screen.
If both these tests are not successful, it means that a part of a line is inside and a
part is outside the screen. For example line AB. Then such lines are subdivided. A
simple method of sub division is to find the point of intersection of the line with one
edge of the screen and to discard that part of the line that lies off the screen.
A
1001 1000 1010
For line BC, we apply the same tests. The line cannot be rejected, so we again
subdivide it at point D. the resulting line BD is entirely on the screen.
The algorithm works as follows. We compute the out codes of both endpoints
and check for trivial acceptance and rejection. If both tests are not successful, we find
an end point that lies outside and test the out code to find the edge that is crossed and
to determine the corresponding intersection point. We can clip off the line segment
from the outside end point to the intersection point by replacing the outside end point
with the intersection point and compute the outside of this new end point to prepare for
the next iteration.
Point A has out code 0000 and point D has out code 1001. The line AD cannot
be accepted and cannot be rejected. Therefore the algorithm chooses D as the outside
point, whose out code shows that the line crosses the top edge and left edge. In our
algorithm we choose the top edge of the screen to clip and we clip AD to AB. We
compute Bs out code as 0000. In our next iteration, we apply the trivial acceptance/
rejection tests to AB, and the line is accepted and displayed.
E
0101 0100 0110
EI requires more iteration. The first end point E has out code 0100. The
algorithm chooses E as the outside point and tests the out code to find the first edge
against which the line is cut is the bottom edge, where EI is clipped top FI. In the
second iteration, FI cannot be completely accepted or rejected. The out code of the first
end point, F is 0000, so the algorithm chooses the outside point I that has out code
1010.
The first edge clipped against is the top edge, yielding FH. H has the
out code 0010. Then the next iteration results in a clip against the right edge to FG.
This is accepted in the final iteration and is displayed.
Do {
If ( ! (outcode0 | outcode1))
{ /* accept and exit */
accept = TRUE; done = TRUE;
}
else if ( outcode0 && outcode1)
/* logical AND is true, so reject and exit */
done = TRUE;
else
{
/* failed both tests, so calculate the line segment to clip */
/* from an outside point to an intersection with clip edge */
double x, y;
/* at least one end point is outside the clip rectangle, find it */
if (outcode0)
outcodeout = outcode0;
else
outcodeout = outcode1;
/* now find the intersection point */
/* use formulas y = y0 + slope * (x x0) */
/* x = x0 + (1/slope) * y y0) */
if (outcodeout && TOP)
/* Divide line at top of clip rectangle*/
{
x = x0 + (x1 x0) * (ymax y0) / y1 y0);
y = ymax;
}
Another example is
Before clipping
vertex list. (2) If both input vertices are inside the window boundary, only the second
vertex is added to the output vertex list. (3) If the first vertex is inside the window
boundary and the second vertex is outside, only the edge intersection with the window
boundary is added to the output vertex list. (4) If both input vertices are outside the
window boundary, nothing is added to the output list.
V1
V1 V2 V1
V2
outin inin
save v1,v2 save v2
V2
V1
V1
V1
V2
inout outout
save V1 save none
(Figure 2.15 Successive processing of pairs of polygon vertices against the left
window boundary)
These four cases are illustrated in Figure. 2.15 for successive pairs of polygon
vertices. Once all vertices have been processed for one clip window boundary, the
output list of vertices is clipped against the next window boundary.
2.4.4CURVE CLIPPING
The bounding rectangle for a circle or other curved object can be used first to
test for overlap with a rectangular clip window. If the bounding rectangle for the object
is completely inside the window, we save the object. If the rectangle is determined to
be completely outside the window, we discard the object. In either case, there is no
further computation necessary. But if the bounding rectangle test fails, we can look for
other computation-saving approaches. For a circle, we can use the coordinate extents
of individual quadrants and then octants for preliminary testing before calculating
curve-window intersections. For an ellipse, we can test the coordinate extents of
individual quadrants. Figure 2.16 illustrates circle clipping against a rectangular
window. Similar procedures can be applied when clipping a curved object against a
general polygon clip region. On the first pass, we can clip the bounding rectangle of
the object against the bounding rectangle of the clip region. If the two regions overlap,
we will need to solve the simultaneous line-curve equations to obtain the clipping
intersection points.
STRING 1
STRING 2 STRING 2
STRING 1 ING 1
STRING 2 STRING 2
STRING 3 RING 3
STRING 1 RING 1
STRING 2 STRING 2
STRING 3 TRING 3