Graphics Reference
In-Depth Information
1.5
From Window to Viewport
One of the first bits of mathematics one runs into in a graphics program is the trans-
formation from the window to the viewport. Both the window and viewport are rep-
resentable as rectangles in the plane whose sides are parallel to the coordinate axes.
What we are looking for is a simple map from one of these rectangles to another.
Intuitively, all this amounts to is a change of scale.
The standard representation for our rectangles is as products of intervals in the
form [a,b] ¥ [c,d]. Normally, the implied assumption in the representation of an inter-
val like [a,b] is that a £ b; however, in our current context where we will be interested
in maps from one interval to another, we do not require that. It will be useful to allow
a > b. Returning to our discussion of windows and viewport, if one uses normalized
device coordinates, the viewport is a subrectangle of [0,1] ¥ [0,1]. If one considers the
viewport as a rectangle in the raster, then it has the form [m 1 ,m 2 ] ¥ [n 1 ,n 2 ], where m i
and n i are integers. There is one caveat, however. The (0,0) position in the raster has
traditionally been associated to the top left-hand corner on the screen. That means that
the y-axis has to be inverted because users always think of that axis as going up, not
down. In other words, if, say, the resolution is 800 ¥ 600 and the viewport is the entire
screen, then the viewport should be represented by the rectangle [0,799] ¥ [599,0].
Mathematically then, the search for the window-to-viewport transformation boils
down to the following: If W = [w a ,w b ] ¥ [w c ,w d ] and V = [v a ,v b ] ¥ [v c ,v d ] are the rec-
tangles that represent the window W and viewport V , respectively, then we want a
map T: W Æ V of the form
(
) =
(
()
()
)
Txy
,
T x T y
,
,
1
2
where each T i is linear. In other words, we have two one-dimensional problems of the
form:
Given intervals [a,b] and [c,d], find the linear map S: [a,b] Æ [c,d] with
S(a) = c and S(b) = d.
If S(x) =ax +b, then the stated boundary conditions for S lead to two equations in
two unknowns a and b, which are easily solved. We get that
dc
ba x
-
-
bc ad
ba
-
-
() =
Sx
+
=+ -
-
xa
ba dc
(
) .
c
-
The second form of the answer says that we send x to that point in [c,d], which is the
same percentage of the way from c to d as x is from a to b. If one remembers that
intuitive fact then one has no need to solve equations because the answer is obvious.
At any rate, we now have the following solution for T:
1
1
Ê
Ë
) ˆ
¯
(
) =
(
(
)
(
)
)
(
(
)
(
)
Tx y
,
ww vvxwvwv ww vvywvwv
b
-
+
-
,
-
+
-
.
b
a
b
a
a
b
d
c
d
c
c
d
-
-
a
d
c
Search WWH ::




Custom Search