0% found this document useful (0 votes)
45 views9 pages

I017 CG Lab6

Uploaded by

jashkathiria98
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views9 pages

I017 CG Lab6

Uploaded by

jashkathiria98
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

SVKM’s NMIMS University

Mukesh Patel School of Technology Management & Engineering

PROGRAM: BTech IT/MBATECH SEMESTER VI

COURSE: Computer Graphics Practical Experiment: 6

Part A (To be referred by students)

SAVE THE FILE AND UPLOAD AS (RollNo_Name_Exp1)

Topic covered: Viewing and Clipping Algorithm


Learning Objective: Learner would be able to
1. The primary objective of viewing and clipping in computer graphics is to manage and optimize the
rendering of graphics.
2. It determining which parts of an object or scene should be visible on the screen.
3. Clipping is used to remove objects, lines, or line segments that are outside the viewing pane,
ensuring that only the relevant portions of the graphics are processed and displayed.
4. This not only improves the efficiency of rendering operations but also ensures that the viewer's
attention is focused on the intended visible area or viewport.

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.

 Cohen-Sutherland Line Clipping Algorithm


The Cohen-Sutherland algorithm is used for line clipping in computer graphics. It divides the viewing area into a
grid of nine regions: four corner regions outside the viewport, four side regions, and one center region where the
viewport lies. Each endpoint of the line segment is assigned a 4-bit code (OutCode) that identifies its region. The
algorithm repeatedly checks and clips the line segments against the viewport boundaries until the segment either
lies entirely within the viewport or is entirely outside.

Steps:

Calculate the OutCodes for both endpoints of the line.


If both endpoints have a code of 0, the line is completely inside the viewport and is accepted.
If the logical AND of the endpoints' OutCodes is not 0, the line is completely outside the viewport and is rejected.
Otherwise, calculate the intersection of the line with the viewport boundaries, update the endpoint(s) and their
OutCodes, and repeat from step 2.

1
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering

PROGRAM: BTech IT/MBATECH SEMESTER VI

COURSE: Computer Graphics Practical Experiment: 6

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.

 The Midpoint Subdivision Line Clipping Algorithm


The Midpoint Subdivision Line Clipping Algorithm is a method used in computer graphics to determine which
portions of a line segment are visible within a specified rectangular clipping window. This algorithm iteratively
subdivides the line segment until the subdivisions are either entirely visible within the clipping window or entirely
outside.

Algorithm Steps:

Initialization: Start with the entire line segment.


Midpoint Calculation: Find the midpoint of the line segment.
Visibility Test: Determine if the endpoints of the segment are inside, outside, or on the boundary of the clipping
window.
Subdivision: If the segment spans the boundaries of the clipping window, subdivide it at the midpoint and apply
the algorithm recursively to each half.
Termination: The process repeats until the subdivisions are trivially accepted as being inside the window or
rejected as being outside.

2
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering

PROGRAM: BTech IT/MBATECH SEMESTER VI

COURSE: Computer Graphics Practical Experiment: 6

Student Name Student Sap ID:-


Student Roll Number Date of Conduction :/ / 2024
Class :- MBA.Tech IT/B.Tech IT VI sem

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

def cohen_sutherland(x1, y1, x2, y2, xmin, ymin, xmax, ymax):


INSIDE = 0 # 0000
LEFT = 1 # 0001
RIGHT = 2 # 0010
BOTTOM = 4 # 0100
TOP = 8 # 1000

def compute_outcode(x, y):


code = INSIDE
if x < xmin:
code |= LEFT
elif x > xmax:
code |= RIGHT
if y < ymin:
code |= BOTTOM
elif y > ymax:
code |= TOP
return code

code1 = compute_outcode(x1, y1)


code2 = compute_outcode(x2, y2)

while True:
if code1 == 0 and code2 == 0: # Trivially accept the line
3
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering

PROGRAM: BTech IT/MBATECH SEMESTER VI

COURSE: Computer Graphics Practical Experiment: 6

return [(x1, y1), (x2, y2)]


elif code1 & code2: # Trivially reject the line
return []
else: # Line needs clipping
x, y = None, None
if code1 != 0:
code = code1
else:
code = code2

if code & TOP:


x = x1 + (x2 - x1) * (ymax - y1) / (y2 - y1)
y = ymax
elif code & BOTTOM:
x = x1 + (x2 - x1) * (ymin - y1) / (y2 - y1)
y = ymin
elif code & RIGHT:
y = y1 + (y2 - y1) * (xmax - x1) / (x2 - x1)
x = xmax
elif code & LEFT:
y = y1 + (y2 - y1) * (xmin - x1) / (x2 - x1)
x = xmin

if code == code1:
x1, y1 = x, y
code1 = compute_outcode(x1, y1)
else:
x2, y2 = x, y
code2 = compute_outcode(x2, y2)

def plot_line(x1, y1, x2, y2):


plt.plot([x1, x2], [y1, y2], color='blue')

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

# Define the line segment


4
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering

PROGRAM: BTech IT/MBATECH SEMESTER VI

COURSE: Computer Graphics Practical Experiment: 6

x1, y1 = -50, -50


x2, y2 = 150, 150

# Plot the viewport


plt.plot([xmin, xmax, xmax, xmin, xmin], [ymin, ymin, ymax, ymax, ymin],
linestyle='--', color='black')

# Plot the original line


plot_line(x1, y1, x2, y2)

# Clip the line


clipped_line = cohen_sutherland(x1, y1, x2, y2, xmin, ymin, xmax, ymax)

# Plot the clipped line


plot_clipped_line(clipped_line)

plt.title('Cohen-Sutherland Line Clipping')


plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.gca().set_aspect('equal', adjustable='box')
plt.grid(True)
plt.show()

if __name__ == "__main__":
main()

Output:

5
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering

PROGRAM: BTech IT/MBATECH SEMESTER VI

COURSE: Computer Graphics Practical Experiment: 6

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

def clip_line(x1, y1, x2, y2, xmin, ymin, xmax, ymax):


dx = x2 - x1
dy = y2 - y1

# Compute region codes for both points


code1 = compute_region_code(x1, y1, xmin, ymin, xmax, ymax)
code2 = compute_region_code(x2, y2, xmin, ymin, xmax, ymax)

while True:
# Both points are inside the viewport, accept the line
if code1 == 0 and code2 == 0:
return [(x1, y1), (x2, y2)]

# Both points are outside the viewport, reject the line


elif (code1 & code2) != 0:
return []

# Partially inside, partially outside, apply clipping


else:
code_out = code1 if code1 != 0 else code2

# Find intersection point


if code_out & 1:
x = xmin
y = y1 + (y2 - y1) * (xmin - x1) / (x2 - x1)
elif code_out & 2:
x = xmax
y = y1 + (y2 - y1) * (xmax - x1) / (x2 - x1)
elif code_out & 4:
y = ymin
x = x1 + (x2 - x1) * (ymin - y1) / (y2 - y1)
elif code_out & 8:
y = ymax
x = x1 + (x2 - x1) * (ymax - y1) / (y2 - y1)

# Update the coordinates


if code_out == code1:
6
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering

PROGRAM: BTech IT/MBATECH SEMESTER VI

COURSE: Computer Graphics Practical Experiment: 6

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 compute_region_code(x, y, xmin, ymin, xmax, ymax):


code = 0
if x < xmin:
code |= 1
elif x > xmax:
code |= 2
if y < ymin:
code |= 4
elif y > ymax:
code |= 8
return code

def plot_line(x1, y1, x2, y2):


plt.plot([x1, x2], [y1, y2], color='blue')

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

# Define the line segment


x1, y1 = 2, 2
x2, y2 = 8, 8

# Plot the viewport


plt.plot([xmin, xmax, xmax, xmin, xmin], [ymin, ymin, ymax, ymax, ymin], linestyle='--',
color='black')

# Plot the original line


plot_line(x1, y1, x2, y2)

# Clip the line


clipped_line = clip_line(x1, y1, x2, y2, xmin, ymin, xmax, ymax)

# Plot the clipped line


7
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering

PROGRAM: BTech IT/MBATECH SEMESTER VI

COURSE: Computer Graphics Practical Experiment: 6

plot_clipped_line(clipped_line)

plt.title('Midpoint Subdivision Line Clipping')


plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.gca().set_aspect('equal', adjustable='box')
plt.grid(True)
plt.show()

if __name__ == "__main__":
main()

Output:

8
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering

PROGRAM: BTech IT/MBATECH SEMESTER VI

COURSE: Computer Graphics Practical Experiment: 6

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:

1. Parameterization: Liang-Barsky uses parametric equations to represent the line segment,


while Cohen-Sutherland uses the concept of region codes.

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.

4. Computational Complexity: Liang-Barsky generally involves fewer arithmetic operations


compared to Cohen-Sutherland, especially in cases where the line lies predominantly outside
the window.

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.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy