Problem for Window_to_Viewport
Problem for Window_to_Viewport
e-PG Pathshala
Subject: Computer Science
Paper: Computer Graphics and Visualization
Module: 2D Viewing
Module No: CS/CGV/8
Quadrant 1– e-text
Objectives:
Understand 2D viewing pipeline
Learn how to perform, window-viewport transformations
Understand the theory behind clipping in 2D with focus on Point Clipping.
Discussion:
2D Viewing Pipeline:
Let’s start our discussion with 2D viewing pipeline. This involves a series of
transformations performed, right from creation, till the object is finally seen or displayed at a
position or a viewing region on a display device like monitor. Each transformation step
performed converts data from one coordinate system to the other. From the figure below
(Recollect discussion from the first module),
Window-Viewport Transformation:
For simplicity, both window and viewport are chosen to be rectangular in shape
whose boundaries are aligned with the coordinate axes. Because the dimensions of window
and viewport are different, we will be performing a scaling like operation. If the window is
bigger in size than the viewport, we perform a scale down operation and vice-versa. As you
can notice from the figure above, the boundaries of the window and viewport are defined by
four infinitely extending lines parallel to the coordinate axes. The window boundaries are
defined by the four lines, xwmin, xwmax, ywmin, yWmax. Where xwmin, xwmax define the two
vertical boundaries, while, ywmin and ywmax define the two horizontal boundaries of the
window. Similarly the viewport boundaries are defined by four infinitely extending lines, xvmin,
xvmax, yvmin, yvmax.
Now for a point (xw,yw) in the window, we have to identify an equivalent point in the
viewport, say (xv,yv). i.e., for every point in the window, there is an equivalent point in the
viewport. The four corner vertices of the window have got their equivalent vertex points in
the viewport, which are nothing but the four corner points of the viewport. For the point
(xwmin, ywmin) in the window, the equivalent point in the viewport is (xvmin, yvmin) and so on.
For any general point in the window, how do we find an equivalent point in the viewport? As
we can see, from the figures above, the percentage horizontal distance of the point in the
window from its left boundary, should be exactly equal to the percentage horizontal distance
of the equivalent point in the viewport from its left boundary. We can apply the same analogy
to the vertical distance of the point from its bottom boundary.
We can represent the percentage horizontal distance of the point from the left boundary as
𝑥𝑤 − 𝑥𝑤𝑚𝑖𝑛 𝑥𝑣 − 𝑥𝑣𝑚𝑖𝑛
× 100 = × 100
𝑥𝑤𝑚𝑎𝑥 − 𝑥𝑤𝑚𝑖𝑛 𝑥𝑣𝑚𝑎𝑥 − 𝑥𝑣𝑚𝑖𝑛
After cancelling 100 on both sides we have
𝑥𝑤 − 𝑥𝑤𝑚𝑖𝑛 𝑥𝑣 − 𝑥𝑣𝑚𝑖𝑛
=
𝑥𝑤𝑚𝑎𝑥 − 𝑥𝑤𝑚𝑖𝑛 𝑥𝑣𝑚𝑎𝑥 − 𝑥𝑣𝑚𝑖𝑛
Our aim to compute the values of xv and yv in terms of other variables in the equation. We
get
𝑥𝑣𝑚𝑎𝑥 − 𝑥𝑣𝑚𝑖𝑛
𝑥𝑣 = 𝑥𝑣𝑚𝑖𝑛 + × (𝑥𝑤 − 𝑥𝑤𝑚𝑖𝑛 )
𝑥𝑤𝑚𝑎𝑥 − 𝑥𝑤𝑚𝑖𝑛
We can rewrite the above equation as
Here sx is a ratio and is also the scaling factor along x-direction. If the horizontal
boundary of the window is larger than that of the viewport then sx <1 (down scaling),
otherwise sx >1 (up scaling). If both the boundaries are of same lengths then sx=1 (no
scaling).
Similarly we can represent the percentage vertical distance of the point from the
bottom boundary as
𝑦𝑤 − 𝑦𝑤𝑚𝑖𝑛 𝑦𝑣 − 𝑦𝑣𝑚𝑖𝑛
× 100 = × 100
𝑦𝑤𝑚𝑎𝑥 − 𝑦𝑤𝑚𝑖𝑛 𝑦𝑣𝑚𝑎𝑥 − 𝑦𝑣𝑚𝑖𝑛
𝑦𝑤 − 𝑦𝑤𝑚𝑖𝑛 𝑦𝑣 − 𝑦𝑣𝑚𝑖𝑛
=
𝑦𝑤𝑚𝑎𝑥 − 𝑦𝑤𝑚𝑖𝑛 𝑦𝑣𝑚𝑎𝑥 − 𝑦𝑣𝑚𝑖𝑛
And rearranging the terms as we did before for yv we get,
Here sy is a ratio and is also the scaling factor along y-direction. If the vertical
boundary of the window is larger than that of the viewport then sy <1 (down scaling),
otherwise sy >1 (up scaling). If both the boundaries are of same lengths then sy=1 (no
scaling).
It is important to note that the transformation, converts real coordinates to normalized
coordinates, which are device independent. Later the device independent coordinates are
converted to appropriate screen coordinates as shown in the figure below.
*********************************************************************************************************
Example:
Problem: For a point (1, 2) in the window, find the equivalent point in the viewport. Assume
window at [(1,1) and (4,4)] and viewport at [(-1, -1) and (1,1)].
Sol:
The point (1, 2) is identified as (xw, yw) respectively. The aim is to compute the equivalent
point (xv,yv).
In point clipping, points that are inside the rectangular region are selected for display
and points that are outside the region are discarded. Now the question is, how do we know
whether a point is inside or outside a given rectangular region? For this we should perform a
test as shown.
𝑥𝑤𝑚𝑖𝑛 ≤ 𝑥 ≤ 𝑥𝑤𝑚𝑎𝑥
𝑦𝑤𝑚𝑖𝑛 ≤ 𝑦 ≤ 𝑦𝑤𝑚𝑎𝑥
From the above two equations we can infer that if x and y satisfy the above
equations, the point is an inside point, so can be selected for display, otherwise it is an
outside point so can be discarded. In the above two equations, there are 4 inequalities,
𝑥𝑤𝑚𝑖𝑛 ≤ 𝑥, - (1)
𝑥 ≤ 𝑥𝑤𝑚𝑎𝑥 , - (2)
𝑦𝑤𝑚𝑖𝑛 ≤ 𝑦, - (3)
𝑦 ≤ 𝑦𝑤𝑚𝑎𝑥 - (4)
The x coordinate should satisfy two inequalities, 𝑥𝑤𝑚𝑖𝑛 ≤ 𝑥 , 𝑥 ≤ 𝑥𝑤𝑚𝑎𝑥 , which says
that the x coordinate should be between the two vertical boundaries xwmin and xwmax, while
the y coordinate should satisfy two other inequalities, 𝑦𝑤𝑚𝑖𝑛 ≤ 𝑦, 𝑦 ≤ 𝑦𝑤𝑚𝑎𝑥 which
means that the y-coordinate should lie between the two horizontal boundaries ywmin and
ywmax . A point (x,y) is said to be an inside point, only if all the 4 inequalities are satisfied by
the point. Let’s check how? Say for example, the first inequality, 𝑥𝑤𝑚𝑖𝑛 ≤ 𝑥 is not satisfied
by the x-coordinate, instead it satisfied the relation, 𝑥𝑤𝑚𝑖𝑛 > 𝑥 , it means that, x coordinate
is onto the left of the left boundary of the window, and so we can infer that the point is
outside the clip window region and so can be discarded. Even if one of the inequalities is not
satisfied by the point, the point can be safely ignored.
*********************************************************************************************************
Example:
Problem: Check the position of point (2,1) with respect to the clip window at [(2,2), (4,4)].
Sol: of course, the point (2,1) is outside the clip window and so can be discarded.
Discussion: Treating the given point as (x,y) and xwmin=2, xwmax=4, ywmin=2, ywmax=4,
Except the 3rd inequality, the other inequalities are satisfied by the point. So as per the
discussion above, the point is an outside point.
*********************************************************************************************************
Summary:
• Understood the steps in 2D viewing pipeline
• Learnt about window-viewport transformation
• Also learnt the idea behind clipping, and in particular point clipping