2D Raster Graphics - UCSByfwang/courses/cs180/notes/2d.pdf · Bresenham’s Circle Algorithm...
Transcript of 2D Raster Graphics - UCSByfwang/courses/cs180/notes/2d.pdf · Bresenham’s Circle Algorithm...
2D Graphics
Computer Graphics
2D Raster Graphics
Integer grid
Sequential (left-right, top-down) scan
i
j
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
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)
Computer Graphics
Line drawing
Line should not have “gaps”
y=f(x)
y=f(x) if |slope|<1
x=f(y) if |slope|>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 +
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( , ) ( , )
Computer Graphics
possible
next intersection
rangepossible
current intersection
range
45o
45o
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
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
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
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
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
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
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
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
Computer Graphics
Polygon Filing
Arbitrary # of sides
Convex or concave
Holes
X
Y
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
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
Computer Graphics
Polygon Patterned Filling
Y
X
Y
X
X
logical
and
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
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
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
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)
Computer Graphics
2D examples
x
y
x’
y’
Trans+Rot
x
y
x’
y’
PTrans
x’
y’
x
y
Rot
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)
Computer Graphics
2D rotation matrix
RPP
x
y
P
x’
y’
PP
cossin
sincos
Rotate θ counterclockwise
What is the transformation R?
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
Computer Graphics
2D examples
x
y
Trans+Rot
x
y
P
Trans Rot
x
y
P
P
P’
P’
P’
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)
Computer Graphics
2D rotation matrix
RPP
x
y
P PP
cossin
sincos
Rotate θ counterclockwise
What is the transformation R?
P’
-
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
Computer Graphics
2D Transformation (cont.)
Inconsistent representation for translation
Cannot be concatenated
Troublesome for
Hierarchical transforms
Interactive, incremental display
nn TTTTPRRRRP )))((( 321123
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
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
Computer Graphics
How about other transforms?
For example, reflection
Mirrored images of each other
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
Computer Graphics
Clipping Against Upright
Rectangular Window
Points
if x x x y y y
then accept otherwise reject
min max min max&
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
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
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
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
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
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
Computer Graphics
Intersections outside (0,1)
range are not valid
But intersection inside (0,1)
range might not be valid
either
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
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
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
Computer Graphics
IN OUT IN OUT
IN OUT IN OUT
Computer Graphics
1
2
3
4
1
2
3
4
5
6
Original Right boundary clipping
Computer Graphics
1
2 3
4
5
6
Bottom boundary clipping
7
1
2 3
4
5
6
Left boundary clipping
7
8
Computer Graphics
1 2
3
4
5
6
Top boundary clipping
7
8
9
Computer Graphics
Other Primitives
Use of extents (extents for a whole string,
words, individual characters)
Divide and Conquer