25
2D Windows World Coordinates:  Coordinate system in which objects are described.                                   May be measured in feet, miles, centimeters, etc. xw yw

2D Windows - cs.carleton.edu · 2D Windows World Coordinates: Coordinate system in which objects are described. May be measured in feet, miles, centimeters, etc. xw yw. 2D Windows

  • Upload
    others

  • View
    0

  • Download
    0

Embed Size (px)

Citation preview

Page 1: 2D Windows - cs.carleton.edu · 2D Windows World Coordinates: Coordinate system in which objects are described. May be measured in feet, miles, centimeters, etc. xw yw. 2D Windows

2D WindowsWorld Coordinates:  Coordinate system in which objects are described.                                  May be measured in feet, miles, centimeters, etc.

xw

yw

Page 2: 2D Windows - cs.carleton.edu · 2D Windows World Coordinates: Coordinate system in which objects are described. May be measured in feet, miles, centimeters, etc. xw yw. 2D Windows

2D WindowsScreen Coordinates:  Coordinate system in which objects are displayed.                                  Usually measured in pixels

xs

ys

CRT Display

Page 3: 2D Windows - cs.carleton.edu · 2D Windows World Coordinates: Coordinate system in which objects are described. May be measured in feet, miles, centimeters, etc. xw yw. 2D Windows

2D Windows

xw

ywWorld coordinates are limited by specifying a world window

xwl                     xwr

ywt

ywb

Page 4: 2D Windows - cs.carleton.edu · 2D Windows World Coordinates: Coordinate system in which objects are described. May be measured in feet, miles, centimeters, etc. xw yw. 2D Windows

2D Windows

xw

ywAn “eye” coordinate system is placed at the center of this window.

xwl      xwc       xwr

ywt

ywc

ywb

xe

ye

xe = xw­xwcye = yw­ywc

Page 5: 2D Windows - cs.carleton.edu · 2D Windows World Coordinates: Coordinate system in which objects are described. May be measured in feet, miles, centimeters, etc. xw yw. 2D Windows

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

Page 6: 2D Windows - cs.carleton.edu · 2D Windows World Coordinates: Coordinate system in which objects are described. May be measured in feet, miles, centimeters, etc. xw yw. 2D Windows

2D Windows

xS

yS

GOAL:  Map world window onto the viewport.

 xvl                   xvr

yvt

yvb

xW

yW

xwl                     xwr

ywt

ywb

Page 7: 2D Windows - cs.carleton.edu · 2D Windows World Coordinates: Coordinate system in which objects are described. May be measured in feet, miles, centimeters, etc. xw yw. 2D Windows

2D Windows

xS

GOAL:  Map world window onto the viewport.

xv = xe*(xvr­xvl)/(xwr­xwl) + xvc

yv = ye*(yvt­yvb)/(ywt­ywb) + yvc

where xe = xw­xwc     ye = yw­ywc

Page 8: 2D Windows - cs.carleton.edu · 2D Windows World Coordinates: Coordinate system in which objects are described. May be measured in feet, miles, centimeters, etc. xw yw. 2D Windows

2D Clipping

World Window   xwl                                                 xwr

ywt

ywb

xvl                                                 xvr

yvt

yvb

ViewportWHERE ?????

Page 9: 2D Windows - cs.carleton.edu · 2D Windows World Coordinates: Coordinate system in which objects are described. May be measured in feet, miles, centimeters, etc. xw yw. 2D Windows

2D Clipping

P

Q

PC

QC

(PC,QC) = Clip(P,Q,Window)

Page 10: 2D Windows - cs.carleton.edu · 2D Windows World Coordinates: Coordinate system in which objects are described. May be measured in feet, miles, centimeters, etc. xw yw. 2D Windows

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

Page 11: 2D Windows - cs.carleton.edu · 2D Windows World Coordinates: Coordinate system in which objects are described. May be measured in feet, miles, centimeters, etc. xw yw. 2D Windows

Clipping in the Viewport ­­The Cohen­Sutherland Algorithm

xb = window edge

(x1,y1)

(x2,y2)

y = (y2­y1)/(x2­x1)*(x­x1) + y1

Let x = xb and compute y

Page 12: 2D Windows - cs.carleton.edu · 2D Windows World Coordinates: Coordinate system in which objects are described. May be measured in feet, miles, centimeters, etc. xw yw. 2D Windows

Clipping in the Viewport ­­The Cohen­Sutherland 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;

Page 13: 2D Windows - cs.carleton.edu · 2D Windows World Coordinates: Coordinate system in which objects are described. May be measured in feet, miles, centimeters, etc. xw yw. 2D Windows

Clipping in the Viewport ­­The Cohen­Sutherland 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;

Page 14: 2D Windows - cs.carleton.edu · 2D Windows World Coordinates: Coordinate system in which objects are described. May be measured in feet, miles, centimeters, etc. xw yw. 2D Windows

Clipping in the Viewport ­­The Cohen­Sutherland 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;

Page 15: 2D Windows - cs.carleton.edu · 2D Windows World Coordinates: Coordinate system in which objects are described. May be measured in feet, miles, centimeters, etc. xw yw. 2D Windows

Clipping in the Viewport ­­The Cohen­Sutherland 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;

Page 16: 2D Windows - cs.carleton.edu · 2D Windows World Coordinates: Coordinate system in which objects are described. May be measured in feet, miles, centimeters, etc. xw yw. 2D Windows

Clipping in the Viewport ­­The Cohen­Sutherland 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

Page 17: 2D Windows - cs.carleton.edu · 2D Windows World Coordinates: Coordinate system in which objects are described. May be measured in feet, miles, centimeters, etc. xw yw. 2D Windows

Clipping in the Viewport ­­The Cohen­Sutherland 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

Page 18: 2D Windows - cs.carleton.edu · 2D Windows World Coordinates: Coordinate system in which objects are described. May be measured in feet, miles, centimeters, etc. xw yw. 2D Windows

Clipping in the Viewport ­­The Cohen­Sutherland 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

Page 19: 2D Windows - cs.carleton.edu · 2D Windows World Coordinates: Coordinate system in which objects are described. May be measured in feet, miles, centimeters, etc. xw yw. 2D Windows

Clipping in the Viewport ­­The Cohen­Sutherland 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); } }

Page 20: 2D Windows - cs.carleton.edu · 2D Windows World Coordinates: Coordinate system in which objects are described. May be measured in feet, miles, centimeters, etc. xw yw. 2D Windows

Clipping in the Viewport ­­The Cohen­Sutherland 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

Page 21: 2D Windows - cs.carleton.edu · 2D Windows World Coordinates: Coordinate system in which objects are described. May be measured in feet, miles, centimeters, etc. xw yw. 2D Windows

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;

Page 22: 2D Windows - cs.carleton.edu · 2D Windows World Coordinates: Coordinate system in which objects are described. May be measured in feet, miles, centimeters, etc. xw yw. 2D Windows

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

Page 23: 2D Windows - cs.carleton.edu · 2D Windows World Coordinates: Coordinate system in which objects are described. May be measured in feet, miles, centimeters, etc. xw yw. 2D Windows

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

Page 24: 2D Windows - cs.carleton.edu · 2D Windows World Coordinates: Coordinate system in which objects are described. May be measured in feet, miles, centimeters, etc. xw yw. 2D Windows

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

Page 25: 2D Windows - cs.carleton.edu · 2D Windows World Coordinates: Coordinate system in which objects are described. May be measured in feet, miles, centimeters, etc. xw yw. 2D Windows

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); } }