Upload
others
View
0
Download
0
Embed Size (px)
Citation preview
2D WindowsWorld Coordinates: Coordinate system in which objects are described. May be measured in feet, miles, centimeters, etc.
xw
yw
2D WindowsScreen Coordinates: Coordinate system in which objects are displayed. Usually measured in pixels
xs
ys
CRT Display
2D Windows
xw
ywWorld coordinates are limited by specifying a world window
xwl xwr
ywt
ywb
2D Windows
xw
ywAn “eye” coordinate system is placed at the center of this window.
xwl xwc xwr
ywt
ywc
ywb
xe
ye
xe = xwxwcye = ywywc
2D Windows
xs
ys
A “viewport” is specified in the screen coordinate system centered on its own internal coordinate system
xvl xvc xvr
yvt
yvc
yvb
xv
yv
2D Windows
xS
yS
GOAL: Map world window onto the viewport.
xvl xvr
yvt
yvb
xW
yW
xwl xwr
ywt
ywb
2D Windows
xS
GOAL: Map world window onto the viewport.
xv = xe*(xvrxvl)/(xwrxwl) + xvc
yv = ye*(yvtyvb)/(ywtywb) + yvc
where xe = xwxwc ye = ywywc
2D Clipping
World Window xwl xwr
ywt
ywb
xvl xvr
yvt
yvb
ViewportWHERE ?????
2D Clipping
P
Q
PC
QC
(PC,QC) = Clip(P,Q,Window)
2D Clipping
P
Q
PC
QCP
Q
P
Q
Cases:
I. Trivial Accept
II. Trivial Reject
III. Clip
I
IIIII
Clipping Philosophy: any algorithm should be fast for cases I and II
Clipping in the Viewport The CohenSutherland Algorithm
xb = window edge
(x1,y1)
(x2,y2)
y = (y2y1)/(x2x1)*(xx1) + y1
Let x = xb and compute y
Clipping in the Viewport The CohenSutherland Algorithm
xb = window edge
(x1,y1)
(x2,y2)
Integer Arithmetic – Midpoint Subdivision
repeat { xm = (x1+x2)/2;ym =(y1+y2)/2; if (xm>xb) { x2 = xm; y2 = ym;} else {x1 = xm; y1 = ym;} } until (x1==xb||x2 == xb)
if (x1==xb) y = y1 else y = y2;
Clipping in the Viewport The CohenSutherland Algorithm
xb = window edge
(x1,y1)
(x2,y2)
Integer Arithmetic – Midpoint Subdivision
repeat { xm = (x1+x2)/2;ym =(y1+y2)/2; if (xm>xb) { x2 = xm; y2 = ym;} else {x1 = xm; y1 = ym;} } until (x1==xb||x2 == xb)
if (x1==xb) y = y1 else y = y2;
Clipping in the Viewport The CohenSutherland Algorithm
xb = window edge
(x1,y1)
(x2,y2)
Integer Arithmetic – Midpoint Subdivision
repeat { xm = (x1+x2)/2;ym =(y1+y2)/2; if (xm>xb) { x2 = xm; y2 = ym;} else {x1 = xm; y1 = ym;} } until (x1==xb||x2 == xb)
if (x1==xb) y = y1 else y = y2;
Clipping in the Viewport The CohenSutherland Algorithm
xb = window edge
(x1,y1)
(x2,y2)
Integer Arithmetic – Midpoint Subdivision
repeat { xm = (x1+x2)/2;ym =(y1+y2)/2; if (xm>xb) { x2 = xm; y2 = ym;} else {x1 =xm; y1 = ym;} } until (x1==xb||x2 == xb)
if (x1==xb) y = y1 else y = y2;
Clipping in the Viewport The CohenSutherland Algorithm
yt
yb
xl xr
1010 1000 1001
0010 0000 0001
0110 0100 0101
yt yb xl xr
4 bit code
bit i of Code(P) = 0 if point P on same side of line i as window else = 1
Clipping in the Viewport The CohenSutherland Algorithm
yt
yb
xl xr
1010 1000 1001
0010 0000 0001
0110 0100 0101
& = bitwise logical and
if Code(P) & Code(Q) != 0000then reject
if (Code(P)==0000) &&(Code(Q)== 0000)then accept
P
P
Q
Q
Clipping in the Viewport The CohenSutherland Algorithm
P
Q
QC
Far(P,Q,W) returns the point in the window W farthest from P along line segment PQ. If there is no farthest point it returns P.
P
Q
Clipping in the Viewport The CohenSutherland Algorithm
P
Q
QC
P
Q
Clip(P,Q,W) { QC = Far(P,Q,W); if (QC==P) return REJECT; else { PC = Far(Q,P,W); return (PC,QC); } }
Clipping in the Viewport The CohenSutherland Algorithm
P
Q
QC
P
Q
Clip(P,Q,W) { QC = Far(P,Q,W); if (QC==P) return REJECT; else { PC = Far(Q,P,W); return (PC,QC); } }
PC
Far(P,Q,W)Far(P,Q,W) { P1 = P; if (Code(Q) == 0000) return Q; else if (Code(P)&Code(Q) != 0000) return P;
Far(P,Q,W)Far(P,Q,W) { P1 = P; if (Code(Q) == 0000) return Q; else if (Code(P)&Code(Q) != 0000) return P; else { quit = false; do { PM = (P+Q)/2; if (Code(PM)&Code(Q)!= 0000) if (Code(PM)&Code(P) != 0000) { quit = true; Q = P1; }
P
Q
PM
Far(P,Q,W)Far(P,Q,W) { P1 = P; if (Code(Q) == 0000) return Q; else if (Code(P)&Code(Q) != 0000) return P; else { quit = false; do { PM = (P+Q)/2; if (Code(PM)&Code(Q)!= 0000) if (Code(PM)&Code(P) != 0000) { quit = true; Q = P1; } else Q = PM;
P
Q
PM
Far(P,Q,W)Far(P,Q,W) { P1 = P; if (Code(Q) == 0000) return Q; else if (Code(P)&Code(Q) != 0000) return P; else { quit = false; do { PM = (P+Q)/2; if (Code(PM)&Code(Q)!= 0000) if (Code(PM)&Code(P) != 0000) { quit = true; Q = P1; } else Q = PM; else P = PM;
P
Q
PM
Far(P,Q,W)Far(P,Q,W) { P1 = P; if (Code(Q) == 0000) return Q; else if (Code(P)&Code(Q) != 0000) return P; else { quit = false; do { PM = (P+Q)/2; if (Code(PM)&Code(Q)!= 0000) if (Code(PM)&Code(P) != 0000) { quit = true; Q = P1; } else Q = PM; else P = PM; while (|P-Q| > tolerance || !quit); } }