An_analysis_and_algorithm_for_polygon_cl
An_analysis_and_algorithm_for_polygon_cl
CONTRIBUTIONS
a~ 8 ~ ~ ~
An Analysis and
Algorithm for Polygon
Clipping
Y ~ U ~ a~ ~J~N A. ~ ~ University of California, Berkeley
Vi+l Q-.. ~
vi
Z 0:3
0=2
0:4
0:3
0:3
0:2
3. FORMALIZATION
As explained in the preceding section, several conclusions can
entry be drawn from the identification of the entry/exit attribute of
the intersection points along It with the boundary lines. Note
that this attribute is independent of which boundary lines are
horizontal or vertical. However, to formalize the notions pre-
entry sented in the previous section, it is convenient to introduce an
x-y coordinate system for the window.
The coordinates of Vt and Vt+lare denoted by (xt, yt) and
(Xt+l, yt+l), respectively. The parametric equations for
ViVi+~ a r e
FIGURE 6. Entryand Exit Points: (a)ll Passes Through Window; x - xt y - yt
t = ~ = (1)
(b) It Bypasses Window. ~xt ,~yt
\
These parametric values can then be used to easily deter-
mine the parametric values of the first and second entry
points and the first and second exit points, which will be
denoted t~_~, tin_Z, t~L~, and ~L2, respectively:
4. CLASSIFICATION
Using the notation introduced in Sec. 3, this section classifies
what contributions, if any, a particular edge of the input
polygon will make to the output polygon. For a given edge of
the input polygon, there are three cases where it will have no
contribution to the output polygon, and hence can be re-
jected. The first of these occurs w h e n the edge terminates
prior to its first entry point (1 < tin 1). The region of the plane
where this occurs is shown in Figure 10 for all orientations of
li. The second case is characterized by an edge that begins
after its first entry point (0 _> t~,_l) and terminates prior to its
fXright
xo~t = txl~f
~ ~
]~Ybottom
~ytop
~Xi > 0
Axi --< 0
~Yi > 0
AYi --< 0
#
I I
you,
~Ytop
= lY~o,,om
Ayi > 0
Ay~ _< 0 (2) ¢
4'
S
y = yj + t ~ y j (5)
y = yi + ti.~,~yi (6)
/
/
%-_ %-
%
e.--'-- e.-,.....
0--.-"
=X,o i
f FIGURE 13. Case 4 (th,_2 <- t~_l and 0 < t~_~ and 1 ~ t,,.2):
Visible Segment.
second one (1 < tin 2), as shown in Figure 11. The third case
applies to an edge if it begins after its second entry point (0 _>
t~n2) and first exit point (0 _> ~ u ) , which is illustrated in
Figure 12.
The above three cases completely account for all the rejec-
tion possibilities. Consider now the cases where there is a
contribution to the output po!ygon. The first of these is the
case where there is a visible segment, as would be found in
line clipping. As indicated in Sec. 2, this will occur if and only ond exi t'~"~-~ L
if the second entry.~_.!p_~precedes the first exit point on fi rst exit
It (t~_2_< ~ , j ) , and V~V~+ibegins prior to the first exit point
(0 < tat_l) and terminates after the second entry point second entry
(1 _> t~ z) (Figure 13). Note that it is still possible for each
of the endpoints of V~V"'"~+" ~ to be outside or inside the win- ~fi rst er
dow. If an endpoint is outside the window, then the cor-
responding endpoint of the visible segment is the intersec-
tion point with the window boundary.
Consider the case where Vj is outside the window. Since
begins prior to the first exit point the only way that V~
will be outside the window m that V~V +1 begin pnor to the
second entry point (0 < ten_2)(Figure 14). This intersection
point will be on the same boundary as the second entry point.
Thus, the intersection point will be on a vertical boundary if
t~_x> t~._yand on a horizontal boundary if t~_y> t~_~. FIGURE 14. V~V~+I Begins Prior to the Second Entry PoinL
Y = Yi + ti,_,Ayi (8)
t
and for a horizontal boundary it becomes t
I J/I t
X = Xi + tin_y,~.Xi
.h
4, //,,
Y = yin (9)
•
The case where Vi+~.is outside. )the w i n d o w can be analyzed
]n an analogous fashion. ViVi+~ must terminate after the
EL I
t
I
-- - ~"
first exit point (1 > t~,_~). The intersection point w i l l he on a
vertical boundary if ~,_~ < ~,_v and on a horizontal boundary
if ~ , , < ~_,. The expression for the intersection point on a
FIGURE 15. Case 5 (0 < t=.l -< 1): Tuming Vertex.
vertical boundary is then
X ~ Xoul
Y = Yi + t ..... Ayi (10) turning vertex is determined and then added to the output
polygon.
and on a horizontal boundary it is These six cases for clipping a polygon edge are summarized
x = Xi + tout_yAXi in Table I. These cases are mutually exclusive with the ex-
ception of Case 5 (turning vertex due to the first entry point).
y = yo,, (11) After creating this turning vertex, the vector V~7~ might
If an endpoint is inside the window, it w i l l also be an continue to generate a visible segment (Case 4) or a turning
endpoint of the visible segment. However, there is no need to vertex due to the second entry point (Case 6). (Cases 4 and 6
add the first endpoint (Vi) to the output polygon because it cannot both occur, since they are mutually exclusive.) Note
w i l l have already been added from the previous edge. (In the that the output of Case 5 must precede that of Case 4 or Case
case of the first edge, this endpoint w i l l be appended from the 6. This ordering is important and must be reflected in the
closing edge.) The second endpoint (Vi+0, though, must be algorithm. Thus, the contribution to the output polygon from
added to the output polygon when this endpoint is inside the an edge is composed of a turning vertex due to the first entry
window. Since V~Vi+~ terminates after the second entry point, point (if any) and then either a visible segment (if any) or a
a sufficient condition for this to occur is that ViVi+~ terminate turning vertex due to the second entry point (if any), but not
prior to the first exit point (1 < to,u). In this case, the contri- both. The possibilities are summarized in Table II. (Recall that
bution to the output polygon is just V~+~. a visible segment is usually output as a sequence of two
The final two cases handle contributions to the output poly- vertices, except that the first endpoint (Vi) is not added to the
gon other than a visible segment. These contributions take the output polygon if it is inside the window.)
form of turning vertices. As explained in Sec. 2 these situa-
tions occur when an entry point of li lies on ViVi+~ and ]s
outside the clip window. The two cases correspond to this TABLE I. Classification of the Six Cases for Clipping a Polygon Edge
entry point being either the first or second entry point, respec- Case Condition Contribulion Figure
tively.
Consider first the case where it is the first entry point. 1 1 < t~.~ none 10
Since the first entry point cannot be on the clip window 2 0 _>t=_~and 1 < t~_2 none 11
3 0 _>t=~ and 0 _>t~_~ none 12
(except at a corner vertex), a sufficient condition for the oc-
4 t,_2 _< t~_l and 0 < t=~_~ visiblesegment 13
currence of this case is that the first entry point be on ViVi.'~ and 1 _> t,~
(0 < t~_~ -~ 1). This is shown in Figure 15 for all orientations of 5 0 < t=_l-< 1 turningvertex 15
li, The turning vertex is (x~, yi.) since this is the corner vertex 6 0 < t,_2-< 1 andt~_~< t ~ turningvertex 16
of the window that is closest to the first entry point. This
turning vertex, then, is added to the output polygon.
The final case is that of a turning vertex due to the second
entry point. This arises when the second entry point lies on TABLE II. Possible Contribution to the Output Polygon from an Edge
(0 < ti._2 ~< 1) and is outside the clip window
Cases Contribution
( ~ u < ti._2) (Figure 16). Unlike the previous case, there are
now two possibilities for the turning vertex. This vertex may 5 Turningvertexdue to firstentrypoint
be one of two corner vertices, depending on whether the 5 then4 Turningvertexdue to firstentrypointfollowedby visiblesegment
second entry point is on a vertical or horizontal boundary. In 5 then6 Turningvertex due to first entry pointfollowedby turningvertexdue
to secondentrypoint
the vertical case (tin_×> tin_y), the closest corner vertex to the Visiblesegment
second entry point is (x~, your),while the horizontal case (ti.y Turningvertexdue to secondentrypoint
> ti._~) yields the turning vertex (Xou. yi.). From this, the
!N
5. HORIZONTAL/VERTICAL EDGES
It has been assumed that 1~is neither horizontal nor vertical.
t
Of course, these situations can occur, and for completeness
they will now be addressed. It will be shown that results of
the previous section can also be applied to horizontal and
vertical edges. This is achieved by carefially assigning positive
and negative values of infinity to the parametric values corre-
sponding to the entry and exit points. As for the oblique
orientation, both the cases of 1~passing through and bypassing y
t i . y ~< tinx ~< to.t_~ ~< to.t_: (12) The analogous analysis holds for the near-vertical case
which is shown in Figure 18. This is represented by
As 1, approaches the horizontal, t,._: and tout y cease to be t~._x ~< ti._y ~< tout_y ~ tout x (14)
finite. Since ti._~ and ~ t x are finite, the infinite values of t~._:
and ~t_: must be as follows: Since t~,_: and ~t_ y are finite, the appropriate infinite values
are
ti._y ~ -oo (13)
t,._x ~ -oo (15)
tout_ y "~ -boo
tout_× ~ -I-oo
NOW, consider the case of 1~bypassing the window. In this
case, there are two sets of infinite values that are valid, de-
pending on whether the derivation is based on entering a
~r horizontal or vertical boundary first. Depending on the direc-
J %
tion of 1~, Figure 19 can be represented as either
S % tin_: ~< tout_: ~< tt.x ~< to.t_x (16}
~R _\
v/ or
s %
J ti.x ~< to.t_x ~< tin v <~ t o . t : (17)
For the horizontal case, t~_~ and ~ t x are finite and t~._y and
t ~ , : are infinite. The infinite values corresponding to inequal-
ities 16 and 17, respectively, are
tin : ~ --O0
"'T_, / %
tiny ~ +oo
to.t_y ~ -4-o0 (19)
t
FIGURE 16. Case 6 (0 < t~j ~_ I and to~_l < t~_2):Turning Vertex.
_/
S
f
S
J
/
/
FIGURE 17. The Near-Horizontal Case Passing Through the Window. FIGURE 19. Ii Bypassing the Window.
VS
V6
v3v
;
FIGURE21. Exampleof PolygonClipping. V ~ y~ ~ . ~
V1 (=Vlo)
For the vertical case, t~._y and ~ L y are finite and t~. and the clipping 100 times on a DEC VAX-11/780 in Pascal under
~ , ~ are infinite. The corresponding infinite values are, re- Berkeley UNIX.) The results of both algorithms are identical:
spectively, both produce a single polygon containing a degenerate edge
tin_x "--* +oo (Figure 22).