A Line Segment Clipping Algorithm in 2D

advertisement
International Journal of Computer Graphics
Vol. 3, No. 2, November, 2012
A Line Segment Clipping Algorithm in 2D
Bimal Kumar Ray
School of Information Technology & Engineering
VIT University, Vellore – 632014, India
bimalkumarray@vit.ac.in, raybk_2000@yahoo.com
Abstract
This paper presents an algorithm for clipping line segment in 2D against rectangular
window. It uses a trivial test for rejecting line segments that are completely beyond the
boundaries of the clipping region otherwise, splits the clipping problem into two calls to a
routine that returns a Boolean value. If at least one of the calls returns false then no portion
of the line segment is inside the clipping region. The algorithm has been compared with the
three traditional algorithms namely, Cohen-Sutherland (CS), Liang-Barsky (LB) and NichollLee-Nicholl (NLN) and two other algorithms, namely, Bui-Skala [13] and Skala [14], with
respect to the number of operations performed and the time. The performance of the proposed
algorithm has been found to be better than the CS, LB, Bui-Skala and Skala algorithm and the
algorithm is found to be simpler than the NLN algorithm in that its development, testing and
validation are easier.
Keywords: clipping, line segment, position of a point with respect to a line, algorithm,
comparison, performance
1. Introduction
Removing a portion of an image is known as clipping. The region (window) against which
clipping is performed is usually rectangular with sides parallel to the coordinate axes. Since
an image may consist of line segments, it is necessary to device algorithm that performs line
clipping. Line clipping is useful in geographic information system, VLSI circuits design,
designing building architecture, to mention a few.
A number of researchers had addressed the problem. To begin with, Cohen-Sutherland [1,
2] outcode algorithm quickly removes those lines that are completely outside the window and
also detect lines that are completely inside the window. For lines that intersect the window
boundaries, the algorithm repeatedly tests and finds point of intersection of the line segment
with each of the window boundaries until the inside test is satisfied. The Cyrus-Beck [3] and
the Liang-Barsky [4] algorithms use a parametric representation of the line segment. NichollLee-Nicholl [5], in order to replace division by multiplication, uses a slightly modified
version of the algorithm used by Cyrus-Beck and Liang-Barsky. Though the NLN algorithm
is more efficient than the CS and LB algorithm but since there is a large number of cases to
be considered; development, testing and validation of the algorithm are difficult. Sobokow, et.
al., [6] encoded the line instead of encoding its end points and showed improvement over the
then existing algorithms. The algorithm is similar to the CS algorithm but in contrast to the
CS algorithm, it does not need to iterate to find out the clipped segment. The line code is an
8-bit number instead of 4-bit outcode used in the CS algorithm. A large number of cases need
to be considered and this is why you find a big switch statement (or a long else-if ladder) in
the implementation. Dörr [7] combined the concept of outcode and parametric line clipping to
device a new algorithm. Andreev and Sofianska [8] reduce the computational load identifying
51
International Journal of Computer Graphics
Vol. 3, No. 2, November, 2012
the basic cases of the locations of the line segment with respect to the window. Sharma and
Manohar [9] proposed the opposite-corner algorithm and the perpendicular-distance
algorithm based on geometric observations. Day [10] proposed an algorithm that works on
object space and image space. Skala proposed an algorithm [11] for line clipping showing
improvement over the Cyrus-Beck algorithm. Skala [12] also proposed an O(lg N) algorithm
for line clipping against convex window. Bui and Skala [13] suggested modifications of the
Cohen-Sutherland algorithm for clipping lines (line suggested algorithm) and line segments
(line segment suggested algorithm). The algorithms are based on coding the direction of line
as well as the end points. In [14] Skala uses homogeneous coordinates and developed line and
line segment clipping algorithm. The later uses outcode to encode the end points of the line
segment. The algorithm does not require division operation, if the input and output
coordinates are homogeneous.
This paper presents a 2D algorithm for line clipping against rectangular window. In
contrast to the CS algorithm, the proposed algorithm does not compute outcode and does not
use iterative algorithm to remove portions of a line segment that are outside the window. In
contrast to the NLN algorithm, only one routine can handle line segments that are either
completely inside the window or intersect the same. It performs trivial test to detect whether
one end point of the line segment is inside the window. If the trivial test fails then it performs
computation followed by test. A higher level routine makes two calls to this routine
interchanging the role of the coordinates of the end points. The former uses trivial test to
remove line segments that are either to the left of the left boundary or to the right of the right
boundary or below the bottom boundary or above the top boundary. The other line segments
are handled by the lower level routine.
The rest of the paper is organized as follows. In Section 2 the methodology adopted is
described, in Section 3 the implementation is presented, in Section 4 comparison of the
proposed algorithm with other algorithms are made, and finally, in Section 5, conclusion is
drawn.
2. Methodology
A line L: ax + by + c = 0 divides the 2D plane into two regions – one containing the origin,
called the origin side and the other not containing the origin, called the non-origin side of the
line. One of these two regions is the positive side and the other is the negative side of the line.
If c > 0 then the origin side is the positive side but if c < 0 then the origin side is the negative
side. If c > 0 and (u1, v1) is any point in the 2D plane such that au1 + bv1 + c > 0 then (u1,
v1) is on the positive side and if c < 0 and (u1, v1) is any point in the 2D plane such that au1
+ bv1 + c < 0 then (u1, v1) is on the negative side of the line. So if (u2, v2) is another point
on the plane and if au2 + bv2 + c has the same sign as that of au1 + bv1 + c then (u1, v1)
and (u2, v2) are on the same side of the line L. On the other hand if au1 + bv1 + c and au2 +
bv2 + c are of opposite sign then (u1, v1) and (u2, v2) are on the opposite sides of the line.
If the clipping window is defined by x = xmin, x = xmax, y = ymin and y = ymax then the
four corners A, B, C and D of the window (Figure 1) have the coordinates (xmin, ymin),
(xmin, ymax), (xmax, ymax) and (xmax, ymin) respectively. If E(x1, y1) and F(x2, y2) are
the end points of a line segment to be clipped then the equation of the line passing through
(x1, y1) and (x2, y2) is (x2 – x1)(y – y1) – (y2 – y1)(x – x1) = 0. If the points A and B are on
the opposite sides of the line containing the segment EF and E as well as F are not onto the
left of the left boundary of the clipping window then the segment EF intersects the left
boundary AB. Thus if the product
P = [(x2 – x1)(ymin – y1) – (y2 – y1)(xmin – x1)] [(x2 – x1)(ymax – y1) – (y2 – y1)(xmin – x1)]
52
International Journal of Computer Graphics
Vol. 3, No. 2, November, 2012
is negative then the intersection point of the segment EF is to be computed and is given by
x = xmin and y = y1 + (xmin – x1)(y2 – y1)/(x2 – x1).
If a line segment passes through one of the corners (xmin, ymin) and (xmin, ymax), then
the product P vanishes. So the test that confirms that the line segment either intersects the left
boundary of the window or passes through one of the corners (xmin, ymin) and (xmin, ymax)
is P ≤ 0.
The other window boundaries can be treated in a similar fashion. The line segments that are
completely beyond the window boundaries and those that are completely inside the clipping
window can be detected by trivial tests.
Figure 1. A Rectangular Window ABCD and Two Line Segments EF and GH
3. Implementation
The methodology is implemented by a top level routine called clipLine which in turn
makes two calls to another routine called clipOneEnd. The clipOneEnd routine accepts the
end points of the segment to be clipped as its arguments. It applies trivial test on (x1, y1) to
test whether it is inside the clipping window. If so the routine returns true without
manipulating (x1, y1). If the trivial test fails then x1 is tested against xmin to find out whether
it is to the left of the left boundary and if so, the points A and B (Figure 1) are tested to find
whether they are on the opposite sides of the line segment EF, using sign test on the product
P. If the test succeeds then the point of intersection of the segment EF with AB is computed.
If the test fails then the y coordinate of the end point E of the segment is tested against the
bottom boundary of the clipping window in a similar manner. If the test on x1 against xmin
fails then x1 is tested against xmax. Those line segments that are neither totally beyond one of
the window boundaries nor totally inside the clipping window nor do they intersect the
window boundaries (e.g. line segment GH in Figure 1) is discarded by the sign test on the
products
P1 = [(x2 – x1)(ymin – y1) – (y2 – y1)(xmin – x1)] [(x2 – x1)(ymax – y1) – (y2 – y1)(xmin – x1)]
53
International Journal of Computer Graphics
Vol. 3, No. 2, November, 2012
P2 = [(x2 – x1)(ymin – y1) – (y2 – y1)(xmax – x1)] [(x2 – x1)(ymax – y1) – (y2 – y1)(xmax – x1)]
P3 = [(x2 – x1)(ymin – y1) – (y2 – y1)(xmin – x1)] [(x2 – x1)(ymin – y1) – (y2 –
y1)(xmax – x1)]
P4 = [(x2 – x1)(ymax – y1) – (y2 – y1)(xmin – x1)] [(x2 – x1)(ymax – y1) – (y2 – y1)(xmax – x1)].
For example, the product P1 is positive because the corner points A and B of the window
are on the same side of GH.
The trivial rejection test is performed inside the routine clipLine that takes the end points
of the line segment to be clipped as its arguments. If the trivial test succeeds then the routine
simply returns but if it fails then two calls to the routine clipOneEnd is made. The second call
is made interchanging the role of x1 and y1 with that x2 and y2. If both the calls return true
then the output line segment is displayed, otherwise no part of the input line segment is
displayed. If one of the end points (say, (x1, y1)) of the line segment is inside the window and
the other end intersects the window, then the trivial test performed inside clipOneEnd (x1, y1,
x2, y2) returns true and call to clipOneEnd(x2, y2, x1, y1) calculates the coordinates of the
point of intersection. The algorithms clipLine and clipOneEnd are presented below.
Algorithm clipLine(x1, y1, x2, y2)
xmin, ymin, xmax, ymax are global variables.
begin
if x1 < xmin then
if x2 < xmin then return;
if x1 > xmax then
if x2 > xmax then return;
if y1 < ymin then
if y2 < ymin then return;
if y1 > ymax then
if y2 > ymax then return;
dx := x2 − x1; dy := y2 − y1;
if clipOneEnd(x1, y1, dx, dy) then
if clipOneEnd(x2, y2, dx, dy) then
drawLine(x1, y1, x2, y2);
return;
end;
Algorithm clipOneEnd(x, y, dx, dy)
begin
if x ≥ xmin then
if x ≤ xmax then
if y ≥ ymin then
if y ≤ ymax then return true;
if x < xmin then
begin
if (dx*(ymin − y) − dy*(xmin − x) )*(dx*(ymax − y) − dy*(xmin − x)) ≤ 0 then
begin
y := y + (xmin − x)*dy/dx;
x := xmin;
return true;
end;
end;
54
International Journal of Computer Graphics
Vol. 3, No. 2, November, 2012
else if x > xmax then
begin
if(dx*(ymin − y)− dy*(xmax − x) )*(dx*(ymax − y) − dy*(xmax − x)) ≤ 0 then
begin
y := y + (xmax − x)* dy/dx;
x := xmax;
return true;
end;
end;
if y < ymin then
begin
if(dx*(ymin − y)−dy*(xmin−x))*(dx*(ymin− y) − dy*(xmax − x)) ≤ 0 then
begin
x := x + (ymin − y)* dx/dy;
y := ymin;
return true;
end;
end;
else if y > ymax then
begin
if (dx*(ymax− y) − dy*(xmin− x))*(dx*(ymax − y) − dy*(xmax − x)) ≤ 0 then
begin
x := x + (ymax − y)*dx/dy;
y := ymax;
return true;
end;
end;
return false;
end;
The clipping window can be transformed to <−1, 1> window with the help of the
transformation formulae x` = sx * x + tx and y` = sy * y + ty, where sx = 2/(xmax - xmin), tx
= −(xmax + xmin)/(xmax − xmin), sy = 2/(ymax − ymin), ty = − (ymax + ymin)/(ymax −
ymin). The transformation to the interval <−1, 1> and the corresponding inverse
transformations are included in the window-to-view transformation and so they do not require
additional computation in the graphics pipeline. The equation of the line segment with the
end points (x1, y1) and (x2, y2) in the prime coordinate system is ax` + by` + c = 0. The
coefficient vector p = [a b c] of the line segment is given by a = y`1 – y`2, b = x`2 – x`1 and c
= x`1y`2 – x`2y`1. With window <−1, 1>, the test conditions P1 ≤ 0, P2 ≤ 0, P3 ≤ 0 and P4
≤ 0 reduce to (b – a + c)*(c – b – a) ≤ 0, (a + b + c)*(a – b + c) ≤ 0, (c – a – b)*(a – b + c) ≤
0 and (b - a + c)*(a + b + c) ≤ 0 respectively. The point of intersection of the line segment p =
[a b c] with the left boundary, in homogeneous coordinates (x`, y`, w) with w = 1, is defined
by the vector cross product [a b c] X [–1, 0, 1] = [b, (c – a), –b] and so the point of
intersection in the Cartesian coordinates are ( – 1, (a – c)/b). The point of intersection of the
line segment with the other boundaries can similarly be computed. As long as the
computations are performed in homogeneous coordinates, no division is required. The
algorithm clipOneEnd in homogeneous coordinates with <–1, 1> window is called
clipOneEndH and is presented below. The modified algorithm clipLineH in <−1, 1> and
homogeneous coordinates is also presented.
55
International Journal of Computer Graphics
Vol. 3, No. 2, November, 2012
Algorithm clipOneEndH(x, y, a, b, c) {
The inputs x, y are in homogeneous coordinates with window <−1, 1>. The output are in the
Cartesian coordinates. The [a, b, c] is the line segment vector.
if x >= −1 then
if x <= 1 then
if y >= −1 then
if y <= 1 then return 1;
if x < −1 then
begin
if (b − a + c)*(c − b − a) ≤ 0 then
begin
y := (a − c)/b;
x := −1;
return 1;
end;
end
else if x > 1 then
begin
if (a + b + c)*(a − b + c) ≤ 0 then
begin
y := −(c + a)/b;
x := 1;
return 1;
end;
end;
if y < −1 then
begin
if (c − a − b)*(a − b + c) ≤ 0 then
begin
x := (b − c)/a;
y := −1;
return 1;
end;
end
else if y > 1 then
begin
if (b − a + c)*(a + b + c) ≤ 0 then
begin
x := − (b + c)/a;
y := 1;
return 1;
end;
end;
return 0;
end;
56
International Journal of Computer Graphics
Vol. 3, No. 2, November, 2012
Algorithm clipLineH(x1, y1, x2, y2)
x1, y1, x2 and y2 are in homogeneous coordinates with w = 1 and the clipping window is <−1,
1>. The outputs are in the Cartesian coordinates.
begin
if x1 < −1 then
if x2 < −1 then return;
if x1 > 1 then
if x2 > 1 then return;
if y1 < −1 then
if y2 < −1 then return;
if y1 > 1 then
if y2 > 1 then return;
a := y1 − y2, b := x2 − x1, c := x1*y2 − x2*y1;
if clipOneEndH(x1,y1,a, b,c) then
if clipOneEndH(x2,y2,a, b, c) then output(x1,y1,x2,y2);
return;
end;
4. Comparison
The proposed algorithm is compared, with respect to the number of assignments (:=),
comparison (<), addition/subtraction (±), multiplication (×), division (/) and the time t[s] to
clip a line segment, with three conventional algorithms namely, CS, LB and NLN algorithm
and with two other relatively recent algorithms namely, the line segment clipping algorithm
proposed in Bui and Skala [13] and in Skala [14]. The CS algorithm has been coded from [13],
the LB algorithm is from [1], the NLN is from [5]. The algorithm by Bui and Skala [13] is
implemented from [13] and the implementation requires a large number of cases to be
considered leading to a big nested switch (or a long nested else-if ladder). The algorithm has
actually tried to unfold the iterations present in the CS algorithm. The line segment clipping
algorithm proposed in [14] is implemented in the Cartesian coordinates because the other
algorithms discussed and compared here, use the same. The algorithm in its Cartesian form is
presented in the Appendix I along with the number of operation counts at different lines of the
algorithm. As seen from this algorithm, a large number of comparisons are required apart
from those required for computation of outcode. The conversion of the vector [c3 c2 c1 c0] into
c, whose value is required in decimal, requires three additions only (because ci values are
either 0 or 1); the evaluation of the line coefficient p = [a b c] requires 2 multiplications, 3
subtractions and 3 assignments. The computation of the coordinates of the point of
intersection of a line segment with one of the window boundary requires the same number of
arithmetic operations and assignments as it is required by the CS algorithm. The
implementation involves a large number of top-level as well as nested comparisons leading to
a big nested switch or long nested else-if ladder. The same line segment clipping has also
been implemented in homogeneous coordinates with <−1, 1> window. This algorithm appears
in the Appendix II. The testing is performed using input in homogeneous coordinates with w
= 1 and the output in the Cartesian coordinates. This algorithm is compared with the proposed
algorithm in homogeneous coordinates with <−1, 1> window.
A total of 135 line segments (Figure 2 through 10) in a wide range of geometrical
distributions are considered for testing. In the figures, the display coordinate system with the
x-axis horizontal, positive towards right and the y-axis vertical, positive downwards, have
been considered. The line segments that originate from the left region of the window are
57
International Journal of Computer Graphics
Vol. 3, No. 2, November, 2012
designated as Li, Mi and Ni, the ones originating from the mid region of the window are
designated as Pi, Qi and Ri and the ones originating from the right region of the window are
designated as Ui, Vi and Wi (in the figures all the line segments have not been labeled so as
avoid clumsiness).
Figure 2. Line Segments Starting from the Top Left Corner
Figure 3. Line Segments Starting from the Left Edge
58
International Journal of Computer Graphics
Vol. 3, No. 2, November, 2012
Figure 4. Line Segments Starting from the Bottom Left Corner
Figure 5. Line Segments Starting from the Top Middle Region
59
International Journal of Computer Graphics
Vol. 3, No. 2, November, 2012
Figure 6. Line Segments Starting from Inside the Window
Figure 7. Line Segments Starting from the Bottom Middle Edge
60
International Journal of Computer Graphics
Vol. 3, No. 2, November, 2012
Figure 8. Line Segments Starting from the Top Right Corner
Figure 9. Line Segments Starting from the Right Edge
61
International Journal of Computer Graphics
Vol. 3, No. 2, November, 2012
Figure 10. Line Segments Starting from the Bottom Right Corner Region
The line segments Li`s start from the top left corner, Mi`s from left edge, Ni`s from bottom
left corner, Pi`s from top middle region, Qi`s from the window region, Ri`s from bottom
middle region, Ui`s from top right corner, Vi`s from right edge and Wi`s from bottom right
corner.
For each line segment the table I shows the number of assignments, comparisons,
addition/subtraction, multiplication and division performed by the proposed algorithm, the CS
algorithm, the LB algorithm, the NLN algorithm, the line segment clipping algorithm in Bui
and Skala [13] and the line segment clipping algorithm in Skala [14] (in the Cartesian
coordinates and in homogeneous coordinates). The same table also shows the time in seconds
(t[s] of the order of 10-8) to clip a line segment.
Table 1. Comparative Study of the Proposed Algorithm with the CS, LB, NLN,
Bui-Skala, and Skala`s Algorithm
The symbol := is for assignment, < is for comparison, ± is for addition/subtraction, × is for
multiplication and / is for division. The column t[s] is the total time to perform these
operations.
62
International Journal of Computer Graphics
Vol. 3, No. 2, November, 2012
63
International Journal of Computer Graphics
Vol. 3, No. 2, November, 2012
Table I (Continued)
64
International Journal of Computer Graphics
Vol. 3, No. 2, November, 2012
65
International Journal of Computer Graphics
Vol. 3, No. 2, November, 2012
The time (of the order of 10-8 seconds) for assignment (:=), comparison (<),
addition/subtraction (±), multiplication (×) and division (/) are 6.6, 11.2, 2.3, 2.3 and 19.9 and
has been calculated based on the timing of 128.106 operations (:=, <, ±, ×, /) on a PC
586/133MHz [13]. A metric
E = (Time to clip by other algorithm) / (Time to clip by the proposed algorithm),
called coefficient of efficiency [13], is used to compare the performance of the proposed
algorithm with the other and the results are shown in Table 2.
Table 2. The Coefficient of Efficiency of the Proposed Algorithms with Respect
to the CS, LB, NLN, Bui-Skala and Skala`s Algorithm
Line
Segment
P1
P2
P3
P4
P5
P6
P7
P8
P9
P10
P11
P12
P13
P14
P15
P16
Q1
Q2
Q3
Q4
Q5
Q6
66
E1 = t[s]CS /
t[s]Proposed
2.600446
1.40492
1.429795
1.40492
1.429795
1.74067
1.310871
1.310871
1.457303
1.115398
1.458161
1.576496
1.62414
1.576496
2.350446
2.350446
0.717391
1.246521
1.255268
1.291054
1.738724
1.738724
E2 = t[s]LB /
t[s]Proposed
5.631696
1.100055
1.100055
1.100055
1.100055
0.961968
0.820462
0.978849
1.171967
1.372501
1.172658
1.335627
1.335627
1.335627
5.631696
5.881696
2.200229
1.555865
1.555865
1.555865
1.282216
1.282216
E3 = t[s]NLN /
t[s]Proposed
3.444196
0.828358
0.890547
0.828358
0.890547
0.765866
0.676586
0.676586
1.02768
0.823921
1.028285
1.292218
1.292218
1.292218
3.444196
3.444196
0.614989
0.943539
0.818688
1.307753
1.059601
1.059601
E4 = t[s]BS /
t[s]Proposed
2.600446
1.033167
1.194306
1.033167
1.194306
1.059425
0.699951
0.699951
1.246466
0.793406
1.247201
1.529381
1.873478
1.529381
2.350446
2.350446
0.717391
1.246521
0.890258
1.370974
1.153673
1.153673
E5 = t[s]S /
t[s]Proposed
3.046875
1.239635
1.259259
1.240741
1.259259
1.109579
1.131579
1.131579
1.374558
1.490004
1.375368
1.524087
1.559555
1.524087
2.796875
2.796875
0.729977
1.687475
1.669583
1.714115
1.424936
1.424936
E5 = t[s]SH /
t[s]ProposedH
3.046875
1.286805
1.307269
1.286805
1.307269
1.17906
1.198061
1.198061
1.44
1.491639
1.556056
1.51831
1.556056
1.847156
2.796875
2.796875
0.676221
1.627077
1.608837
1.654236
1.436943
1.436943
International Journal of Computer Graphics
Vol. 3, No. 2, November, 2012
Q7
Q8
Q9
Q10
Q11
Q12
Q13
Q14
Q15
Q16
R1
R2
R3
R4
R5
R6
R7
R8
R9
R10
R11
R12
R13
R14
R15
L1
L2
L3
L4
L5
L6
L7
L8
L9
L10
L11
L12
L13
L14
L15
L16
M1
M2
M3
M4
M5
M6
M7
M8
M9
M10
M11
M12
M13
M14
M15
M16
1.148836
1.796181
1.319511
1.319511
1.280227
1.270624
1.270624
1.747471
1.691511
1.691511
2.280357
1.515381
1.40492
1.398839
1.706227
1.106992
1.310871
1.234082
1.106992
1.375586
1.381312
2.511595
2.360714
2.480357
1.621391
4.5
1.8
2
2.393596
1.616644
1.79303
1.79303
1.157455
1.46557
1.494211
2.149299
1.356836
1.822268
1.356836
1.500493
4.598214
1.667868
4.598214
1.848094
4.696429
1.220558
1.431644
1.848094
1.518612
1.524921
1.553943
1.882416
1.40492
1.882416
1.882416
5.098214
1.802402
1.320621
1.381944
1.707988
1.707988
1.707988
1.707988
1.707988
1.437861
1.330214
1.330214
5.980357
1.58495
1.100055
1.100055
0.988278
1.063089
0.978849
0.937022
1.27252
1.035919
1.035919
1.883578
5.860714
5.980357
1.58495
3.6875
4.705357
4.825
1.275862
1.330214
1.076616
1.076616
1.555865
1.212675
1.212675
1.23309
1.067024
1.045466
1.067024
1.682759
3.6875
1.582583
3.6875
1.109679
3.6875
1.628381
1.172658
1.109679
1.255521
1.276656
1.255521
1.076616
1.100055
1.076616
1.076616
3.6875
2.011411
0.476207
0.598958
0.752946
0.752946
0.615888
1.152772
1.152772
0.954118
0.793115
0.793115
2.555357
0.97539
0.797402
0.859591
0.759463
0.72748
0.648549
0.782125
0.72748
0.87923
0.820666
1.492191
2.555357
2.555357
1.102224
1.799107
1.798214
1.798214
1.3133
0.972259
0.928438
0.928438
0.836978
0.763863
0.886959
0.886959
0.720107
0.752002
0.720107
0.917241
1.799107
0.822222
1.799107
0.663011
1.799107
0.642114
0.64614
0.663011
0.762776
0.614196
0.847319
0.714552
0.68021
0.714552
0.714552
1.799107
0.983183
0.839015
1.2125
1.417285
1.417285
0.879529
1.270624
1.270624
0.817197
1.092246
1.092246
2.280357
1.526266
1.064124
1.108347
1.055433
0.808455
0.699951
1.124566
0.808455
1.182978
1.08355
1.970185
2.360714
2.480357
1.568386
4.5
1.880357
2
1.73202
1.092246
1.119979
1.119979
1.291054
1.18769
1.413772
1.508836
1.075067
1.279256
1.075067
2.10936
4.598214
1.735135
4.598214
1.092131
4.696429
0.838535
1.002357
1.092953
1.158991
0.84795
1.229653
1.119979
1.033167
1.119979
1.119979
5.098214
1.936937
1.470132
1.457986
1.881711
1.881711
1.852466
1.832824
1.832824
1.452312
1.365976
1.365976
2.6375
1.468528
1.364566
1.352128
1.112576
1.453008
1.131579
1.065293
1.453008
1.244144
1.255856
2.283483
2.717857
2.8375
1.468528
5.392857
2.2375
2.357143
1.506404
1.403409
1.182229
1.182229
1.758648
1.456429
1.476843
1.510969
1.311528
1.281064
1.311528
1.484236
5.59375
1.796396
5.59375
1.279956
5.794643
1.859342
1.355628
1.279956
1.507886
1.522082
1.543218
1.331205
1.364566
1.331205
1.331205
6.09375
1.796396
1.471994
1.473274
1.819438
1.819438
1.789568
1.769505
2.059678
1.467854
1.374555
1.374555
2.6375
1.46023
1.423641
1.409896
1.184611
1.453859
1.198892
1.127965
1.453859
1.287593
1.300457
1.300457
2.717857
2.8375
1.46023
5.392857
2.2375
2.357143
1.522751
1.414469
1.229861
1.229861
1.699635
1.476721
1.498689
1.337618
1.36326
1.337618
2.442328
1.496825
5.59375
1.687913
5.59375
1.34025
5.794643
1.799575
1.418033
1.34025
1.533016
1.548332
1.571137
1.395102
1.423641
1.395102
1.728202
6.09375
1.687913
67
International Journal of Computer Graphics
Vol. 3, No. 2, November, 2012
N1
N2
N3
N4
N5
N6
N7
N8
N9
N10
N11
N12
N13
U1
U2
U3
U4
U5
U6
U7
U8
U9
U10
U11
U12
U13
U14
V1
V2
V3
V4
V5
V6
V7
V8
V9
V10
V11
V12
V13
V14
V15
V16
W1
W2
W3
W4
W5
W6
W7
W8
W9
W10
W11
W12
W13
Average
68
5.5
5
1.338469
5.098214
1.41158
1.816973
1.201988
1.822268
1.666559
1.827176
1.900298
2.425303
2
3.666667
4
1.336621
3.732143
1.336621
1.144578
1.627398
1.816973
1.816973
1.79303
1.573694
2.155723
2.080357
2
4.065476
1.707253
1.882416
1.882416
1.381312
1.201751
1.525552
1.525552
1.518612
1.201751
1.848094
1.40492
1.848094
1.588671
3.797619
3.732143
4.333333
2
2.195689
2.066964
1.827951
1.621695
1.822268
1.816973
1.185469
1.386863
1.386863
4.065476
4
1.977999
4.1875
3.6875
1.230159
3.6875
1.135197
1.076616
1.555865
1.045466
1.282216
1.045466
4.983631
1.594771
5.083333
4.916667
4.916667
1.006576
4.916667
1.006576
1.428624
0.913338
1.076616
1.076616
1.076616
1.237562
1.149068
4.505357
4.625
4.916667
1.772896
1.076616
1.076616
1.035919
1.489532
1.052366
1.255521
1.255521
1.489532
1.109679
1.100055
1.109679
1.394918
4.916667
4.916667
4.916667
5.083333
1.443787
4.983631
1.045466
1.195913
1.045466
1.076616
1.428624
1.067024
1.067024
4.916667
4.916667
2.069103
1.799107
1.799107
0.691877
1.799107
0.65773
0.673317
0.685885
0.861534
0.814433
0.80341
0.933036
1.067227
0.933036
3.127976
3.127976
0.747092
3.127976
0.747092
1.159912
0.807718
1.05959
1.05959
0.999734
0.987873
1.301686
2.276786
2.276786
3.127976
1.067761
0.815642
0.815642
0.739459
0.834793
0.818612
0.818612
1.051735
0.834793
0.914176
0.859315
0.914176
1.209635
3.127976
3.127976
3.127976
2.462798
1.400676
2.462798
1.068974
1.06881
1.127099
0.946794
1.005111
0.893834
0.893834
3.127976
3.127976
1.241014
5.5
5
1.756769
5.098214
1.271249
1.347167
1.424652
1.336606
1.161082
1.168432
1.900298
1.787582
2
3.666667
4
1.121143
3.732143
1.121143
1.267251
1.070864
1.352487
1.317372
1.149774
1.050995
1.609583
2.080357
2
4.065476
1.754367
1.143655
1.143655
1.054399
0.852303
0.84795
0.84795
1.194322
0.852303
1.123663
1.002211
1.123663
1.588671
3.797619
3.732143
4.333333
2
1.675402
2.066964
1.203307
1.109675
1.337122
1.412078
1.299744
1.248525
1.248525
4.065476
4
1.70754
6.392857
5.892857
1.427638
6.09375
1.331432
1.259644
1.803181
1.223198
1.424936
1.234823
2.197917
1.427638
2.297619
4.261905
4.595238
1.208902
4.395833
1.208902
1.614823
1.053487
1.259644
1.259644
1.24182
1.340485
1.30701
2.4375
2.357143
4.729167
1.701959
1.271615
1.271615
1.255856
1.700799
1.522082
1.522082
1.507886
1.700799
1.218536
1.240741
1.218536
1.583377
4.529762
4.395833
4.928571
2.297619
1.339814
2.364583
1.292689
1.396334
1.281064
1.200053
1.614823
1.221448
1.221448
4.729167
4.595238
2.002827
6.392857
5.892857
1.435564
6.09375
1.388994
1.315727
1.745034
1.273636
1.436943
1.28649
2.197917
1.435564
2.297619
4.261905
4.595238
1.247645
4.395833
1.247645
1.558157
1.11218
1.315727
1.315727
1.295958
1.346865
1.306528
2.4375
2.357143
4.729167
1.606253
1.329006
1.329006
1.300457
1.643273
1.548332
1.548332
1.533016
1.599845
1.271895
1.286805
1.271895
1.487546
4.529762
4.395833
4.928571
2.297619
1.34142
2.364583
1.350471
1.405792
1.337618
1.249631
1.558157
1.264028
1.291105
4.729167
4.595238
2.022372
International Journal of Computer Graphics
Vol. 3, No. 2, November, 2012
From this table, it may be observed that the average performance of the proposed algorithm
is about 198% of the CS, 207% of the LB and 124% of the NLN, 171% of the Bui and Skala
[13] line segment clipping algorithm, 200% of the Skala [14] line segment clipping algorithm
in the Cartesian coordinates and 202% of the same algorithm in the homogeneous coordinates
with <−1, 1> window. Out of the 135 line segments that had been considered, except in one
case only, the performance of the proposed algorithm has been found to better than the CS
algorithm, except in seven cases, the performance has been found to be better than the LB
algorithm, except in seventeen cases, the performance has been found to better then the Bui
and Skala [13] and except in one case, the performance has been found to be better than the
Skala [14] algorithm. Though the performance of the NLN algorithm has been found to better
than the proposed one, but as it is well known [15], implementation of the NLN algorithm
requires multiple routines to be developed one each for (x1, y1) in each of the corner and
edge regions of the window and also for the other point (x2, y2) exterior or interior of the
clipping window. Obviously, there are a large number of cases and the resulting algorithm is
quite complex. Consequently, development, testing and validation are of considerable
concern. On the contrary, the proposed algorithm requires development of one routine and
calling it twice from a top level routine. The proposed algorithm is simpler than the NLN
algorithm in that its development, testing and validation are much easier than those of the
NLN algorithm. The Bui and Skala [13] algorithm also suffers from similar drawback as the
NLN algorithm, as it is evident from the presence of a big nested switch (or nested else-if
ladder) in its implementation.
5. Conclusion
An algorithm for clipping line segment in 2D has been proposed. The resulting algorithm
requires two calls to a routine from a top level routine. The implementation has been
compared with the CS, LB, NLN, the line segment clipping algorithm in Bui and Skala [13]
and Skala [14]. The average performance of the proposed algorithm has been found to be
better than the CS, LB, NLN, [13] and [14]. The performance of the proposed algorithm is
better then the CS, LB and [14], except in a few cases. As compared to the algorithm [13], the
performance of the proposed algorithm is better, except in 17 cases. But the implementation
of this algorithm involves a large number of top-level as well as nested comparisons leading
to a big nested switch or a long nested else-if ladder. Though in a large number of cases, the
performance of the NLN algorithm is better than the proposed algorithm, but its development,
testing and validation is difficult.
APPENDIX I
The line segment clipping algorithm Skala[14] in Cartesian coordinates. The comments are
written in italics.
Algorithm code(x, y)
begin
c := 0;
if x < xmin then c := 1;
else if x > xmax then c := 2;
if y < ymin then c := c + 4 ;
else if y > ymax then c := c + 8;
end;
69
International Journal of Computer Graphics
Vol. 3, No. 2, November, 2012
Algorithm C_LS(x1, y1, x2, y2)
cA := code(x1,y1);
cB := code (x2,y2);
Arrays TAB1, TAB2, MASK, x and y
Array TAB1 :={15, 0,0,1,1,16,0,2,2,0,16,1,1,0,0,15}; None := 15, N/A:=16
Array TAB2 :={15, 3,1,3,2,16,2,3,3,2,16,2,3,1,3,15}; None := 15, N/A:=16
Array MASK :={15, 4,4,2,2,16,4,8,8,4,16,2,2,4,4,15};
Array x :={xmin, xmax, xmax, xmin};
Array y :={ymin, ymin, ymax, ymax};
if cA lor cB = 0 then
begin
drawLine (x1,y1,x2,y2);
return;
end;
if cA land cB ≠ 0 then return;
a := y1 − y2; b := x2 − x1; c := x1*y2 − x2*y1;
Computation of the line vector p = [a b c] requires 3 subtractions and 2 multiplications
for k =1, 4 do
if a*xk + b*yk + c ≥ 0 then
Computation of each ck requires 3 additions because x[k] and y[k] are either 1 or -1
ck := 1;
else
ck:=0;
Computation of c = [c3 c2 c1 c0 ] requires 3 additions as ci = 0 or 1
c := c0 + 2*c1 + 4*c2 + 8*c3;
if c = 0 then return;
if c = 15 then return;
i := TAB1[c]; int j := TAB2[c];
if cA ≠ 0 & cB ≠ 0 then
begin
if i = 0 then
begin
x1 := x1 + (y1 − ymin)*b/a;
y1 := ymin;
end;
else if i = 1 then
begin
y1 := y1 + (x1 − xmax)*a/b;
x1 := xmax;
end
else if i = 2 then
begin
x1 := x1 + (y1 − ymax)*b/a;
y1 := ymax;
end
else return;
if j = 1 then
begin
y2 := y2 + (x2 − xmax)*a/b;
70
International Journal of Computer Graphics
Vol. 3, No. 2, November, 2012
x2 := xmax;
end;
else if j = 2 then
begin
x2 := x2 + (y2 − ymax)*b/a;
y2 := ymax;
end;
else if j = 3 then
begin
y2 := y2 + (x2 − xmin)*a/b;
x2 := xmin;
end
else return;
end
else
begin
if cA= 0 then
begin
if cB & MASK[c] ≠ 0 then
begin
if i = 0 then
begin
x2 := x2 + (y2 − ymin)*b/a;
y2 := ymin;
end
else if i = 1 then
begin
y2 := y2 + (x2 − xmax)*a/b;
x2 := xmax;
end
else if i = 2 then
begin
x2 := x2 + (y2 − ymax)*b/a;
y2 := ymax;
end
else return;
end
else
begin
if j = 1 then
begin
y2 := y2 + (x2 − xmax)*a/b;
x2 := xmax;
end
else if j = 2 then
begin
x2 := x2 + (y2 − ymax)*b/a;
y2 := ymax;
end
71
International Journal of Computer Graphics
Vol. 3, No. 2, November, 2012
else if j = 3 then
begin
y2 := y2 + (x2 − xmin)*a/b;
x2 := xmin;
end
else return;
end
end
else if cB = 0 then
begin
if cA land MASK[c] ≠ 0 then
begin
if i = 0 then
begin
x1 := x1 + (y1 − ymin)*b/a;
y1 := ymin;
end
else if i = 1 then
begin
y1 := y1 + (x1 − xmax)*a/b;
x1 := xmax;
end
else if i = 2 then
begin
x1 := x1 + (y1 − ymax)*b/a;
y1 := ymax;
end
else return;
end
else
begin
if j = 1 then
begin
y1 := y1 + (x1 − xmax)*a/b;
x1 := xmax;
end
else if j = 2 then
begin
x1 := x1 + (y1 − ymax)*b/a;
y1 := ymax;
end
else if j = 3 then
begin
y1 := y1 + (x1 − xmin)*a/b;
x1 := xmin;
end
else return;
end
end
72
International Journal of Computer Graphics
Vol. 3, No. 2, November, 2012
end
drawLine(x1,y1,x2,y2);
end;
APPENDIX II
Skala [14] in Homogeneous coordinates with <−1, 1> window
Algorithm code(x, y, c)
begin
c := 0;
if x < −1 then c := 1;
else if x > 1 then c := 2;
if y < −1 then c := c | 4 ;
else if y > 1 then c := c | 8;
end;
Algorithm CLSH(x1, y1, x2, y2)
x1, y1, x2, y2 are in homogeneous coordinates with w = 1
begin
code(x1,y1,cA);
code (x2,y2,cB);
Array TAB1 :={15, 0,0,1,1,16,0,2,2,0,16,1,1,0,0,15}; None = 15, N/A=16
Array TAB2 :={15, 3,1,3,2,16,2,3,3,2,16,2,3,1,3,15}; None = 15, N/A=16
Array MASK :={15, 4,4,2,2,16,4,8,8,4,16,2,2,4,4,15};
Array x :={−1, 1, 1, −1};
Array y :={−1, −1, 1, 1};
if cA | cB = 0 then output (x1,y1,x2,y2);
if cA & cB) ≠ 0 then return;
a := y1 − y2; b := x2 − x1; c := x1*y2 − x2*y1;
for k =1 to 4 do
if a*xk + b*yk + c ≥ 0 then c := c0 + 2*c1 + 4*c2 + 8*c3;
if c = 0 then return;
if c = 15 then return;
i := TAB1[c]; j := TAB2[c];
if cA ≠ 0 & cB ≠ 0 then
begin
if i = 0 then
begin
x1 := (b − c)/a;
y1 := −1;
end
else if i = 1 then
begin
x1 := 1;
y1 := − (c + a)/b;
end
else if i = 2 then
begin
73
International Journal of Computer Graphics
Vol. 3, No. 2, November, 2012
x1 := − (b + c)/a;
y1 = 1;
end
if j =1 then
begin
x2 := 1;
y2 := − (c + a)/b;
end
else if j = 2 then
begin
x2 := − (b + c)/a;
y2 := 1;
end
else if j = 3 then
begin
x2 := −1;
y2 := (a − c)/b;
end
end
else
begin
if cA = 0 then
begin
if (cB & MASK[c]) ≠ 0 then
begin
if i = 0 then
begin
x2 := (b − c)/a;
y2 := −1;
end
else if i = 1 then
begin
x2 := 1;
y2 := − (c + a)/b;
end
else if i = 2 then
begin
x2 := −(b + c)/a;
y2 := 1;
end;
end
else
begin
if j =1 then
begin
x2 := 1;
y2 := −(c + a)/b;
end
74
International Journal of Computer Graphics
Vol. 3, No. 2, November, 2012
else if j = 2 then
begin
x2 := −(b + c)/a;
y2 := 1;
end
else if j = 3 then
begin
x2 := −1;
y2 := (a − c)/b;
end;
end;
end
else if cB = 0 then
begin
if (cA & MASK[c]) ≠ 0 then
begin
if i = 0 then
begin
x1 := (b − c)/a;
y1 := −1;
end
else if i =1 then
begin
x1 := 1;
y1 := −(c + a)/b;
end
else if i = 2 then
begin
x1 := −(b + c)/a;
y1 := 1;
end;
end
else
begin
if j = 1 then
begin
x1 := 1;
y1 := −(c + a)/b;
end
else if j = 2 then
begin
x1 := −(b + c)/a;
y1 := 1;
end
else if j = 3 then
begin
x1 := −1;
75
International Journal of Computer Graphics
Vol. 3, No. 2, November, 2012
y1 := (a − c)/b;
end;
end;
end;
end;
output(x1,y1,x2,y2);
end;
References
[1] J. D. Foley, A. van Dam, S. K. Feiner and J. F. Hughes, “Computer Graphics: Principles and Practice”,
Addison-Wesley, (2nd edition in C), (1996).
[2] D. Hearn and P. Baker, “Computer Graphics with OpenGL”, 3rd ed., Prentice Hall, (2004).
[3] M. Cyrus and J. Beck, “Generalized two- and three-dimensional clipping”, Computers & Graphics, vol. 3, no.
1, (1978).
[4] Y. D. Liang and B. A. Barsky, “A new concept and method for line clipping”, ACM Transactions on
Graphics, vol. 3, no. 1, (1984).
[5] T. M. Nicholl, D. T. Lee and R. A. Nicholl, “An efficient new algorithm for 2-D line clipping: its
development and analysis”, Computer & Graphics, vol. 21, no. 4, (1987).
[6] S. Sobkow Mark, P. Pospisil and Y.-H. Yang, “A Fast Two Dimensional Line Clipping Algorithm via Line
Encoding”, Computer & Graphics, vol. 11, no. 4, (1987).
[7] M. Dörr, “A new approach to parametric line clipping”, Computers & Graphics, vol. 14, no. 3-4, (1990).
[8] R. Andreev and E. Sofianska, “New algorithm for two-dimensional line clipping”, Computers & Graphics,
vol. 15, no. 4, (1991).
[9] N. C.Sharma and S. Manohar, “Line clipping revisited: Two efficient algorithms based on simple geometric
observations”, Computers & Graphics, vol. 16, no. 1, (1992).
[10] J. D. Day, “An algorithm for clipping lines in object and image space”, Computers & Graphics, vol. 16, no. 4,
(1992).
[11] V. Skala, “An efficient algorithm for line clipping by convex polygon”, Computers & Graphics, vol. 17, no. 4,
(1993).
[12] V. Skala, “O(lg N) line clipping algorithm in E2”, Computers & Graphics, vol. 18, no. 4, (1994).
[13] D. H. Bui and V. Skala, “Fast algorithms for clipping lines and line segments in E2”, The Visual Computer,
vol. 14, no. 1, (1998).
[14] V. Skala, “A new approach to line and line segment clipping in homogeneous coordinates”, The Visual
Computer, vol. 21, no. 11, (2005).
[15] D. F. Rogers, “Procedural Elements for Computer Graphics”, 2nd Edition, Tata McGraw-Hill, (2005).
Author
Bimal Kumar Ray received his Ph.D. degree in Computer Science
from Indian Statistical Institute, Kolkata, India. He received his Master
degree in Applied Mathematics from Calcutta University and his
Bachelor degree in Mathematics from St. Xavier's college, Kolkata. His
research interests are in computer graphics, computer vision and image
processing. He has published a number of research papers in peer
reviewed journals.
76
Download