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

I017 CG Lab7

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)
5 views

I017 CG Lab7

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/ 6

SVKM’s NMIMS University

Mukesh Patel School of Technology Management & Engineering

PROGRAM: BTech IT/MBATECH SEMESTER VI

COURSE: Computer Graphics Practical


Experiment: 7

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.

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

PROGRAM: BTech IT/MBATECH SEMESTER VI

COURSE: Computer Graphics Practical


Experiment: 7

Student Name Ritesh Kumar Student Sap ID:- 70412100036


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

Aim:- To learn window clipping algorithm.


Assignment 7
Objective: To learn window clipping algorithm.

1. A convex polygon and a convex clipping area are given. The task is to clip polygon edges
using the Sutherland–Hodgman Algorithm. Input is in the form of vertices of the polygon in
clockwise order. Examples:

A(100,100),B(300,100),C(300,300),D(100,300) and a clipping rectangle with corners


Xmin=150,Ymin=150,Xmax=250,Ymax=250 . we could then apply the algorithm step-by-step to
determine which parts of the polygon are inside the clipping area and thus what the resulting clipped
polygon coordinates would be.

Code:
import matplotlib.pyplot as plt

def clip_line(p1, p2, edge):


x1, y1 = p1
x2, y2 = p2
x_min, y_min = edge[0]
x_max, y_max = edge[2]

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

PROGRAM: BTech IT/MBATECH SEMESTER VI

COURSE: Computer Graphics Practical


Experiment: 7

# Initialize variables
dx = x2 - x1
dy = y2 - y1
t0, t1 = 0.0, 1.0

# Clip against left edge


if dx == 0 and x1 < x_min:
return None, None
elif dx != 0:
t_left = (x_min - x1) / dx
if dx > 0:
t0 = max(t0, t_left)
else:
t1 = min(t1, t_left)

# Clip against right edge


if dx == 0 and x1 > x_max:
return None, None
elif dx != 0:
t_right = (x_max - x1) / dx
if dx > 0:
t1 = min(t1, t_right)
else:
t0 = max(t0, t_right)

# Clip against bottom edge


if dy == 0 and y1 < y_min:
return None, None
elif dy != 0:
t_bottom = (y_min - y1) / dy
if dy > 0:
t0 = max(t0, t_bottom)
else:
t1 = min(t1, t_bottom)

# Clip against top edge


if dy == 0 and y1 > y_max:
return None, None
elif dy != 0:
t_top = (y_max - y1) / dy
if dy > 0:
t1 = min(t1, t_top)

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

PROGRAM: BTech IT/MBATECH SEMESTER VI

COURSE: Computer Graphics Practical


Experiment: 7

else:
t0 = max(t0, t_top)

if t0 > t1:
return None, None

clipped_p1 = (x1 + t0 * dx, y1 + t0 * dy)


clipped_p2 = (x1 + t1 * dx, y1 + t1 * dy)

return clipped_p1, clipped_p2

def sutherland_hodgman(polygon, clip_rect):


output_list = polygon

for edge in clip_rect:


input_list = output_list
output_list = []

for i in range(len(input_list)):
p1 = input_list[i]
p2 = input_list[(i + 1) % len(input_list)]

intersection, p2_clipped = clip_line(p1, p2, edge)

if intersection:
output_list.append(intersection)

if p2_clipped:
output_list.append(p2_clipped)

return output_list

def is_inside(point, rect):


x, y = point
x_min, y_min = rect[0]
x_max, y_max = rect[2]

return x_min <= x <= x_max and y_min <= y <= y_max

def plot_polygon(polygon, color='b', label=''):


plt.plot(*zip(*polygon), color=color, label=label)

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

PROGRAM: BTech IT/MBATECH SEMESTER VI

COURSE: Computer Graphics Practical


Experiment: 7

# Example usage
polygon = [(100, 100), (300, 100), (300, 300), (100, 300)]
clip_rect = [(150, 150), (250, 150), (250, 250), (150, 250)]

clipped_polygon = sutherland_hodgman(polygon, [clip_rect])

plot_polygon(polygon, label='Original Polygon')


plot_polygon(clipped_polygon, color='g', label='Clipped Polygon')
plt.xlim([0, 400])
plt.ylim([0, 400])
plt.title('Sutherland-Hodgman Clipping')
plt.legend()
plt.show()

Output:

Conclusion: -

1. Use a more efficient data structure: Instead of iterating over each edge of the polygon and the
clipping rectangle separately, consider using more efficient data structures like doubly linked
lists or other spatial indexing structures to speed up the clipping process.

2. Implement optimizations: Explore optimization techniques such as Cohen-Sutherland line


clipping for line segments outside the clipping rectangle, reducing the number of calculations
needed.

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

PROGRAM: BTech IT/MBATECH SEMESTER VI

COURSE: Computer Graphics Practical


Experiment: 7

3. Parallelization: Investigate parallel algorithms to speed up the clipping process, especially for
large polygons or complex clipping areas. Techniques like parallel processing or GPU
acceleration could be beneficial.

You might also like