2. This Presentation is Based on
Following Research Papers
• S. R. Kodituwakku, K. R. Wijeweera, M. A. P. Chamikara
(2013), An Efficient Algorithm for Line Clipping in Computer
Graphics Programming, Ceylon Journal of Science (Physical
Sciences), Volume 17, pp. 1-7.
• S. R. Kodituwakku, K. R. Wijeweera, M. A. P. Chamikara
(2012), An Efficient Line Clipping Algorithm for 3D Space,
International Journal of Advanced Research in Computer
Science and Software Engineering, Volume 2 (Issue 5), pp.
96-101.
3. What is Clipping ?
Any procedure which identifies that portion of a scene
which is either inside or outside a region is referred to as
a clipping algorithm or clipping
4. Line Clipping Algorithms
• In computer graphics, line clipping is the process of
removing lines or portions of lines outside of a region
of interest
• Well known algorithms
– Cohen Sutherland 2D Algorithm
– Cohen Sutherland 3D Algorithm
– Liang Barsky 2D Algorithm
– Liang Barsky 3D Algorithm
7. The Scissoring Method
FOR each point ( x, y ) on the line segment
IF ( x, y ) is inside the region
Save ( x, y )
END IF
END FOR
8. The Scissoring Method
(Against a Rectangular Window)
Condition:
( x >= minx ) AND ( x <= maxx )
AND
( y >= miny ) AND ( y <= maxy )
9. The Scissoring Method
(Against a Cuboidal Volume)
Condition:
( x >= minx ) AND ( x <= maxx )
AND
( y >= miny ) AND ( y <= maxy )
AND
( z >= minz ) AND ( z <= maxz )
10. Problem with the Scissoring Method
Computational cost =
(Cost to evaluate the condition)
*
(Number of points on the line segment)
Problem:
What will happen in high resolution systems?
11. Proposed Model
• The simplest model ever invented
• Faster than well known clipping algorithms
• Low memory consumption
• Flexible with different shapes of clipping regions
• Easy to extend to higher dimensions
• A single mathematical model
12. Proposed Model against a Rectangle
• Line segments are divided into four classes
• Let ( x1, y1 ) and ( x2, y2 ) be the end points of the
line segment
x1 = x2 y1 = y2 Nature of the line segment
F F Declined
F T Parallel to x-axis
T F Parallel to y-axis
T T A point
13. Proposed Model against a Rectangle
(Separation into Classes)
IF { ( x1 != x2 ) AND ( y1 != y2 ) }
// Declined
ELSE IF ( x1 != x2 )
// Parallel to x-axis
ELSE IF ( y1 != y2 )
// Parallel to y-axis
ELSE
// A point
END IF
14. Proposed Model against a Rectangle
(Separation into Classes)
IF { ( x1 != x2 ) AND ( y1 != y2 ) }
// Declined
ELSE IF ( x1 != x2 )
// Parallel to x-axis
ELSE IF ( y1 != y2 )
// Parallel to y-axis
ELSE
// A point
END IF
15. Proposed Model against a Rectangle
(Declined)
Equation of the line segment
( x – x1) / (x2 – x1 ) = ( y – y1 ) / ( y2 – y1 );
Let’s take
L = x2 – x1;
M = y2 – y1;
16. Proposed Model against a Rectangle
(Declined)
Using the parameter t,
( x – x1) / (x2 – x1 ) = ( y – y1 ) / ( y2 – y1 );
( x – x1) / (x2 – x1 ) = t t = ( x – x1) / L;
( y – y1 ) / ( y2 – y1 ) = t t = ( y – y1 ) / M;
17. Proposed Model against a Rectangle
(Declined)
Using the parameter t,
( x – x1) / (x2 – x1 ) = ( y – y1 ) / ( y2 – y1 );
( x – x1) / (x2 – x1 ) = t x = x1 + L * t;
( y – y1 ) / ( y2 – y1 ) = t y = y1 + M * t;
18. Proposed Model against a Rectangle
(Declined)
Intersection with x = p line,
t = ( p –x1) / L;
x = p;
y = y1 + M * t;
19. Proposed Model against a Rectangle
(Declined)
Intersection with y = q line,
t = ( q – y1) / M;
x = x1 + L * t;
y = q;
20. Proposed Model against a Rectangle
(Declined)
L = x2 – x1; M = y2 – y1;
FOR each end point ( x, y ) of the line segment
IF ( x < minx )
t = ( minx – x1 ) / L; x = minx; y = y1 + M * t;
ELSE IF ( x > maxx )
t = ( maxx – x1 ) / L; x = maxx; y = y1 + M * t;
IF ( y < miny )
t = ( miny – y1 ) / M; x = x1 + L * t; y = miny;
ELSE IF ( y > maxy )
t = ( maxy – y1 ) / M; x = x1 + L * t; y = maxy;
END IF
END FOR
21. Proposed Model against a Rectangle
(Declined)
An important observation after code is executed!
IF line segment is completely outside
Both end points become a single point
ELSE
End points are the end points of clipped line segment
END IF
22. Proposed Model against a Rectangle
(Declined)
Example 1: Line segment is completely inside
B
A
23. Proposed Model against a Rectangle
(Declined)
Example 2: Line segment is partially inside
B
A
B’
B’’
24. Proposed Model against a Rectangle
(Declined)
Example 3: Line segment intersects clipping window
B
A
B’
A’
A’’
B’’
25. Proposed Model against a Rectangle
(Declined)
Example 4: Line segment is completely outside
B
A
A’
A’’
B’
B’’
26. Proposed Model against a Rectangle
(Declined)
How can we prove this result?
By considering all the possible test cases
1 2 3
4 5 6
7 8 9
27. Proposed Model against a Rectangle
(Declined)
• One end point can be in 9 regions
• Therefore both the end points can be in 9*9 (= 81)
ways
• Now the problem can be divided into 81 classes
• Each class can be further divided into subclasses
based on dissimilarities
• Since the result is true for all the test cases then the
result is true for all the possible cases
28. Proposed Model against a Rectangle
(Declined)
Example 1: Both end points are in the region 1
B
A
B’A’
Due to “minx”:
Subclass 1
A’’
B’’
29. Proposed Model against a Rectangle
(Declined)
Example 1: Both end points are in the region 1
B
A
B’A’
Due to “minx”:
Subclass 2
30. Proposed Model against a Rectangle
(Declined)
Example 1: Both end points are in the region 1
B
A
B’A’
Due to “minx”:
Subclass 3
A’’
B’’
31. Proposed Model against a Rectangle
(Declined)
Example 2: End points are in the regions 2 and 4
B
A
Subclass 1
A’
A’’
B’
32. Proposed Model against a Rectangle
(Declined)
Example 2: End points are in the regions 2 and 4
B
A
Subclass 2 A’
B’
33. Proposed Model against a Rectangle
(Declined)
An important observation after code is executed!
IF line segment is completely outside
Both end points become a single point
ELSE
End points are the end points of clipped line segment
END IF
Now we have proved the result !
34. Proposed Model against a Rectangle
(Declined)
IF{ [ abs ( L ) < 1 ] AND [ abs ( M ) < 1 ] }
// Ignore
ELSE
FOR each end point (x, y) of the line segment
x = FLOOR ( x + 0.5 );
y = FLOOR ( y + 0.5 );
END FOR
Line ( x1, y1, x2, y2 );
END IF
35. Proposed Model against a Rectangle
(Precision Error)
Example 1:
1 / 3 = 0.33333…
• But in computer it is stored as 0.333333
• Therefore checking the exact equality causes a
problem
36. Proposed Model against a Rectangle
(Precision Error)
Example 2:
• Mathematically; 1 / 3 = 1 - ( 2 / 3 )
• In computer
1 / 3 = 0.333333
2 / 3 = 0.666666
1 - ( 2 / 3 ) = 0.333334 Not equal
37. Proposed Model against a Rectangle
(Dealing with Precision Error)
• Seek approximate equality instead of exact equality
( x1 = x2 ) AND ( y1 = y2)
is replaced with
[ abs ( L ) < 1 ] AND [ abs ( M ) < 1 ]
38. Proposed Model against a Rectangle
(Integer Inputs to Line Drawing Algorithms)
• Efficient line drawing algorithms deal only with
integers
– Example: Bresenham line drawing algorithm
• If floating point inputs were provided then they
would be truncated
• Truncation may badly affect the quality of graphics
– Example: ( 3.9, 4.0 ) ( 3, 4 )
• Therefore proper approximation should be done
when converting into integers
39. Proposed Model against a Rectangle
(Better Approximation)
p = FLOOR ( p + 0.5 )
Example:
10 10.59.5 119
40. Proposed Model against a Rectangle
(Separation into Classes)
IF { ( x1 != x2 ) AND ( y1 != y2 ) }
// Declined
ELSE IF ( x1 != x2 )
// Parallel to x-axis
ELSE IF ( y1 != y2 )
// Parallel to y-axis
ELSE
// A point
END IF
41. Proposed Model against a Rectangle
(Parallel to x-axis)
IF { ( y1 >= miny ) AND ( y1 =< maxy ) }
FOR each end point (x, y) of the line segment
IF ( x < minx )
x = minx;
ELSE IF ( x > maxx )
x = maxx;
END IF
END FOR
IF ( x1 != x2 )
Line ( x1, y1, x2, y2 );
END IF
END IF
42. Proposed Model against a Rectangle
(Parallel to x-axis)
Example 1: Line segment is completely inside
BA
43. Proposed Model against a Rectangle
(Parallel to x-axis)
Example 2: Line segment is partially inside
BA B’
44. Proposed Model against a Rectangle
(Parallel to x-axis)
Example 3: Line segment intersects clipping window
BA B’A’
45. Proposed Model against a Rectangle
(Parallel to x-axis)
Example 4: Line segment is completely outside 1
BA B’
A’
46. Proposed Model against a Rectangle
(Parallel to x-axis)
Example 5: Line segment is completely outside 2
A B
47. Proposed Model against a Rectangle
(Separation into Classes)
IF { ( x1 != x2 ) AND ( y1 != y2 ) }
// Declined
ELSE IF ( x1 != x2 )
// Parallel to x-axis
ELSE IF ( y1 != y2 )
// Parallel to y-axis
ELSE
// A point
END IF
48. Proposed Model against a Rectangle
(Parallel to y-axis)
IF { ( x1 >= minx ) AND ( x1 =< maxx ) }
FOR each end point ( x, y ) of the line segment
IF ( y < miny )
y = miny;
ELSE IF ( y > maxy )
y = maxy;
END IF
END FOR
IF ( y1 != y2 )
Line ( x1, y1, x2, y2 );
END IF
END IF
49. Proposed Model against a Rectangle
(Parallel to y-axis)
Example 1: Line segment is completely inside
B
A
50. Proposed Model against a Rectangle
(Parallel to y-axis)
Example 2: Line segment is partially inside
B
A
B’
51. Proposed Model against a Rectangle
(Parallel to y-axis)
Example 3: Line segment intersects clipping window
B
A
B’
A’
52. Proposed Model against a Rectangle
(Parallel to y-axis)
Example 4: Line segment is completely outside 1
B
A
B’A’
53. Proposed Model against a Rectangle
(Parallel to y-axis)
Example 5: Line segment is completely outside 2
B
A
54. Proposed Model against a Rectangle
(Separation into Classes)
IF { ( x1 != x2 ) AND ( y1 != y2 ) }
// Declined
ELSE IF ( x1 != x2 )
// Parallel to x-axis
ELSE IF ( y1 != y2 )
// Parallel to y-axis
ELSE
// A point
END IF
55. Proposed Model against a Rectangle
(A Point)
IF <condition>
PutPixel ( x, y );
END IF
Condition:
( x >= minx ) AND ( x <= maxx )
AND
( y >= miny ) AND ( y <= maxy )
Similar to Scissoring Method !
56. Proposed Model against a Rectangle
(Separation into Classes)
IF { ( x1 != x2 ) AND ( y1 != y2 ) }
// Declined
ELSE IF ( x1 != x2 )
// Parallel to x-axis
ELSE IF ( y1 != y2 )
// Parallel to y-axis
ELSE
// A point
END IF
57. Proposed Model against a Rectangle
(Experimental Comparison)
• Programming Language:
C++;
• Computer:
Intel(R) Pentium(R) Dual CPU;
E2180 @ 2.00 GHz;
2.00 GHz, 0.98 GB RAM;
• IDE Details:
Turbo C++;
Version 3.0;
Copyright(c) 1990, 1992 by Borland International, Inc;
58. Proposed Model against a Rectangle
(Experimental Comparison)
Method:
• Clip window with values minx = miny = 100 and maxx =
maxy = 300 was used
• Random points were generated in the range 0 - 399
– randomize() function
• Those random points were used to generate random line
segments
• Number of clock cycles taken by each algorithm to clip
100000000 random line segments were counted
– clock() function
60. Proposed Model against a Rectangle
(Experimental Comparison)
Average Ratio =
Clock cycles for traditional algorithm
Clock cycles for proposed algorithm
61. Proposed Model against a Rectangle
(Experimental Comparison)
Average Ratios
Cohen Sutherland: Proposed = 1.1297
Liang Barsky: Proposed = 1.0677
The proposed algorithm is 1.13 times faster than
Cohen Sutherland and 1.07 times faster than
Liang Barsky
62. Proposed Model against a Cuboid
(Separation into Classes)
• Let ( x1, y1, z1 ) and ( x2, y2, z2 ) be the end points
of the line segment
IF { ( x1 != x2 ) AND ( y1 != y2 ) AND ( z1 != z2 ) }
// Declined
ELSE
// Simply a 2D clipping problem
END IF
63. Proposed Model against a Cuboid
(Declined)
Equation of the line segment
( x – x1) / (x2 – x1 ) = ( y – y1 ) / ( y2 – y1 ) = ( z – z1) / ( z2 – z1 );
Let’s take
L = x2 – x1;
M = y2 – y1;
N = z2 – z1;
64. Proposed Model against a Cuboid
(Declined)
Using the parameter t,
( x – x1) / (x2 – x1 ) = ( y – y1 ) / ( y2 – y1 ) = ( z – z1) / ( z2 – z1 );
( x – x1) / (x2 – x1 ) = t t = ( x – x1) / L;
( y – y1 ) / ( y2 – y1 ) = t t = ( y – y1 ) / M;
( z – z1) / ( z2 – z1 ) = t t = ( z – z1) / N;
65. Proposed Model against a Cuboid
(Declined)
Using the parameter t,
( x – x1) / (x2 – x1 ) = ( y – y1 ) / ( y2 – y1 ) = ( z – z1) / ( z2 – z1 );
( x – x1) / (x2 – x1 ) = t x = x1 + L * t;
( y – y1 ) / ( y2 – y1 ) = t y = y1 + M * t;
( z – z1) / ( z2 – z1 ) = t z = z1 + N * t;
66. Proposed Model against a Cuboid
(Declined)
Intersection with x = p plane,
t = ( p –x1) / L;
x = p;
y = y1 + M * t;
z = z1 + N * t;
67. Proposed Model against a Cuboid
(Declined)
Intersection with y = q plane,
t = ( q – y1) / M;
x = x1 + L * t;
y = q;
z = z1 + N * t;
68. Proposed Model against a Cuboid
(Declined)
Intersection with z = r plane,
t = ( r –z1) / N;
x = x1 + L * t;
y = y1 + M * t;
z = r;
69. Proposed Model against a Cuboid
(Declined)
L = x1 – x2; M = y1 – y2; N = z1 – z2;
FOR each end point ( x, y ) of the line segment
IF ( x < minx )
t = ( minx –x1) / L; x = minx; y = y1 + M * t; z = z1 + N * t;
ELSE IF ( x > maxx )
t = ( maxx –x1) / L; x = maxx; y = y1 + M * t; z = z1 + N * t;
IF ( y < miny )
t = ( miny – y1) / M; x = x1 + L * t; y = miny; z = z1 + N * t;
ELSE IF ( y > maxy )
t = ( maxy – y1) / M; x = x1 + L * t; y = maxy; z = z1 + N * t;
IF (z < minz )
t = ( minz –z1) / N; x = x1 + L * t; y = y1 + M * t; z = minz;
ELSE IF ( z > maxz )
t = ( maxz –z1) / N; x = x1 + L * t; y = y1 + M * t; z = maxz;
END IF
END FOR
70. Proposed Model against a Cuboid
(Declined)
IF { [ abs ( L ) < 1 ] AND [ abs ( M ) < 1 ] AND [ abs ( N ) < 1 ] }
// Ignore
ELSE
FOR each end point ( x, y, z ) of the line segment
x = FLOOR ( x + 0.5 );
y = FLOOR ( y + 0.5 );
z = FLOOR ( z + 0.5 );
END FOR
Line ( x1, y1, z1, x2, y2, z2 );
END IF
71. Proposed Model against a Cuboid
(Proof)
• Consider all the possible test cases
• Proof is done as we did for a rectangular clipping
window
72. Proposed Model against a Cuboid
(Experimental Comparison)
Average Ratios
Cohen Sutherland: Proposed = 1.1268
Liang Barsky: Proposed = 1.0651
The proposed algorithm is 1.13 times faster than
Cohen Sutherland and 1.07 times faster than
Liang Barsky