Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
16 views

Computer Graphics Unit 3

Uploaded by

tanishaanmol5519
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Computer Graphics Unit 3

Uploaded by

tanishaanmol5519
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

RENAISSANCE UNIVERSITY, INDORE

School of Computer Science

BCA/BSC V Sem

Subject: Computer Graphics


Unit 3

CLIPPING

The word ‘Clipping’ refers to the removal of objects, lines, or line segments that are outside
the viewing pane. ‘Clipping’ is a process that subdivides elements of a picture to be
displayed into its visible and invisible parts, thus allowing us to discard the invisible parts
of the picture.

In 2-D, the clipping process can be applied to a variety of graphics primitives. Clipping can
be performed with respect to a clipping boundary, which may be a convex or concave
polygonal boundary. The specified region of the scene is called a clip window or simply
window. Internal clipping removes parts of a picture outside the window and external
clipping removes those parts that are inside a region.

Clipping is used to extract parts of a defined scene for viewing, anti-aliasing lines or object
boundaries, identifying visible surfaces in three dimensional views and many other
applications. Depending on the type of application, the clip window can be a general
polygon or a window that has curved boundaries.

Clipping algorithms can be applied in world coordinates so that the contents of the window
interior are mapped to device coordinates.

Line Clipping

Line clipping is a clipping concept in which a line that lies outside the clipping window is
removed from the clip region. As a result, only lines which are inside the view plane are
visible.
Cohen Sutherland Line Clipping

Cohen Sutherland Algorithm is one of the popular line clipping algorithms used for the
purpose.

Cohen Sutherland uses region code to clip a portion of the line which is not present in the
visible region. It divides a region into 9 columns based on (X_MAX,Y_MAX) and
(X_MIN,Y_MIN).

The central part is viewing region or window, all the lines which lie within this region are
completely visible. A region code is always assigned to endpoints of the given line.

To check whether the line is visible or not.

A line can be drawn:

a) Inside the Window, if that is the case, then no clipping is required

b) Completely outside the Window, if that is the case, then no clipping is required because
entire line isn’t in the window.

c) Partially inside or outside the window, if that is the case, then we need to find the
intersection point and clipping would take place.
Algorithm of Cohen Sutherland Line Clipping
1) First, define a window or View plane. Get coordinates from the user of a line.
2) Initialize the region code for initial and end coordinates of a line to 0000.
3) Check whether the line lies within, partially or outside the window.

o Now, Assign the region code for both the initial and end coordinates.
o After Assigning, If both the endpoints give 0000, then the line is completely
within the window.
o Else perform AND operation, if the result is not 0000, then the line is not
inside the window and that line would not be considered for clipping.
o Else the line is partially inside the window.

4) After confirming the line is partially inside the window, the next step is to find the
intersection point at the window boundary. By using the following formula:

If the line passes through the top,


x=x+(W_ymax-y)/slope ;
y=W_ymax;

If the line passes through the bottom,


x=x+(W_ymin-y)/slope ;
y=W_ymin;

if the line passes through the left region,


y=y+(W_xmin-x)*slope,
x=W_xmin;

if the line passes through the right region,


y=y+(W_xmax-x)*slope ,
x=W_xmax

5) Now, overwrite the endpoint with a new one and update it.
6) Repeat 4th step till your line doesn’t get clipped completely.
Cyrus Beck line clipping algorithm

Cyrus Beck is a line clipping algorithm that is made for convex polygons. It was originally
introduced by Cyrus and Beck in 1978. It is more efficient and faster than Cohen-
Sutherland Line Clipping Algorithm where the lines are clipped about four times.

Steps of Cyrus Beck Line Clipping Algorithm:

The Cyrus-Beck line clipping algorithm works in steps mentioned below:


1. Define the convex polygon by its edges & vertices.

2. Determine the parametric equation of the line: P(t) = P0 + t(P1 - P0), where P0 & P1 are
the endpoints of the line segment, & t is a parameter between 0 & 1.

3. For each edge of the polygon, calculate the outward-pointing normal vector (Ni).

4. Compute the dot product of each normal vector with the line direction vector (P1 - P0). If
the dot product is 0, the line is parallel to the edge; if it's greater than 0, the line points
inside the edge; & if it's less than 0, the line points outside the edge.

5. Calculate tE (entry) & tL (exit) for each edge using the parametric equation & normal
vectors. Update tE with the maximum value & tL with the minimum value.

6. If tE > tL, the line segment is completely outside the polygon & can be discarded.

7. If tE ≤ tL, clip the line segment using the tE & tL values to obtain the visible portion of the
line.

Cases

There are three primary cases to consider when applying the Cyrus-Beck line clipping
algorithm:
1. Completely inside: If both endpoints of the line segment are inside the convex polygon,
the entire line segment is visible & no clipping is required. In this case, tE ≤ 0 & tL ≥ 1.

2. Completely outside: If the line segment is entirely outside the convex polygon, it can be
discarded as it won't be visible. This happens when tE > tL.

3. Partially inside: If the line segment is partially inside the convex polygon, it needs to be
clipped. The visible portion of the line segment is determined by calculating the
intersection points using the tE & tL values. The new endpoints of the clipped line segment
are P(tE) & P(tL).

For a point P(t) on the line P1P2 to be the point of intersection with an edge Ei of a convex
clipping window:

1. Ni.[P(t) – Pi]=0
2. Ni.[P1 + (P2 – P1)t – Pt]=0
3. Ni.[P1 – Pi] + Ni.[P2 – P1]t=0
4. Ni.[P1 – Pi] + Ni.[P2 – P1]t=0
t= – Ni.[P1 – Pi] / Ni.D

Where D=P1-P2 is the vector from P1 to P2.


Polygon clipping
To clip a polygon, we cannot directly apply a line-clipping method to the individual polygon
edges because this approach would produce a series of unconnected line segments as
shown in figure .

Sutherland Hodgman Polygon clipping algorithm

Sutherland-Hodgman Algorithm is one such standard method for clipping arbitrarily


shaped polygons with a rectangular clipping window. Sutherland-Hodgman clipper actually
follows a divide & conquer strategy. It decomposes the problem of polygon-clipping against
a clip window into identical sub-problems.

There are four possible cases when processing vertices in sequence around the perimeter
of a polygon. As each pair of adjacent polygon vertices is passed to a window boundary
clipper, we make the following tests:

(1) If the first vertex is outside the window boundary and the second vertex is inside, both
the intersection point of the polygon edge with the window boundary and the second
vertex are added to the output vertex list.

(2) If both input vertices are inside the window boundary, only the second vertex is added
to the output vertex list.

(3) li the first vertex is inside the boundary and the second vertex is outside, only the edge
intersection with the window boundary is added to the output vertex list.
(4) If both input vertices are outside the window boundary, nothing is added to the output
list. These four cases are illustrated in Fig for successive pairs of polygon vertices. Onct. all
vertices have been processed for one clip window boundary, the output 11st of vertices is
clipped against the next window boundary.

In Cohen- Sutherland Algorithm we will divide the lines into following Sections-

● Visible Line: When both points (starting and ending) of the line are entirely
situated inside the window.

● Invisible Line: When both points (Starting and ending) of the line are completely
situated outside the window.

If we have (xmin, xmax) and (ymin, ymax) coordinates of the view pane (window).

Then, it should be described as-

x0, x1 > xmax

y0, y1 > ymax

x0, x1 < xmin

y0, y1 < ymin


● Clipped Line: Every Line has two endpoints. Let (x0, y0) and (x1, y1) are points of the
line. If one point of the line situated inside the window and the other one is outside
the window, then the line is known as Clipped Line.

Weiler-Atherton Polygon Clipping


When the clipped polygons have two or more separate sections, then it is the concave
polygon handled by this algorithm. The vertex-processing procedures for window
boundaries are modified so that concave polygon is displayed.

Let the clipping window be initially called clip polygon and the polygon to be clipped the
subject polygon. We start with an arbitrary vertex of the subject polygon and trace around
its border in the clockwise direction until an intersection with the clip polygon is
encountered:

1. If the edge enters the clip polygon, record the intersection point and continue to trace the
subject polygon.

2. If the edge leaves the clip polygon, record the intersection point and make a right turn to
follow the clip polygon in the same manner (i.e., treat the clip polygon as subject polygon
and the subject polygon as clip polygon and proceed as before).
Whenever our path of traversal forms a sub-polygon we output the sub-polygon as part of
the overall result. We then continue to trace the rest of the original subject polygon from a
recorded intersection point that marks the beginning of a not-yet traced edge or portion of
an edge. The algorithm terminates when the entire border of the original subject polygon
has been traced exactly once.

For example, the number in fig (a) indicates the order in which the edges and portion of
edges are traced. We begin at the starting vertex and continue along the same edge (from 1
to 2) of the subject polygon as it enters the clip polygon. As we move along the edge that is
leaving the clip polygon, we make a right turn (from 4 to 5) onto the clip polygon, which is
now considered the subject polygon. Following the same logic leads to the next right turn
(from 5 to 6) onto the current clip polygon, this is the original subject polygon. With the
next step done (from 7 to 8) in the same way, we have a sub-polygon for output in fig (b).
We then resume our traversal of the original subject polygon from the recorded
intersection point where we first changed our course. Going from 9 to 10 to 11 produces no
output. After skipping the already traversed 6 and 7, we continue with 12 and 13 and come
to an end. The fig (b) is the final result.

You might also like