Unit_3_Part_II.docx
Unit_3_Part_II.docx
Viewing Tranformation:
Viewport:
1. An area on a display device to which a window is mapped is called a viewport.
2. A viewport is a polygon viewing region in computer graphics. The viewport is an area
expressed in rendering-device-specific coordinates, e.g. pixels for screen coordinates,
in which the objects of interest are going to be rendered.
3. A viewport defines in normalized coordinates a rectangular area on the display device
where the image of the data appears. You define a viewport with the GPORT
command. You can have your graph take up the entire display device or show it in
only a portion, say the upper-right part.
General Terms:
● World coordinate – It is the Cartesian coordinate w.r.t which we define the diagram, like
Xwmin, Xwmax, Ywmin, Ywmax
● Device Coordinate –It is the screen coordinate where the objects are to be displayed, like
Xvmin, Xvmax, Yvmin, Yvmax
● Window –It is the area on the world coordinate selected for display.
● ViewPort –It is the area on the device coordinate where graphics is to be displayed.
Now the relative position of the object in Window and Viewport are same.
For x coordinate,
For y coordinate,
Where sx is the scaling factor of x coordinate and sy is the scaling factor of y coordinate
Example: Let us assume,
● for window, Xwmin = 20, Xwmax = 80, Ywmin = 40, Ywmax = 80.
● for viewport, Xvmin = 30, Xvmax = 60, Yvmin = 40, Yvmax = 60.
● Now a point ( Xw, Yw ) be ( 30, 80 ) on the window. We have to calculate that point on the
viewport
i.e ( Xv, Yv ).
● First of all, calculate the scaling factor of x coordinate Sx and the scaling factor of y coordinate
Sy using the above-mentioned formula.
Sx = ( 60 - 30 ) / ( 80 - 20 ) = 30 / 60
Sy = ( 60 - 40 ) / ( 80 - 40 ) = 20 / 40
● So, now calculate the point on the viewport ( Xv, Yv ).
Xv = 30 + ( 30 - 20 ) * ( 30 / 60 ) = 35
Yv = 40 + ( 80 - 40 ) * ( 20 / 40 ) = 60
● So, the point on window ( Xw, Yw ) = ( 30, 80 ) will be ( Xv, Yv ) = ( 35, 60 ) on viewport.
1. This transformation involves developing formulas that start with a point in the
world window, say (xw, yw).
2. The formula is used to produce a corresponding point in viewport coordinates, say
(xv, yv). We would like for this mapping to be "proportional" in the sense that if xw is
30% of the way from the left edge of the world window, then xv is 30% of the way
from the left edge of the viewport.
3. Similarly, if yw is 30% of the way from the bottom edge of the world window, then
yv is 30% of the way from the bottom edge of the viewport. The picture below
shows this proportionality.
1. The position of the viewport can be changed allowing objects to be viewed at
different positions on the Interface Window.
2. Multiple viewports can also be used to display different sections of a scene at
different screen positions. Also, by changing the dimensions of the viewport, the size
and proportions of the objects being displayed can be manipulated.
3. Thus, a zooming affect can be achieved by successively mapping different
dimensioned clipping windows on a fixed sized viewport.
4. If the aspect ratio of the world window and the viewport are different, then the image
may look distorted.
Clipping:
Clipping is the process of cutting parts of a scene or object that are outside a defined region
called the clipping window or viewport.Only the portion inside the window is visible or
processed for display.
1. Point Clipping
● Check if a point lies inside or outside a rectangular clipping window.
● If inside → display it; if outside → discard.
2. Line Clipping
● Lines are clipped to show only the visible parts inside the window.
● Algorithms decide which portions of a line to keep or discard.
3. Polygon Clipping
● Complex shapes like polygons may need trimming.
● New clipped polygons are created from intersections with the clipping window.
4. Text Clipping
● If a text is partially outside, either clip the text or discard the entire word/character.
5. Curve Clipping
● Similar to line clipping but for curves like circles, ellipses, or splines.
Point Clipping
Clipping a point from a given window is very easy. Consider the following figure, where the
rectangle indicates the window.
Point clipping is the process of determining whether a point lies inside or outside a
predefined clipping window (usually a rectangle or box).
Point clipping tells us whether the given point (X, Y) is within the given window or not; and
decides whether we will use the minimum and maximum coordinates of the window.
Clipping Region:
Let’s assume the rectangular clipping window is defined as:
● XminX_{min}Xmin: Left boundary
● XmaxX_{max}Xmax: Right boundary
● YminY_{min}Ymin: Bottom boundary
● YmaxY_{max}Ymax: Top boundary
Point Clipping Condition:
A point P(x,y)P(x, y)P(x,y) is inside the clipping region if and only if:
Step-by-Step Algorithm:
Example:
Given:
● Clipping Window:
Xmin=10,Xmax=100X_{min} = 10, X_{max} = 100Xmin=10,Xmax=100
Ymin=20,Ymax=80Y_{min} = 20, Y_{max} = 80Ymin=20,Ymax=80
Points:
Results:
✅ ✅
Point X Range Check Y Range Check Result
❌ ✅
(30, 50) 10 ≤ 30 ≤ 100 20 ≤ 50 ≤ 80 Accepted
❌ ✅
(5, 25) 5 < 10 20 ≤ 25 ≤ 80 Rejected
✅ ❌
(110,70) 110 > 100 20 ≤ 70 ≤ 80 Rejected
(90, 90) 10 ≤ 90 ≤ 100 90 > 80 Rejected
The X-coordinate of the given point is inside the window, if X lies in between Wx1 ≤ X ≤
Wx2. Same way, Y coordinate of the given point is inside the window, if Y lies in between
Wy1 ≤ Y ≤ Wy2.
Line Clipping:
Line clipping is the process of removing portions of lines that fall outside a clipping
window, and keeping the visible parts that lie inside the window. The concept of line
clipping is same as point clipping. In line clipping, we will cut the portion of line which is
outside of window and keep only the portion that is inside the window.
Clipping Window:
Usually rectangular, defined by:
● Xmin,XmaxX_{min}, X_{max}Xmin,Xmax
● Ymin,YmaxY_{min}, Y_{max}Ymin,Ymax
A line segment is defined by its two endpoints (x1,y1)(x_1, y_1)(x1,y1) and (x2,y2)(x_2,
y_2)(x2,y2).
Formula to check binary digits:- TBRL which can be defined as top, bottom, right, and left
accordingly.
Algorithm
Steps
1) Assign the region codes to both endpoints.
2) Perform OR operation on both of these endpoints.
3) if OR = 0000,
then it is completely visible (inside the window).
else
Perform AND operation on both these endpoints.
i) if AND ? 0000,
then the line is invisible and not inside the window. Also, it can’t
be considered for clipping.
ii) else
AND = 0000, the line is partially inside the window and considered for
clipping.
4) After confirming that the line is partially inside the window, then we find the
intersection with the boundary of the window. By using the following formula:-
Slope:- m= (y2-y1)/(x2-x1)
a) If the line passes through top or the line intersects with the top boundary of the
window.
x = x + (y_wmax – y)/m
y = y_wmax
b) If the line passes through the bottom or the line intersects with the bottom boundary of
the window.
x = x + (y_wmin – y)/m
y = y_wmin
c) If the line passes through the left region or the line intersects with the left boundary of
the window.
y = y+ (x_wmin – x)*m
x = x_wmin
d) If the line passes through the right region or the line intersects with the right boundary
of the window.
y = y + (x_wmax -x)*m
x = x_wmax
5) Now, overwrite the endpoints with a new one and update it.
6) Repeat the 4th step till your line doesn’t get completely clipped
It eliminates the line outside the viewport which means it has a table of endpoints. 4Bit
Region Code represents TBRL in each direction like
TBRL — 1 0 0 0 means
T == 1 and B, R, and L are 0’s that show the line is at the top outside the boundary.
● Line can be completely inside the window (This line should be accepted).
● Line can be completely outside of the window (This line will be completely removed
from the region).
● Line can be partially inside the window (We will find intersection point and draw
only that portion of line that is inside region).
If we draw lines, they may be in any position. For example, there are four-line segments. AB, CD,
EF and GH. The rectangle box is the viewport. Now if we see the lines, the line AB is inside the
viewport completely. The EF is completely outside, so we can ignore EF. But for CD and GH we
need special care. The C’D (part of CD) is inside the viewport, and (G’H’) part of GH is inside the
viewport.
After clipping, it will be look like −
The Cohen-Sutherland Line Clipping Algorithm is used to determine which parts of a line segment
lie inside a defined rectangular clipping window and which parts lie outside. The algorithm divides
the 2D space into 9 regions. The middle region represents the area inside the clipping window, and
the surrounding 8 regions represent the space outside.
By using a simple 4-bit region code (TBRL), the algorithm can efficiently determine whether a line
lies entirely inside, entirely outside, or partially inside the clipping window.
The 4-bit region code is used to identify the position of a point relative to the clipping window. Each
bit in the code represents a direction (Top, Bottom, Right, Left). Here is how the code works:
Any point can be in one of these regions, and based on its location, the algorithm decides whether to
display or clip the line.
● Assign Region Codes − First, we calculate the region codes for both endpoints of the line. If
both points have a region code of 0000, the line lies entirely inside the clipping window and is
drawn without any further processing.
● Check for Trivial Rejection − If the bitwise AND of the region codes of the two points is not
equal to 0000, the line is entirely outside the window and is discarded.
● Check for Partial Visibility − If one of the endpoints has a region code of 0000 but the other
does not, or if both endpoints have non-zero region codes, the line is partially inside the
window. In this case, the algorithm calculates the intersection points with the clipping
boundaries and adjusts the line to fit within the window.
● Repeat as Necessary − If a line is partially inside the window, the process repeats for the
shortened line segments until all parts of the line are either inside or discarded.
Let us go through an example to see how the Cohen-Sutherland Line Clipping Algorithm works in
practice.
Problem
We are given a line with endpoints P1(70, 20) and P2(100, 10), and we need to clip this line against
a clipping window with the following boundaries:
Since 50 ≤ 70 ≤ 80 and 10 ≤ 20 ≤ 40, the point P1 lies inside the window, so its region code is 0000.
For P2(100, 10): x = 100, which is greater than xwmax = 80. The y-coordinate is on the
boundary ywmin = 10.
Therefore, the region code for P2 is 0010 (indicating that it is to the right of the window).
Since the region code of P1 is 0000 and the region code of P2 is 0010, the line is partially inside the
window. We now need to find the intersection point where the line crosses the window boundary.
Next, we find the intersection point on the right boundary (x = 80). The equation of the line is −
● y - y1 = m(x - x1)
● y - 20 = (-1/3)(80 - 70)
● y - 20 = (-1/3) * 10
The visible portion of the line is between P1(70, 20) and the intersection point M(80, 16.67). The
portion of the line beyond M is outside the window and is discarded.
So in this example, we clipped the line P1(70, 20) and P2(100, 10) against a window with
boundaries (50, 10) and (80, 40). We calculated the intersection point on the right boundary and
determined the visible portion of the line that lies within the clipping window.
Conclusion
In this chapter, we covered the basic principles of clipping, how the 4-bit region codes work, and
how to determine whether a line is fully inside, fully outside, or partially inside a clipping window.
We also walked through an example to understand the steps involved in calculating intersection
points and determining the visible portion of a line.
Polygon clipping is the process of removing certain parts of a polygon that fall outside a specified
rectangular boundary. We call this the viewport. The aim is to display only the part of the polygon
that is within the window. Any portion of the polygon that extends outside the window will be
clipped off, ensuring that the polygon fits neatly inside the viewing area.
Sutherland-Hodgman Algorithm:
The Sutherland-Hodgman Algorithm clips a polygon one edge at a time. The clipping process
involves four stages:
● Left Clipping − Clipping the parts of the polygon that are outside the left edge of the window.
● Right Clipping − Clipping the parts of the polygon that are outside the right edge of the
window.
● Top Clipping − Clipping the parts of the polygon that are outside the top edge of the window.
● Bottom Clipping − Clipping the parts of the polygon that are outside the bottom edge of the
window.
The key characteristic of this algorithm is that the output of each clipping step becomes the input for
the next step. This way, by the end of all four steps, any part of the polygon that lies outside the
window will be removed.
How the Sutherland-Hodgman Algorithm Works?
The Sutherland-Hodgman Algorithm clips each polygon edge based on its relationship to the
clipping window. It follows a set of rules to determine which parts of the polygon to keep and which
parts to discard. The process can be broken down into four cases −
Case 1: Edge from Inside to Outside − If the edge of the polygon goes from inside the window
to outside, the point where the edge exits the window is calculated. This point becomes part of the
clipped polygon. The portion of the edge outside the window is discarded.
Case 2: Edge from Outside to Inside − If the edge goes from outside the window to inside, the
point where the edge enters the window is calculated. Then both this point and the following vertex
inside the window are kept. The portion outside is discarded.
Case 3: Edge Completely Inside − If the entire edge of the polygon lies inside the window, both
vertices of the edge are retained.
Case 4: Edge Completely Outside − If the edge lies completely outside the window, it is discarded
entirely.
These four cases allow the algorithm to process each edge of the polygon and determine whether it
should be kept, clipped, or partially retained. This process is repeated for all four edges of the
window (left, right, top, and bottom).
Steps of the Sutherland-Hodgman Algorithm
The Sutherland-Hodgman Algorithm works through a series of clipping operations. Each step uses
the output from the previous step as its input. Let us break down the steps −
Consider the polygon and window on which we will apply clipping.
Problem:
● V1(25, 30)
● V2(40, 45)
● V3(55, 30)
● V4(40, 15)
First, we perform clipping against the left boundary (xmin = 30). The polygon edges are:
V1(25, 30) to V2(40, 45): This edge crosses from left to inside, so we calculate the intersection
point and update the edge as P(30, 35) to V2(40, 45).
V2(40, 45) to V3(55, 30): This edge at right of the left edge, so no clipping is needed.
V3(55, 30) to V4(40, 15): This edge at right of the left edge, so no clipping is needed.
V4(40, 15) to V1(25, 30): This edge crosses from inside to left, so we calculate the intersection
point and update the edge as Q(30, 25) to P(30, 35).
Step 2: Right Clipping
V2(40, 45) to V3(55, 30) : Crosses right boundary, so we calculate the intersection point and
update as V2(40, 45) to R(50, 35).
Here clipping are from P to V2 and V2 to R. The intersections are T(35, 40) and U(45, 40)
The bottom part contains segments Q to V4 and V4 to S, with intersections V(35, 20) and W(45,
20)
The final clipped polygon looks like this −