2D Raster Graphics - UCSByfwang/courses/cs180/notes/2d.pdf · Bresenham’s Circle Algorithm...

53
2D Graphics

Transcript of 2D Raster Graphics - UCSByfwang/courses/cs180/notes/2d.pdf · Bresenham’s Circle Algorithm...

Page 1: 2D Raster Graphics - UCSByfwang/courses/cs180/notes/2d.pdf · Bresenham’s Circle Algorithm integer operations only s i t i (0,0) (x i 1, y i 1) ...

2D Graphics

Page 2: 2D Raster Graphics - UCSByfwang/courses/cs180/notes/2d.pdf · Bresenham’s Circle Algorithm integer operations only s i t i (0,0) (x i 1, y i 1) ...

Computer Graphics

2D Raster Graphics

Integer grid

Sequential (left-right, top-down) scan

i

j

Page 3: 2D Raster Graphics - UCSByfwang/courses/cs180/notes/2d.pdf · Bresenham’s Circle Algorithm integer operations only s i t i (0,0) (x i 1, y i 1) ...

Computer Graphics

Line drawing

A very important operation

used frequently, block diagrams, bar charts,

engineering drawing, architecture plans, etc.

curves as concatenation of small line segments

Criteria

line should appear straight

illuminate nearest grid point

Page 4: 2D Raster Graphics - UCSByfwang/courses/cs180/notes/2d.pdf · Bresenham’s Circle Algorithm integer operations only s i t i (0,0) (x i 1, y i 1) ...

Computer Graphics

Line drawing

Line should terminate correctly

Line should have a constant intensity

specify both end points

instead of end point + slope +length

intensity ~ # of dots/unit length

5/4

5 4 2/

brightness adjustment

(antialiasing)

Page 5: 2D Raster Graphics - UCSByfwang/courses/cs180/notes/2d.pdf · Bresenham’s Circle Algorithm integer operations only s i t i (0,0) (x i 1, y i 1) ...

Computer Graphics

Line drawing

Line should not have “gaps”

y=f(x)

y=f(x) if |slope|<1

x=f(y) if |slope|>1

Page 6: 2D Raster Graphics - UCSByfwang/courses/cs180/notes/2d.pdf · Bresenham’s Circle Algorithm integer operations only s i t i (0,0) (x i 1, y i 1) ...

Computer Graphics

Line drawing

Line should be drawn as fast as possible

Brute-force method

DDA (digital differential analyzer)

bimy

ixixifor

bmxy

i

no

);;(

my

mbmx

bxm

bmxy

i

i

i

ii

)1(

11

1 fp *

1 fp +

1 fp +

Page 7: 2D Raster Graphics - UCSByfwang/courses/cs180/notes/2d.pdf · Bresenham’s Circle Algorithm integer operations only s i t i (0,0) (x i 1, y i 1) ...

Computer Graphics

Bresenham’s Line Algorithminteger operations only

(0,0)( , )x yi i 1 1

si

ti

if s t or s t t

else s t or s t s

i

i

0

0

0 1

0 0 2 1 2 1

slope

x x y y( , ) ( , )

Page 8: 2D Raster Graphics - UCSByfwang/courses/cs180/notes/2d.pdf · Bresenham’s Circle Algorithm integer operations only s i t i (0,0) (x i 1, y i 1) ...

Computer Graphics

possible

next intersection

rangepossible

current intersection

range

45o

45o

Page 9: 2D Raster Graphics - UCSByfwang/courses/cs180/notes/2d.pdf · Bresenham’s Circle Algorithm integer operations only s i t i (0,0) (x i 1, y i 1) ...

Computer Graphics

sdy

dxx y

t ydy

dxx

s tdy

dxx y

dx s t x dy y dx dy dx

i i i

i i i

i i i i

i i i i

( )

( ) ( )

( )

( ) ( )

1 1

1 1

1 1

1 1

1

1 1

2 1 2 1

2 2

floating point

Integer!!

(0,0)( , )x yi i 1 1

si

ti

)1( 1 ixdx

dy

Bresenham’s Line Algorithm

id

Page 10: 2D Raster Graphics - UCSByfwang/courses/cs180/notes/2d.pdf · Bresenham’s Circle Algorithm integer operations only s i t i (0,0) (x i 1, y i 1) ...

Computer Graphics

)(22

)(22

)(2)(2

2)(2

2)(2

11

11

111

1

11

iiii

iiii

iiiiii

iii

iii

yydxdydd

yydxdydd

yydxxxdydd

dxdydxydyxd

dxdydxydyxd

Bresenham’s Line Algorithm

Page 11: 2D Raster Graphics - UCSByfwang/courses/cs180/notes/2d.pdf · Bresenham’s Circle Algorithm integer operations only s i t i (0,0) (x i 1, y i 1) ...

Computer Graphics

d d dy dx y y

if d choose t

y y

d d dy dx

if d choose s

y y

d d dy

initial condition d dy dx x y

i i i i

i i

i i

i i

i i

i i

i i

1 1

1

1

1

1

1 0 0

2 2

0

1

2

0

2

2 0 0

( )

( )

( , ) ( , )

•Complexity: 1 left shift + 2 integer additions

Bresenham’s Line Algorithm

Page 12: 2D Raster Graphics - UCSByfwang/courses/cs180/notes/2d.pdf · Bresenham’s Circle Algorithm integer operations only s i t i (0,0) (x i 1, y i 1) ...

Computer Graphics

Circle Drawing

Symmetry reduces drawing to 1/8

(x,y)

(x,-y)

(y,x)(-y,x)

(-x,y)

(-x,-y)

(-y,-x) (y,-x)

x x r

y y r

c

c

cos

sin

Page 13: 2D Raster Graphics - UCSByfwang/courses/cs180/notes/2d.pdf · Bresenham’s Circle Algorithm integer operations only s i t i (0,0) (x i 1, y i 1) ...

Computer Graphics

Bresenham’s Circle Algorithminteger operations only

si

ti

(0,0)

( , )x yi i 1 1( , ) ( , )x yc c

o o

0 0

45 90

D s x y r D s D t t

D t x y r D s D t s

i i i i i i

i i i i i i

( ) ( ) ( ) | ( )| | ( )|

( ) ( ) | ( )| | ( )|

12

12 2

12

12 2

1 1

1

Page 14: 2D Raster Graphics - UCSByfwang/courses/cs180/notes/2d.pdf · Bresenham’s Circle Algorithm integer operations only s i t i (0,0) (x i 1, y i 1) ...

Computer Graphics

d D s D t D s D t

d r x y y

d r x y y

d d x y y y y

i i i i i

i i i i

i i i i

i i i i i i i

| ( )| | ( )| ( ) ( )

( ) ( )

( ) ( )

( ) ( )

2 2 1 1

2 2 2 1

4 6 2 2

21

21

212

12

12 2 2

1 12

12

1

Bresenham’s Circle Algorithm

Page 15: 2D Raster Graphics - UCSByfwang/courses/cs180/notes/2d.pdf · Bresenham’s Circle Algorithm integer operations only s i t i (0,0) (x i 1, y i 1) ...

Computer Graphics

d r x y r

if d choose t

y y d d x

if d choose s

y y d d x y

i i

i i i i i

i i

i i i i i i

1 0 0

1 1 1

1 1

3 2 0

0

4 6

0

1 4 4 6

( , ) ( , )

,

,

•Complexity: only integer and shift operations

Bresenham’s Circle Algorithm

Page 16: 2D Raster Graphics - UCSByfwang/courses/cs180/notes/2d.pdf · Bresenham’s Circle Algorithm integer operations only s i t i (0,0) (x i 1, y i 1) ...

Computer Graphics

Other primitives

Ellipse

symmetry reduces to 1/4

Bresenhem’s ellipse algorithm

Curve

difficult

approximation using short line segments

general curve forms (Bezier, B-spline, etc.)

Characters

rectangular grid patterns

Page 17: 2D Raster Graphics - UCSByfwang/courses/cs180/notes/2d.pdf · Bresenham’s Circle Algorithm integer operations only s i t i (0,0) (x i 1, y i 1) ...

Computer Graphics

Polygon Filing

Arbitrary # of sides

Convex or concave

Holes

X

Y

Page 18: 2D Raster Graphics - UCSByfwang/courses/cs180/notes/2d.pdf · Bresenham’s Circle Algorithm integer operations only s i t i (0,0) (x i 1, y i 1) ...

Computer Graphics

Scan Line Algorithm

Edge table

sort edges by scanline (using min Y)

record

x coordinate of ymin

ymax

to be addedx

( , )minx y

ymax

yi

yi1

x

Page 19: 2D Raster Graphics - UCSByfwang/courses/cs180/notes/2d.pdf · Bresenham’s Circle Algorithm integer operations only s i t i (0,0) (x i 1, y i 1) ...

Computer Graphics

Set y to smallest y in ET

Initialize AET to be Null

Repeat until AET and ET are empty

move from ET bucket y to AET those edges

whose ymin=y

sort edges in AET by x (insertion sort)

fill in pixel values in between x pairs

remove from AET those edges whose ymax = y

increment y by 1

update x for all edges in AET x x x

Scan Line Algorithm

Page 20: 2D Raster Graphics - UCSByfwang/courses/cs180/notes/2d.pdf · Bresenham’s Circle Algorithm integer operations only s i t i (0,0) (x i 1, y i 1) ...

Computer Graphics

Polygon Patterned Filling

Y

X

Y

X

X

logical

and

Page 21: 2D Raster Graphics - UCSByfwang/courses/cs180/notes/2d.pdf · Bresenham’s Circle Algorithm integer operations only s i t i (0,0) (x i 1, y i 1) ...

Computer Graphics

Pattern can be anchored at

a fixed point: transparent object moves on a

patterned background

a polygon corner: patterned object

Polygon Patterned Filling

Page 22: 2D Raster Graphics - UCSByfwang/courses/cs180/notes/2d.pdf · Bresenham’s Circle Algorithm integer operations only s i t i (0,0) (x i 1, y i 1) ...

Computer Graphics

2D Transformation

For animation, manipulation, user interaction

translation, rotation, scaling

x

y

x

y

T

T

x

y

x

y

x

y

S

S

x

y

x

y

x

y

'

'

'

'

cos sin

sin cos

'

'

0

0

Page 23: 2D Raster Graphics - UCSByfwang/courses/cs180/notes/2d.pdf · Bresenham’s Circle Algorithm integer operations only s i t i (0,0) (x i 1, y i 1) ...

Computer Graphics

A Very Common Confusion

What is being transformed? Points or coordinate

system?

For CG, pipeline operations are always applied to

features (points, lines, curves, planes)

But you can think in either way:

Points are physically moved in a fixed coordinate

system (e.g., in modeling transform), or

A coordinate system is moved, while points stay

stationary (e.g., in viewing transform)

Both interpretations are useful

Page 24: 2D Raster Graphics - UCSByfwang/courses/cs180/notes/2d.pdf · Bresenham’s Circle Algorithm integer operations only s i t i (0,0) (x i 1, y i 1) ...

Computer Graphics

2D Rigid Transformations

A rigid transformation maps one coordinate

system into another

Preserves distances and angles

To transform points from one coordinate

frame to another, find the rigid

transformation that brings the two

coordinate frames in alignment

Translate so that their origins coincide

Rotate so that their axes coincide (x with x, y

with y, and z with z)

Page 25: 2D Raster Graphics - UCSByfwang/courses/cs180/notes/2d.pdf · Bresenham’s Circle Algorithm integer operations only s i t i (0,0) (x i 1, y i 1) ...

Computer Graphics

2D examples

x

y

x’

y’

Trans+Rot

x

y

x’

y’

PTrans

x’

y’

x

y

Rot

Page 26: 2D Raster Graphics - UCSByfwang/courses/cs180/notes/2d.pdf · Bresenham’s Circle Algorithm integer operations only s i t i (0,0) (x i 1, y i 1) ...

Computer Graphics

Translate the coordinate system by (Tx,Ty)

What is the translated point?

2D Translation

y

x

T

TPP

x

y

P

(Tx,Ty)

Page 27: 2D Raster Graphics - UCSByfwang/courses/cs180/notes/2d.pdf · Bresenham’s Circle Algorithm integer operations only s i t i (0,0) (x i 1, y i 1) ...

Computer Graphics

2D rotation matrix

RPP

x

y

P

x’

y’

PP

cossin

sincos

Rotate θ counterclockwise

What is the transformation R?

Page 28: 2D Raster Graphics - UCSByfwang/courses/cs180/notes/2d.pdf · Bresenham’s Circle Algorithm integer operations only s i t i (0,0) (x i 1, y i 1) ...

Computer Graphics

2D Rigid Transformations

A rigid transformation moves an object

from one location to another location

Preserves distances and angles

To transform points from one place to

another, find the rigid transformation that

Translate so that the object moves

Rotate so that the object reorients

Page 29: 2D Raster Graphics - UCSByfwang/courses/cs180/notes/2d.pdf · Bresenham’s Circle Algorithm integer operations only s i t i (0,0) (x i 1, y i 1) ...

Computer Graphics

2D examples

x

y

Trans+Rot

x

y

P

Trans Rot

x

y

P

P

P’

P’

P’

Page 30: 2D Raster Graphics - UCSByfwang/courses/cs180/notes/2d.pdf · Bresenham’s Circle Algorithm integer operations only s i t i (0,0) (x i 1, y i 1) ...

Computer Graphics

Translate the coordinate system by (Tx,Ty)

What is the translated point?

2D Translation

y

x

T

TPP

x

y

P

(-Tx,-Ty)

(Tx,Ty)

Page 31: 2D Raster Graphics - UCSByfwang/courses/cs180/notes/2d.pdf · Bresenham’s Circle Algorithm integer operations only s i t i (0,0) (x i 1, y i 1) ...

Computer Graphics

2D rotation matrix

RPP

x

y

P PP

cossin

sincos

Rotate θ counterclockwise

What is the transformation R?

P’

-

Page 32: 2D Raster Graphics - UCSByfwang/courses/cs180/notes/2d.pdf · Bresenham’s Circle Algorithm integer operations only s i t i (0,0) (x i 1, y i 1) ...

Computer Graphics

2D Transformation

For animation, manipulation, user interaction

translation, rotation, scaling

x

y

x

y

T

T

x

y

x

y

x

y

S

S

x

y

x

y

x

y

'

'

'

'

cos sin

sin cos

'

'

0

0

Page 33: 2D Raster Graphics - UCSByfwang/courses/cs180/notes/2d.pdf · Bresenham’s Circle Algorithm integer operations only s i t i (0,0) (x i 1, y i 1) ...

Computer Graphics

2D Transformation (cont.)

Inconsistent representation for translation

Cannot be concatenated

Troublesome for

Hierarchical transforms

Interactive, incremental display

nn TTTTPRRRRP )))((( 321123

Page 34: 2D Raster Graphics - UCSByfwang/courses/cs180/notes/2d.pdf · Bresenham’s Circle Algorithm integer operations only s i t i (0,0) (x i 1, y i 1) ...

Computer Graphics

Homogeneous Coordinates

consistent representation for all three

can be concatenated & pre-computed

( , ) ( , , ),

( , , ) ( / , / )

x y wx wy w w

wx wy w wx w wy w

0

Page 35: 2D Raster Graphics - UCSByfwang/courses/cs180/notes/2d.pdf · Bresenham’s Circle Algorithm integer operations only s i t i (0,0) (x i 1, y i 1) ...

Computer Graphics

x

y

T

T

x

y

x

y

x

y

x

y

S

S

x

y

x

y

x

y

'

'

'

'

cos sin

sin cos

'

'

1

1 0

0 1

0 0 1 1

1

0

0

0 0 1 1

1

0 0

0 0

0 0 1 1

x

y TRS

x

y

'

' ( )

1 1

Page 36: 2D Raster Graphics - UCSByfwang/courses/cs180/notes/2d.pdf · Bresenham’s Circle Algorithm integer operations only s i t i (0,0) (x i 1, y i 1) ...

Computer Graphics

How about other transforms?

For example, reflection

Mirrored images of each other

Page 37: 2D Raster Graphics - UCSByfwang/courses/cs180/notes/2d.pdf · Bresenham’s Circle Algorithm integer operations only s i t i (0,0) (x i 1, y i 1) ...

Computer Graphics

Try to represent the new transform as a

composite of T, R, S

)(tan

100

0cossin

0sincos

1

a

b

),( ba

100

010

001

)(tan

100

0cossin

0sincos

1

a

b

Page 38: 2D Raster Graphics - UCSByfwang/courses/cs180/notes/2d.pdf · Bresenham’s Circle Algorithm integer operations only s i t i (0,0) (x i 1, y i 1) ...

Computer Graphics

Clipping Against Upright

Rectangular Window

Points

if x x x y y y

then accept otherwise reject

min max min max&

Page 39: 2D Raster Graphics - UCSByfwang/courses/cs180/notes/2d.pdf · Bresenham’s Circle Algorithm integer operations only s i t i (0,0) (x i 1, y i 1) ...

Computer Graphics

Clipping Against Upright

Rectangular Window

boundary window of points end:)','(),','(

line of points end:),(),,(

1',0

)''('')(

)''('')(

2211

2211

121121

121121

yxyx

yxyx

tt

yytyyyty

xxtxxxtx

Lines– trivially accepted if both end points inside

– otherwise Points

Page 40: 2D Raster Graphics - UCSByfwang/courses/cs180/notes/2d.pdf · Bresenham’s Circle Algorithm integer operations only s i t i (0,0) (x i 1, y i 1) ...

Computer Graphics

Cohen-Sutherland Line-Clipping Algorithm

1001 1000 1010

0001 0000 0010

0101 0100 0110

ymax

ymin

xmin xmax

Outcodes

bit1 --above: ymax-y

bit2 --below: y-ymin

bit3 --right of: xmax-x

bit4 --left of: x-xmin

Page 41: 2D Raster Graphics - UCSByfwang/courses/cs180/notes/2d.pdf · Bresenham’s Circle Algorithm integer operations only s i t i (0,0) (x i 1, y i 1) ...

Computer Graphics

Trivially-accept: both end points having

outcode 0000

Trivially-reject: corresponding bits in two

outcodes are set, or outcode1 & outcode2

nonzero

1001 1000 1010

0001 0000 0010

0101 0100 0110

Page 42: 2D Raster Graphics - UCSByfwang/courses/cs180/notes/2d.pdf · Bresenham’s Circle Algorithm integer operations only s i t i (0,0) (x i 1, y i 1) ...

Computer Graphics

Neither: need more testing

E.g. mid-point algorithm

divide a line segment

into two line segments

test each line independently

recursive division if necessary

guarantee to stop in O(logn) steps

( , ),( , )x y x y1 1 2 2

( , ),(( ) / ,( ) / )

(( ) / ,( ) / ),( , )

x y x x y y

x x y y x y

1 1 1 2 1 2

1 2 1 2 2 2

2 2

2 2

Page 43: 2D Raster Graphics - UCSByfwang/courses/cs180/notes/2d.pdf · Bresenham’s Circle Algorithm integer operations only s i t i (0,0) (x i 1, y i 1) ...

Computer Graphics

Cyrus-Beck (Liang-Basky) Line Clipping

Can be more efficient when intersection tests are

unavoidable

Work in the parameter (t) space to locate true

intersections before calculating 2D coordinates

Work for all kinds of clipping polygons and in 3D

Two basic steps:

find intersections (t)

classify intersections

Page 44: 2D Raster Graphics - UCSByfwang/courses/cs180/notes/2d.pdf · Bresenham’s Circle Algorithm integer operations only s i t i (0,0) (x i 1, y i 1) ...

Computer Graphics

Find intersections

0 cbyax

),( baN

oP

1P

)(tP

EP

0))(( EPtPN

0))(( EPtPN

0))(( EPtPN

)(

)(

0)()(

0))((

0))((

1

1

1

o

Eo

Eoo

Eoo

E

PPN

PPNt

PPNPPNt

PPPtPN

PtPN

Cyrus-Beck (Liang-Basky) Line Clipping

Page 45: 2D Raster Graphics - UCSByfwang/courses/cs180/notes/2d.pdf · Bresenham’s Circle Algorithm integer operations only s i t i (0,0) (x i 1, y i 1) ...

Computer Graphics

Intersections outside (0,1)

range are not valid

But intersection inside (0,1)

range might not be valid

either

Page 46: 2D Raster Graphics - UCSByfwang/courses/cs180/notes/2d.pdf · Bresenham’s Circle Algorithm integer operations only s i t i (0,0) (x i 1, y i 1) ...

Computer Graphics

Cyrus-Beck (Liang-Basky) Line Clipping

Classify intersections

leaving)ly (potential PL0)(

entering)ly (potential PE0)(

1

1

o

o

PPN

PPN

1P

0P

N1P

0P

N

Page 47: 2D Raster Graphics - UCSByfwang/courses/cs180/notes/2d.pdf · Bresenham’s Circle Algorithm integer operations only s i t i (0,0) (x i 1, y i 1) ...

Computer Graphics

Locate the largest PE point

& t>0

Locate the smallest PL point

& t<1

PE < PL for a valid line

0P

0P

0P

1P

1P1P

PL PL

PL

PL

PE

PE

PE

PE

Page 48: 2D Raster Graphics - UCSByfwang/courses/cs180/notes/2d.pdf · Bresenham’s Circle Algorithm integer operations only s i t i (0,0) (x i 1, y i 1) ...

Computer Graphics

Polygon Clipping (Sutherland-Hodgman)

Given an ordered sequence of polygon

vertices

And a convex clipping polygon

Output ordered clipped polygon vertices

Using divide-and-conquer, one clipping

edge at a time

Page 49: 2D Raster Graphics - UCSByfwang/courses/cs180/notes/2d.pdf · Bresenham’s Circle Algorithm integer operations only s i t i (0,0) (x i 1, y i 1) ...

Computer Graphics

IN OUT IN OUT

IN OUT IN OUT

Page 50: 2D Raster Graphics - UCSByfwang/courses/cs180/notes/2d.pdf · Bresenham’s Circle Algorithm integer operations only s i t i (0,0) (x i 1, y i 1) ...

Computer Graphics

1

2

3

4

1

2

3

4

5

6

Original Right boundary clipping

Page 51: 2D Raster Graphics - UCSByfwang/courses/cs180/notes/2d.pdf · Bresenham’s Circle Algorithm integer operations only s i t i (0,0) (x i 1, y i 1) ...

Computer Graphics

1

2 3

4

5

6

Bottom boundary clipping

7

1

2 3

4

5

6

Left boundary clipping

7

8

Page 52: 2D Raster Graphics - UCSByfwang/courses/cs180/notes/2d.pdf · Bresenham’s Circle Algorithm integer operations only s i t i (0,0) (x i 1, y i 1) ...

Computer Graphics

1 2

3

4

5

6

Top boundary clipping

7

8

9

Page 53: 2D Raster Graphics - UCSByfwang/courses/cs180/notes/2d.pdf · Bresenham’s Circle Algorithm integer operations only s i t i (0,0) (x i 1, y i 1) ...

Computer Graphics

Other Primitives

Use of extents (extents for a whole string,

words, individual characters)

Divide and Conquer