Professional Documents
Culture Documents
Chapter 4
2D Viewing Algorithms
1
Coordinate Systems
y v
(u,v)
(x, y)
0 x 0 u
RM [4]-2
Window: A rectangular region Viewport: A rectangular region
in World Coordinate System. in Device Coordinate System.
Sine Function
(0,300)
1.5
0.5
sin(theta)
0
-180
-150
-120
0
-90
-60
-30
30
60
90
120
150
180
-0.5 (0,0) (360,0)
-1
-1.5
theta
RM [4]-3
Window to Viewport Transformation
We denote the boundaries of the world window by four real values
xmin, ymin, xmax, ymax denoting its left, bottom, right, top
margins respectively. Similary the boundaries of the viewport on
the screen are defined by four integer values umin, vmin, umax,
vmax. When a graphics display is generated using arbitrary
coordinates on the world window, the important problem
encountered in viewing this display on the actual viewport on the
screen is to have a function which maps every point (x,y) on the
window to a corresponding point (u,v) on the viewport. The
following window to viewport transformation achieves this
relationship.
RM [4]-4
Window to Viewport Transformation
(xmax, ymax)
(u, v)
(xmin,ymin)
(umin, vmin)
vmax vmin
d1
ymax ymin
v = d1 y + d2
d 2 vmin d1 ymin
RM [4]-6
Window to Viewport Transformation
400
0.2
0.1 100
-0.05 +0.05 250 550
u = 3000 x + 400
v = 3000 y 200
RM [4]-7
Aspect Ratio
RM [4]-8
Window to Viewport Transformation
For distortion-free mapping from the window to the viewport, we must have
c1 = d1 (in magnitude)
400
0.2
100
0.1
-0.05 +0.05 250 350
u = 1000 x + 300
RM v = 3000 y 200 [4]-9
W-V Transform (OpenGL)
Specifying a Viewport:
glViewport(umin, vmin, wid, hgt);
where,
wid=umax-umin
hgt=vmax-vmin
RM [4]-10
Line Clipping
A line is required to be clipped against a rectangular clipping
window such that the portion of the line that falls outside the
window is not displayed.
There are many possible arrangements of a segment with respect
to the window.
We therefore need an organized approach to identify the correct
situation and to compute the new end points of each clipped
segment.
Efficiency is important because a typical picture contains
thousands of line segments, each of which must be clipped against
a window.
RM [4]-11
Line Clipping
RM [4]-12
Cohen-Sutherland Algorithm
A rapid divide-and-conquer approach to the line clipping
Clipping window
A B R L
Trivial Accept
0000
0000
Trivial Reject
1010
0010
1001 1010
0000
0100
RM [4]-17
Region Codes
“Other Cases”
i. The point that lies outside the window is considered.
(This is the point whose region code is non-zero).
ii. For the above point, an edge outside which the point lies is
identified. (If a particular bit is non-zero, the corresponding edge
of the window is considered).
iii. The intersection of the line with the edge is computed. The
initial point in (i) is now replaced by the new intersection point.
Its region code is computed.
0000
Trivial Accept
0000
RM [4]-19
Computing Intersection Point (Eg.)
Q
A
P
x=xmin
y y1 x x1
The equation of the line PQ is y 2 y1 x2 x1
Q
A
P
x=xmin
RM [4]-21
Line Clipping (OpenGL)
RM [4]-22