I017 CG Lab6
I017 CG Lab6
Prerequisites:-
- python
Outcomes:-
- Student will explore the clipping process can be applied to various geometric entities such as
points, lines, and polygons, and it is fundamental in the rendering pipeline of computer graphics
to handle the visibility of objects within a defined region of interest.
Steps:
1
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
Example: Consider clipping a line with endpoints P1(70,20) and P2(100,10) against a window with corners at
(50,10) and (80,40). The Cohen-Sutherland algorithm would calculate OutCodes for P1 and P2, determine that part
of the line is inside the viewport, and then clip the line segment to fit within the window.
Algorithm Steps:
2
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
Aim:- To learn the line clipping Algorithm like Cohen Sutherland and Midpoint division clipping
Algorithm.
Assignment 6
Objective: To learn line clipping algorithm.
1. Write a program to perform cohen Sutherland line clipping algorithm with appropriate
example.
Code:
import matplotlib.pyplot as plt
while True:
if code1 == 0 and code2 == 0: # Trivially accept the line
3
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
if code == code1:
x1, y1 = x, y
code1 = compute_outcode(x1, y1)
else:
x2, y2 = x, y
code2 = compute_outcode(x2, y2)
def plot_clipped_line(clipped_line):
if clipped_line:
x_values = [point[0] for point in clipped_line]
y_values = [point[1] for point in clipped_line]
plt.plot(x_values, y_values, color='red')
def main():
# Define the viewport
xmin, ymin = -100, -100
xmax, ymax = 100, 100
if __name__ == "__main__":
main()
Output:
5
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
2. Create a program to implement the midpoint subdivision line clipping technique and
ensure that it handles the case when the line is both partially within and outside the
viewport window.
Given a line segment with endpoints P1(2, 2) and P2(8, 8) and a clipping window with corners at (4, 4) and
(6, 6).
Code:
import matplotlib.pyplot as plt
while True:
# Both points are inside the viewport, accept the line
if code1 == 0 and code2 == 0:
return [(x1, y1), (x2, y2)]
x1, y1 = x, y
code1 = compute_region_code(x1, y1, xmin, ymin, xmax, ymax)
else:
x2, y2 = x, y
code2 = compute_region_code(x2, y2, xmin, ymin, xmax, ymax)
def plot_clipped_line(clipped_line):
if clipped_line:
x_values = [point[0] for point in clipped_line]
y_values = [point[1] for point in clipped_line]
plt.plot(x_values, y_values, color='red')
def main():
# Define the viewport
xmin, ymin = 4, 4
xmax, ymax = 6, 6
plot_clipped_line(clipped_line)
if __name__ == "__main__":
main()
Output:
8
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
Conclusion: -
The Liang-Barsky line clipping algorithm, like Cohen-Sutherland, is a method used to clip lines
against a rectangular viewing window. Both algorithms work by determining if a line segment is
entirely inside, entirely outside, or partially inside and partially outside the clipping window.
However, there are differences between the two techniques:
2. Condition for Clipping: In Cohen-Sutherland, the line is clipped against each side of the
window sequentially, whereas Liang-Barsky computes the intersection points of the line with
the window boundaries directly using parametric equations.
3. Efficiency: Liang-Barsky is often more efficient than Cohen-Sutherland because it avoids the
need for multiple intersection calculations and comparisons against region codes. Liang-
Barsky computes the intersection points only when necessary, potentially reducing the
number of calculations.
5. Handling Horizontal and Vertical Lines: Liang-Barsky handles horizontal and vertical lines
more effectively than Cohen-Sutherland, as it does not require special cases for these
orientations.