0% found this document useful (0 votes)
11 views

An_analysis_and_algorithm_for_polygon_cl

This document presents an analysis and algorithm for polygon clipping, addressing the need for effective clipping methods in raster graphics. The algorithm extends the concept of clipping from lines to polygons, considering the visibility status of edges and the unique challenges posed by polygon corners. It introduces a systematic approach to identify entry and exit points, ultimately producing a clipped output polygon that may include segments of the clip window boundary.

Uploaded by

Groleo Mutt
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

An_analysis_and_algorithm_for_polygon_cl

This document presents an analysis and algorithm for polygon clipping, addressing the need for effective clipping methods in raster graphics. The algorithm extends the concept of clipping from lines to polygons, considering the visibility status of edges and the unique challenges posed by polygon corners. It introduces a systematic approach to identify entry and exit points, ultimately producing a clipped output polygon that may include segments of the clip window boundary.

Uploaded by

Groleo Mutt
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

RESEARCH

CONTRIBUTIONS

a~ 8 ~ ~ ~
An Analysis and
Algorithm for Polygon
Clipping
Y ~ U ~ a~ ~J~N A. ~ ~ University of California, Berkeley

ABSTRACT: The process of polygon


clipping is analyzed and the
understanding gained thereby
forms the foundation of a very
You-Dong Liang'sprimary 1. INTRODUCTION direct algorithm. Each edge of the
research interestis computer- With the increasing popularity of raster graphics, it has be- input polygon is considered to be a
aided geometric design. vector and is then extended to be a
come necessary to develop clipping algorithms that are capa-
Brian Barsky's research
interest includecomputer- ble of handling polygons. In Refs. [1] and [2], the authors line of infinite extent. The
aided geometricdesign and describe a family of clipping algorithms for lines. Similar ideas boundary line segments of the clip
modeling,and interactive are extended in the present paper to perform polygon clip- window are extended in a similar
three-dimensional computer ping. fashion to become boundary lines.
graphics. The intersection points of an
Authors'PresentAddresses: Polygon clipping is the process of removing those parts of a
You-DongHang.Departmentof polygon that lie outside a region called the clip window. Both extended edge with the beundary
Mathematics,Zhejiang the input and output to a polygon clipping algorithm are lines are identified as entry or exit
University,Hangzhou,Zhejiang, polygons. Such an algorithm clips the input polygon to the points. It is observed that these
People'sRepublicof China; boundaries of the clip window, thereby producing a clipped entry and exit points can be used to
BrianA. Barsky,Berkeley
ComputerGraphicsLaboratory, output polygon (Figure 1). characterize the visibility status of
ComputerScienceDivision, A major distinction between line and polygon clipping is the edge. Not only might the edge
Universityof California, that the output of the latter may involve segments of the have a visible segment, but it can
Berkeley.California94720. window boundary in addition to parts of the input polygon. A also induce the inclusion in the
particularly difficult case occurs when the input polygon sur- output polygon of a vertex at a
rounds a comer of the clip window; here, the output clipped corner of the clip window called a
polygon should contain two segments that meet at the corner turning vertex. To quantify the
(Figure 2). visibility conditions, the edge/s
Permissionto copy without These complications are naturally handled by the algorithm written in parametric form. The
fee all or part of this material presented in this paper. As a result of a careful analysis of the possible contributions (including
is granted provided that the process of polygon clipping, a very direct algorithm is develo nil) that can be provided to the
copies are not made or oped. output polygon by a particular edge
distributed for direct are established. This analysis is
commercial advantage, the
ACM copyright notice and 2. KEY CONCEPTS then embedded in an algorithm. An
the title of the publication The algorithm assumes that the clip window is rectangular arbitrary example input polygon is
and its date appear, and and coplanar with the input polygon. Both the input and given and the output of the
notice is given that copying algorithm is shown. The execution
is by permissionof the output polygons are represented as an ordered sequence of
Associationfor Computing vertices. The algorithm then operates on a list of input ver- time for this example using this
Machinery. To copy tices VlV2 °'" Vn(Vn+l = Vl) and produces a list of output vertices algorithm is measured to be half of
otherwise, or to republish, V'1V'2 .-- V'm(V'm÷l= V'1), where m usually differs from n what is required by the standard
requires a fee and/or specific Sutherland-Hedgman reentrunt
permission.© 1983 ACM (and could even be zero if the entire polygon is outside the
0001-0782/83/1000-0868 75¢ window). polygon clipping algorithm.

868 Communicationsof the ACM November 1983 Volume26 Number 11


RESEARCH CONTRIBUTtONS

Vi+l Q-.. ~

vi

FIGURE 3, The ith Edge is the Vector from Vj to Vj+I.

we define the visible side to be that side on which the win-


dow lies, as shown in Figure 4.
The four boundary lines together divide the plane into nine
regions. Each of these nine regions is on the visible side of
some number, say p, of the boundary lines. The number p
can be two or three or four. The nine regions and their
associated p values are shown in Figure 5. The highest value
of p is four and occurs in only one region; this region is the
clip window. The lowest value of p is two and occurs in the
four corner regions.
If the line lj is not horizontal or vertical, it will intersect
once with each of the boundary lines. (The case of a horizon-
tal or vertical edge is discussed in Sec. 5.) A key observation is
that at two of these intersection points the line 1~enters the
visible region of the corresponding boundary line, while at the
other two intersection points, it exits. These first two points
are called entry points and the second two points are exit
points (Figure 6). If 1~enters at a comer vertex of the window,
the entry points are considered to be coincident, and similarly
for 1~exiting at a corner vertex. Considering the p value of the
FIGURE 1. Input Polygon and Clipped Output Polygon. regions through which li passes, this value increases by one at
an entry point, and decreases by one at an exit point.
The method considers each edge of the polygon independ-
ently. The ordering of the vertices provides a direction for
each edge which can therefore be considered as a vector. To
partition the polygon into a set of disjoint vectors, each vector
is considered to be a half-open interval containing the head,
but not the tail.The ith edgeis ~ which is a vector from
Vi to V~+i,as shown in Figure 3.
The vector V~V~+iis extended to be the directed line 1~of
infinite extent having the same direction as V~V~+i.Similarly,
each of the four boundary line segments of the clip window is FIGURE 4. The Visible Side of a Boundary Line.
extended to be a line of infinite extent, called a boundary
line. Each boundary line divides the plane into two regions;

0=2 0:3 0:2

Z 0:3

0=2
0:4

0:3
0:3

0:2

FIGURE 5. The Nine Regions of the Plane and their Associated p


FIGURE 2. Input Polygon Surrounding a Comer of the Clip Window. Values.

November 1983 Volume 26 Number 11 Communications of the ACM 869


RESEARCH CONTRIBUTIONS

Further observations can be made about the entry and exit


points. Assuming the line It is not horizontal or vertical, it
must start in one of the corner regions and end in the diago-
nally opposite comer region. Therefore, it starts and ends with
a p value of two. Moving along the direction of 1~, the p value
will increase as It leaves the first corner region, and hence the
first intersection point must be an entry point. Similarly, the p
value will decrease as It arrives at the opposite comer region,
and thus the last intersection point is always an exit point.
Recall that the clip window is the only region with a p
value of four. Since the first intersection point is an entry
point, in order for the line It to pass through the clip window
the second intersection point must also be an entry point.
Therefore, the identification of entry/exit attributes of the
two middle points is equivalent to the determination of
whether the line 1~passes through or bypasses the window.
The former case occurs when the middle points are an entry
point followed by an exit point [Figure 6(a)] while the latter FIGURE 7. Turning Vertex due to First Entry Point.
case corresponds to an exit point followed by an entry point
[Figure 6(b)].
°

ular, to have a visible segment, ViVt+l cannot be entirely on


The first case [Figure 6(a)] may or may not have a visible one of the invisible sides of the window. This condition will
segment depending on where VtVt+~ lies on It. In partic- be quantified for use in the algorithm. The second case [Fig-
ure 6(b)] will never have a visible segment. Nonetheless, It
cannot be indiscriminately rejected as it would be for line
clipping as it }may still affect the output polygon depending on
(a)
,
where VtVi+~ lies on It.
In both cases, It can affect the output polygon even if it does
not have a visible segment. This is what distinguishes polygon
clipping from line clipping. A vertex at a corner of the clip
window must sometimes be included in the output polygon.
This occurs when the input polygon surrounds the comer.
exi Such a corner vertex is called a turning vertex because the
output polygon "turns" at this vertex. This situation can be
detected by determining if an entry point of It occurs
on VtVt+~ and outside the chp window.
It should be noted that this is a necessary, but not sufficient
entry i condition for the polygon to surround the comer. For exam-
ple, the entire polygon might lie outside the window. The
accurate determination of this situation would require consid-
ering all the edges, not just one. Thus, this condition will
sometimes generate extraneous turning vertices. For example,
a turning vertex could be generated by an edge of a polygon
even though the entire polygon lies outside the window.
(b) When this condition holds, the comer vertex of the win-
dow that is closest to the relevant entry point is included in
the output polygon (Figures 7 and 8). Note that a sufficient
condition for the existence of a turning vertex is that
,,,.exit contain the first entry point. This is because the first
entry point cannot be on the clip window (unless it coincides
with the second entry point at a comer vertex of the window)
(Figure 7).

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

B70 Communications of the ACM November 1983 Volume 26 Number 11


RESEARCH CONTRIBUTIONS

\
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:

tio_2 = max(ti .... ti.~)

t ..... = min(to,, x, to,,j)

tou, 2 = max(tou,_~, tou,_y) (4)

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

FIGURE 8. Tuming Vertex due to Second Entry Point.

where ~xj = x~+a- xi and Ayj = Yi+~- Y~. The parametric


values corresponding to Vi and V~+aare t = 0 and t = 1,. Y=Yt0p
respectively. If 0 < t _< 1, then the line segment V~V~+~is
traced out; allowing -oo < t < oo generates the line 1~.
If li points to the right, it will enter at the left vertical
boundary and exit at the right vertical boundary, and con- Y=Yb0tt0m
versely for 1~pointing to the left. Similarly, if 1~points up, it
will enter at the bottom horizontal boundary and exit at the
top horizontal boundary, and conversely for 1~pointing down.
To be more specific, consider the following inequalities that X : X l e f t X=Xright
define the interior of the clip w i n d o w {Fig. 9).
Xle~--< X _< Xr~e,ht
FIGURE 9. The Interior of the Clip Window.
y~om --< Y --< Y~O
Now, the vertical boundary line containing an entry point
will be denoted x = x~,, and x = x,~, will denote the vertical
boundary line containing an exit point. The analogous hori-
zontal boundary lines will be denoted y = yi, and y = your, %

respectively. From the preceding discussion,


|Xleit ~X~ > 0
Xin = ~[Xrighl ~X i ~ 0

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

From Eq. 1, the corresponding parametric values are as


follows:
t,._x = (x~. x i ) / ~ ,
- Ax~ -~ 0

tou,_x= (Xo.,- xJ/Ax. A x i ~ O


!
t,.~ = (y~. - yi)/~y~, Ayi-~ 0

to.,_y= (you, y,)/ay,, Ayi -~ 0


- (3) FIGURE 10. Case I (1 < t._l): Reject.

November 1983 Volume 26 Number 11 Communications of the ACM 871


RESEARCH CONI'NBImONS

To derive the appropriate expression for the intersection


point, the parametric equations (1) for ~ are re-
written in the following form.
x = Xi+ t,~xi

y = yj + t ~ y j (5)

Then the intersection point on a vertical boundary is


X = Xj + tin_x,~Xi

y = yi + ti.~,~yi (6)

FIGURE 11. Case 2 (0 >_ t~_~and 1 < t~_2): Reject.


O•"4DO.......
e
A o.,...... ~
-----e

/
/

%-_ %-
%
e.--'-- e.-,.....
0--.-"

=X,o i

f FIGURE 13. Case 4 (th,_2 <- t~_l and 0 < t~_~ and 1 ~ t,,.2):
Visible Segment.

FIGURE 12. Case 3 (0 ~ t~_2and 0 _> to~_~):Reject.

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

872 Communications of the ACM November 1983 Volume 26 Number 11


RESEARCH CONTNBUnON$

and on a horizontal boundary it is


X = x~ + tio_yAxi I

Y = Yi + ti, yAyi (7)


k
These equations can be simplified by recalling Eq. (3): the
expression for a vertical boundary becomes
X = Xin

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

November1983 Volume26 Number 11 Communications of the ACM 873


RESEARCH CONTRIBUTIONS

!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

the window will be considered.


First, the case of 1~passing through the window will be )
considered. A horizontal (or vertical) orientation can be
treated as a limiting case of the oblique orientation. The near-
horizontal case is shown in Figure 17 and can be character-
ized by the condition. FIGURE 18. The Near-Vertical Case Passing Through the Window.

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

/ tou~_r ~ -oo (18)


/- and
\--

"'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.

874 Communications of the ACM November 1983 Volume 26 Number I I


RESEARCH CONTRIBInlONS

x[n + 1] := x[ll; if deltay < > 0 then touty := (yout - y[i])/deltay


y[n + 1] := y[1]; else (* horizontal *)
for i :-- 1 to n do if (ybottom < = y[i]) and (y[i] < = ytop)
begin (* edge V[i]V[i + 1]*) then (* inside *)
deltax := x[i + 1] - x[i]; touty := infinity
deltay := y[i + 1] - y[i]; else (* outside *)
if deltax > 0 touty := -infinity;
then begin (*l[i] points to the right *) if toutx < touty then (* first exit at x *) tout1 := toutx
xin := xlefl; else (* first exit at y *) tout1 := touty;
xout := xright if (0 < tin2) or (0 < tout1)
end then begin (* case 4 or 6 *)
else begin (*l[i] points to the left *) if tin2 < = tout1
xin := xright; then begin (* case 4 -- visible segment *)
xout := xleft if 0 < tin2
end; then (* V[i] outside window *)
if deltay > 0 if tinx > tiny then (* vertical boundary *)
then begin (*l[i] points up *) appendvertex
yin :-- ybottom; (xin, y[i] + tinx * deltay)
yout := ytop else (* horizontal boundary *)
end appendvertex (x[i] + tiny * deltax, yin);
else begin (*l[i] points down *) if I > tout1
yin := ytop; then (* V[i + 1] outside window *)
yout := ybottom if toutx < touty then (* vertical boundary *}
end; appendvertex
if deltax < > 0 then tinx := (xin - x[i])/deltax (xout, y[i] + toutx * deltay)
else (* vertical *) tinx := -infinity; else (* horizontal boundary *)
if deltay < > 0 then tiny := (yin - y[i])/deltay appendvertex (x[i] + touty * deltax, yout)
else (* horizontal *} tiny := -infinity; else (* V[i + 1] inside w i n d o w *) appendvertex
if tinx < tiny (x[i + 1], y[i + 1])
then begin (* first entry at x then y *) end (* case 4 *)
tinl := tinx; else (* case 6 -- turning vertex *) if tinx > tiny then
tin2 := tiny (* second entry at x *)
end appendvertex (xin, yout)
else begin (* first entry at y then x *) else (* second entry at y *) appendvertex
tin1 := tiny; (xout, yin)
tin2 := tinx
end; end (* case 4 or 6 *)
if I > = tin1 end (* case 3 or 4 or 6 *)
then begin (* case 2 or 3 or 4 or 6 *) end (* case 2 or 3 or 4 or 6 *)
ff 0 < tin1 then(* case 5--turning vertex *) appondvertex
(xin, yin); end (* edge V[i]V[i + 1] *).
if I > = tin2
then begin {* case 3 or 4 or 6 *)
if deltax < > 0 then toutx := (xout - x[i]}/deltax
else (* vertical *)
if (xleft < = x[i]) and (x[i] < = xright) then
(* inside *}
toutx := infinity
else (* outside *)
toutx := -infin-
ity;

FIGURE 20. The Algorithm Coded in Pascal.

November 1983 Volume 26 Number 11 Communications of the ACM 875


1 RESEARCH CONTNBUTIONS

VS

V6

v3v
;
FIGURE21. Exampleof PolygonClipping. V ~ y~ ~ . ~
V1 (=Vlo)

FIGURE22. DisplayShowingthe ExampleInputPolygon

876 Communications of the ACM November 1983 Volume 26 Number 11


i~$1M.qCN ¢ON1rNB(/llONS

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).

tout_x --, +oo (20) 8. CONCLUSIONS


A very direct algorithm has been developed by carefully ana-
and
lyzing the process of polygon clipping. In this algorithm, each
ti._x ~ --oo edge of the polygon is treated independently (and hence could
be processed in parallel). Each is considered to be a
to.t_x ~ --oo (21) vector and is extended to be a line of infinite extent. Simi-
The choice of which set of infinite values to use is com- larly, each of the boundary line segments is extended to be a
pletely arbitrary. The actual decision is based on a slight line of infinite extent. The intersection points of an extended
efficiency that could be exploited if the -oo values were used. edge with the boundary lines are identified as entry or exit
Due to the manner in which the actual algorithm is struc- points. An important observation is that this identification can
tured, ti._. and ti. y are always computed whereas ~ t . and be used to characterize the visibility status of the edge. Re-
~ L y are not calculated in Cases I or 2. When lj passes gardless of whether the edge has a visible segment, it might
through the window, tj._y ~ -oo if li is horizontal and induce the inclusion in the output polygon of a vertex at a
t=x ~ -oo if li is vertical. Thus, the simplest approach is to corner of the clip window. This is called a turning vertex
use the -oo values for the case where 1~bypasses the window. because the output polygon "turns" at this point.
In this manner, the check for whether 1~passes inside or out- These concepts were formalized with the introduction of
side the window need only be done for Cases 3 through 6. some notation. The edge was expressed as parametric equa-
tions. Using this notation, the possible contributions (including
6. AIGORITHM nil) that can be provided to the output polygon by a particular
Figure 20 shows the algorithm coded in Pascal. The ordering edge of the input polygon were enumerated.
of the cases has been reorganized to avoid redundant compar- After extending this development to handle the case of
isons. Note that Case 5 is dealt with prior to Case 4 or Case 6, horizontal or vertical edges, the analysis was embedded in an
as was explained in Sec. 4. If none of Cases 4, 5, and 6 is algorithm coded in Pascal. An arbitrary example input poly-
applicable, no output is generated. gon is given and the type of contribution and the actual
The computational complexity of the algorithm is linear in output for each edge of the polygon is shown. In the example,
the number of edges. The exact requirements are dependent this algorithm required only half the execution time than that
upon the particular data. In general, the worst case for a of the traditional Sutherland-Hodgman reentrant polygon
single edge occurs when both of its endpoints lie outside the clipping algorithm.
window and it has a visible segment. In this case, two inter-
section points must be computed and included in the output Acknowledgments. The authors would like to thank Shini-
polygon. This requires a total of 18 comparisons, 8 additions/ chiro Haruyama for his programming effort, Michael H. Ar-
subtractions, 2 multiplications, and 4 divisions. nold and Anthony D. DeRose for their assistance, Douglas B,
7. EXAMPLE Terry for his interest, and the other members of the computer
As an example, consider the closed polygon graphics class at Berkeley for their enthusiasm during the
V~V2VsV4VsVsVTV,VgV~o(= V~) with V2 lying on the boundary development of this analysis and algorithm. Appreciation is
line, as shown in Figure 21. Table III shows for each edge of also expressed to Ivan E. Sutherland and to Kevin J, Wefler for
the input polygon, the type of contribution to the output taking the time to read a draft of this paper.
polygon as well as the actual output. Since the input polygon
is closed, the initial and final vertices of the output polygon
will be on the same boundary of the window, and thus must REFERENCES
be connected as a final step in the algorithm. 1. Liang You-Dong and Barsky, Brian A. A new concept and method for
This example was computed using both this algorithm as line clipping. ACM Transactions on Graphics, to appear.
2. Liang You-Dong and Barsky, Brian A. Three new algorithms for two-
well as the traditional Sutherland-Hodgman [3] reentrant pol- dimensional line clipping. (In preparation.)
ygon clipping algorithm. The former algorithm required only 3. Sutherland Ivan E. and Hodgman, Gary W. Reentrant polygon clip-
half the execution time of the latter. (Specifically, they re- ping. Comm. ACM. 17, 1 (Jan. 1974) 32-42.
quired 5 and 10 seconds cpu time, respectively, to perform 4. Tilove, Robert B. Set membership classification: A unified approach
to geometric intersection problems. IEEE Trans. Computers C.29, 10
(Oct. 1980) 874-883.
5. Weiler, Kevin J. Hidden Surface Removal Using Polygon Area Sorting.
TABLE III. The Type of Contribution and the Actual Output for Each Edge of Master's Thesis, Cornell University, Ithaca, N.Y. (Jan. 1978).
the Example Input Polygon 6. Weiler, Kevin J. Polygon comparison using a graph representation.
Proc. SIGGRAPH '89, ACM, (July, 1980), pp. 10-18. Also Research
Edge Contribution ActualOutput Report No. 6, Institute of Building Sciences, Carnegie-Mellon Univer-
sity, Pittsburgh, Pennsylvania, May 1980.
VIV2 Turningvertex V'l
V2V3 n+ 1 n ..I- 1
CR Categories and Subject Descriptors: 1.3.3 [Computer Graphics]:
V3V~ Visiblesegment V'2V'3 Picture/Image Generation--display algorithms; 1.3.4 [Computer Graph-
V~V5 Visiblesegment V'4 ics]: Graphics Utilities--software support; 1.3.5 [Computer Graphics]:
VsV6 Two turning vertices V'sV'e Computational Geometry and Object Modeling--geometric algorithms,
VeV7 Visible segment V'TV'8 languages, and systems
VTV8 Visible segment V's General Term: Algorithms
VsVs Visible segment V'loV'. Additional Key Words and Phrases: clipping, polygon clipping
VgVlo Visiblesegment V'nV'13
Received 3/82; revised 11/82; accepted 11/82

November 1983 Volume26 Number 11 Communications of the ACM 877

You might also like