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

Unit_3_Part_II.docx

The document discusses 2D and 3D transformations in computer graphics, focusing on viewing transformations, window clipping, and viewport transformations. It outlines the stages of viewing transformation, the definitions and roles of windows and viewports, and the mathematical calculations involved in mapping world coordinates to device coordinates. Additionally, it covers various clipping techniques, including point, line, polygon, and curve clipping, along with algorithms like Cohen-Sutherland for line clipping.

Uploaded by

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

Unit_3_Part_II.docx

The document discusses 2D and 3D transformations in computer graphics, focusing on viewing transformations, window clipping, and viewport transformations. It outlines the stages of viewing transformation, the definitions and roles of windows and viewports, and the mathematical calculations involved in mapping world coordinates to device coordinates. Additionally, it covers various clipping techniques, including point, line, polygon, and curve clipping, along with algorithms like Cohen-Sutherland for line clipping.

Uploaded by

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

UNIT-111-11 PART 2D AND 3D TRANSFORMATION & VIEWING

Window Clipping- Introduction, Viewing Transformation, Clipping, Point


Clipping, Line Clipping, Cohen-Sutherland Line clipping, Polygon Clipping,
Sutherland-Hodgman Algorithm, Curve Clipping .

Viewing Tranformation:

Viewing transformation is the process of converting a 3D scene or object into a 2D view on a


display screen.​
It determines what to see and how to see an object by mapping the 3D world coordinates into 2D
screen coordinates.
Stages of Viewing Transformation:
1.​ Model Transformation (Modeling Coordinates → World Coordinates)
o​ Objects are initially defined in their own local coordinates (model coordinates).
o​ They are placed into the world using translation, rotation, and scaling.
2.​ World-to-View Transformation (World Coordinates → Eye/Camera Coordinates)
o​ The whole scene is adjusted relative to a virtual camera (eye).
o​ This step involves positioning the camera and setting its direction.
3.​ Projection Transformation (Camera Coordinates → Clip Coordinates)
o​ Maps 3D view volume into a canonical 3D cube.
o​ Types:
▪​ Perspective Projection (objects farther away look smaller).
▪​ Orthographic Projection (objects remain the same size, regardless of depth).
4.​ Viewport Transformation (Clip Coordinates → Device Coordinates)
o​ Converts the 2D projection into screen coordinates.
o​ Maps to the specific area on the display device (viewport).

1.​ A world-coordinate area selected for display is called a window.


2.​ In computer graphics, a window is a graphical control element.
3.​ It consists of a visual area containing some of the graphical user interface of the
program it belongs to and is framed by a window decoration.
4.​ A window defines a rectangular area in world coordinates. You define a window with
a GWINDOW statement. You can define the window to be larger than, the same size
as, or smaller than the actual range of data values, depending on whether you want to
show all of the data or only part of the data.

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.

Window to viewport transformation:


Window to Viewport Transformation is the process of transforming 2D
world-coordinate objects to device coordinates. Objects inside the world or
clipping window are mapped to the viewport which is the area on the screen where
world coordinates are mapped to be displayed.
Window-to-Viewport transformation is the process of transforming a two-
dimensional, world-coordinate scene to device coordinates.
1.​ In particular, objects inside the world or clipping window are mapped to the
viewport. The viewport is displayed in the interface window on the screen.
2.​ In other words, the clipping window is used to select the part of the scene that is to be
displayed. The viewport then positions the scene on the output device.
Example:

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.

Mathematical Calculation of Window to Viewport:


It may be possible that the size of the Viewport is much smaller or greater than the Window. In these
cases, we have to increase or decrease the size of the Window according to the Viewport and for this,
we need some mathematical calculations.
(xw, yw): A point on Window
(xv, yv): Corresponding point on Viewport
We have to calculate the point (xv, yv)

Now the relative position of the object in Window and Viewport are same.
For x coordinate,

For y coordinate,

So, after calculating for x and y coordinate, we get

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.

Why Clipping is Needed:


●​ To display only relevant parts of a scene.
●​ To save memory and processing time.
●​ To improve rendering performance.
●​ To correctly simulate the view as per camera settings.
Types of Clipping:

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.

Viewing & Clipping


The primary use of clipping in computer graphics is to remove objects, lines, or line
segments that are outside the viewing pane. The viewing transformation is insensitive to the
position of points relative to the viewing volume − especially those points behind the viewer
− and it is necessary to remove these points before generating the view.

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:

Xmin≤x≤XmaxANDYmin≤y≤YmaxX_{min} \leq x \leq X_{max} \quad \text{AND} \quad Y_{min}


\leq y \leq Y_{max}Xmin​≤x≤Xmax​ANDYmin​≤y≤Ymax​
Otherwise, the point is outside and will be clipped (not displayed).

Step-by-Step Algorithm:

1.​ Input the coordinates of the point (x,y)(x, y)(x,y).


2.​ Input the clipping rectangle boundaries Xmin,Xmax,Ymin,YmaxX_{min}, X_{max},
Y_{min}, Y_{max}Xmin​,Xmax​,Ymin​,Ymax​.
3.​ Check:
o​ If x<Xminx < X_{min}x<Xmin​or x>Xmaxx > X_{max}x>Xmax​or y<Yminy <
Y_{min}y<Ymin​or y>Ymaxy > Y_{max}y>Ymax​, then reject (clip) the point.
o​ Else, accept (draw) the point.

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:

1.​ P1(30,50)P_1(30, 50)P1​(30,50)


2.​ P2(5,25)P_2(5, 25)P2​(5,25)
3.​ P3(110,70)P_3(110, 70)P3​(110,70)
4.​ P4(90,90)P_4(90, 90)P4​(90,90)

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​).

Cohen-Sutherland Line Clippings Algorithm:


In this algorithm, we are given 9 regions on the screen. Out of which one region is of the
window and the rest 8 regions are around it given by 4 digit binary. The division of the
regions are based on (x_max, y_max) and (x_min, y_min).
The central part is the viewing region or window, all the lines which lie within this region
are completely visible. A region code is always assigned to the endpoints of the given line.
To check whether the line is visible or not.

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.

There are three possible cases for any given line.


1.​ Completely inside the given rectangle : Bitwise OR of region of two end points of
line is 0 (Both points are inside the rectangle)
2.​ Completely outside the given rectangle : Both endpoints share at least one outside
region which implies that the line does not cross the visible region. (bitwise AND of
endpoints != 0).
3.​ Partially inside the window : Both endpoints are in different regions. In this case, the
algorithm finds one of the two points that is outside the rectangular region. The
intersection of the line from outside point and rectangular window becomes new corner
point and the algorithm repeats
There are 3 possibilities for the line −

●​ 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).

Why Line Clipping is Needed?

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 −

What is Cohen-Sutherland Line Clipping?

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.

How the 4-Bit Region Code Works?

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:

●​ T (Top) − 1 if the point is above the window, 0 otherwise.

●​ B (Bottom) − 1 if the point is below the window, 0 otherwise.

●​ R (Right) − 1 if the point is to the right of the window, 0 otherwise.

●​ L (Left) − 1 if the point is to the left of the window, 0 otherwise.


Understanding the Nine Regions

The 9 regions in the Cohen-Sutherland algorithm represent:

●​ The center (the clipping window itself).

●​ The four corners (outside the window but diagonally).

●​ The four sides (top, bottom, left, and right).

Any point can be in one of these regions, and based on its location, the algorithm decides whether to
display or clip the line.

How the Cohen-Sutherland Line Clipping Algorithm Works?

The Cohen-Sutherland Line Clipping Algorithm follows a step-by-step procedure to determine


whether a line is fully inside, fully outside, or partially inside the clipping window. Here’s the
process:

●​ 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.

Example of Cohen-Sutherland Line Clipping

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:

●​ Lower-left corner (50, 10)

●​ Upper-right corner (80, 40)

Step 1: Assign Region Codes

First, we calculate the region codes for P1 and P2.

For P1(70, 20) −

●​ xwmin = 50, xwmax = 80

●​ ywmin = 10, ywmax = 40

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).

Step 2: Check for Trivial Cases

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.

Step 3: Calculate the Intersection Point

The slope of the line P1P2 is calculated as −

●​ Slope (m) = (y2 - y1) / (x2 - x1)

●​ Slope (m) = (10 - 20) / (100 - 70) = -10 / 30 = -1/3

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

●​ y = 20 - 10/3 = 20 - 3.33 = 16.67

Thus, the intersection point is M(80, 16.67).

Step 4: Draw the Clipped Line

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 (Sutherland Hodgman Algorithm):

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.

Step 1: Left Clipping


In the first step, the algorithm clips the polygon against the left edge of the window. Any part of the
polygon that extends to the left of the window is clipped off. The output of this step is the clipped
polygon, which is passed as input to the next step.

Step 2: Right Clipping


Next, the algorithm clips the polygon against the right edge of the window. The process is like the
left clipping. This time it removes any parts of the polygon that lie outside the right boundary. The
output is again passed to the next step.
Step 3: Top Clipping
In the third step, the algorithm performs top clipping. It removes the portions of the polygon that lie
above the top boundary of the window. The resulting polygon is then passed to the final step.

Step 4: Bottom Clipping


Finally, the algorithm clips the polygon against the bottom edge of the window. Any parts of the
polygon that extend below the bottom boundary are clipped. The final output is the clipped polygon,
which now fits entirely within the window.

Problem:

We are given a rectangular window and a polygon (square) with vertices –

●​ V1(25, 30)

●​ V2(40, 45)

●​ V3(55, 30)

●​ V4(40, 15)

The clipping window is defined by –


Showing the window as ABDC –

We need to clip the polygon using the Sutherland-Hodgman Algorithm.

Step 1: Left Clipping

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

Next, we perform right clipping against xmax = 50:

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).

Then, clip V3 to V4, the intersection is S(50,25)

Step 3: Top Clipping

Now, we clip against the top boundary (y_max = 40) −

Here clipping are from P to V2 and V2 to R. The intersections are T(35, 40) and U(45, 40)

Step 4: Bottom Clipping

Finally, we clip against the bottom boundary (y_min = 20):

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 −

You might also like