Вы находитесь на странице: 1из 77

2D Geometric

Transformations
Geometric Transformation
Definition
Changes in orientation, size, and shape by altering
coordinates of objects.
Translation, Rotation, Scaling

Motivation Why do we need geometric
transformations in CG?
As a viewing aid
As a modeling tool
As an image manipulation tool
Basic 2D Transformations
Translation


Scale


Rotation


Shear


tx x x + =
'
ty y y + =
'
sx x x =
'
sy y y =
'
sin y - cos x x =
'
cos y sin y y + =
'
y hx x x + =
'
x hy y y + =
'
Basic 2D Transformations
Translation


Scale


Rotation


Shear


tx x x + =
'
ty y y + =
'
sx x x =
'
sy y y =
'
sin y - cos x x =
'
cos y sin y y + =
'
y hx x x + =
'
x hy y y + =
'
Transformations
can be combined
(with simple algebra)
Basic 2D Transformations
Translation


Scale


Rotation


Shear


tx x x + =
'
ty y y + =
'
sx x x =
'
sy y y =
'
sin y - cos x x =
'
cos y sin y y + =
'
y hx x x + =
'
x hy y y + =
'
sy y
sx x x
=
'
= '
y
) cos sy) (y sin sx) ((x y
) sin sy) (y cos sx) ((x x
u u
u u
+ =
'
= '
Basic 2D Transformations
Translation


Scale


Rotation


Shear


tx x x + =
'
ty y y + =
'
sx x x =
'
sy y y =
'
sin y - cos x x =
'
cos y sin y y + =
'
y hx x x + =
'
x hy y y + =
'
ty ) cos sy) (y sin sx) ((x y
tx ) sin sy) (y cos sx) ((x x
+ + =
'
+ = '
u u
u u
Basic 2D Transformations
Translation


Scale


Rotation


Shear


tx x x + =
'
ty y y + =
'
sx x x =
'
sy y y =
'
sin y - cos x x =
'
cos y sin y y + =
'
y hx x x + =
'
x hy y y + =
'
Basic 2D Transformations
Translation


Scale


Rotation


Shear


tx x x + =
'
ty y y + =
'
sx x x =
'
sy y y =
'
sin y - cos x x =
'
cos y sin y y + =
'
y hx x x + =
'
x hy y y + =
'
ty ) cos sy) (y sin sx) ((x y
tx ) sin sy) (y cos sx) ((x x
+ + =
'
+ = '
u u
u u
Matrix Representation
Represent a 2D Transformation by a Matrix



Apply the Transformation to a Point
(

=
(

'
'
y
x
d c
b a
y
x
dy cx y
by ax x
+ =
'
+ =
'
(

d c
b a
Transformation
Matrix
Point
Matrix Representation
Transformations can be combined by matrix
multiplication





(

=
(

'
'
y
x
l k
j i
h g
f e
d c
b a
y
x
Matrices are a convenient and efficient way
to represent a sequence of transformations
Transformation
Matrix
22 Matrices
What types of transformations can be
represented with a 22 matrix?
2D Identity


2D Scaling
y y
x x
=
'
=
'
y sy y
x sx x
=
'
=
'
(

=
(

'
'
y
x
y
x
1 0
0 1
(

=
(

'
'
y
x
sy
sx
y
x
0
0
22 Matrices
What types of transformations can be
represented with a 22 matrix?
2D Rotation


2D Shearing
(


=
(

'
'
y
x
y
x
u u
u u
cos sin
sin cos
(

=
(

'
'
y
x
shy
shx
y
x
1
1
y x y
y x x
+ =
'
=
'
u u
u u
cos sin
sin cos
y x shy y
y shx x x
+ =
'
+ =
'
22 Matrices
What types of transformations can be
represented with a 22 matrix?
2D Mirror over Y axis


2D Mirror over (0,0)
y y
x x
=
'
=
'
y y
x x
=
'
=
'
(

=
(

'
'
y
x
y
x
1 0
0 1
(

=
(

'
'
y
x
y
x
1 0
0 1
2D Translation
2D translation can be represented by a 33
matrix
Point represented with homogeneous coordinates
tx x x + =
'
ty y y + =
'
(
(
(

(
(
(

=
(
(
(

'
'
1 1 0 0
1 0
0 1
1
y
x
ty
tx
y
x
Basic 2D Transformations
Basic 2D transformations as 3x3 Matrices
(
(
(

(
(
(

=
(
(
(

'
'
1 1 0 0
1 0
0 1
1
y
x
ty
tx
y
x
(
(
(

(
(
(


=
(
(
(

'
'
1 1 0 0
0 cos sin
0 sin cos
1
y
x
y
x
u u
u u
(
(
(

(
(
(

=
(
(
(

'
'
1 1 0 0
0 0
0 0
1
y
x
sy
sx
y
x
(
(
(

(
(
(

=
(
(
(

'
'
1 1 0 0
0 1
0 1
1
y
x
shy
shx
y
x
Translate
Shear
Scale
Rotate
Homogeneous Coordinates
Add a 3rd coordinate to every 2D point
(x, y, w) represents a point at location (x/w, y/w)
(x, y, 0) represents a point at infinity






1 2
1
2
x
y
(2, 1, 1) or (4, 2, 2) or (6, 3, 3)
Convenient Coordinate System to
Represent Many Useful Transformations
Linear Transformations
Linear transformations are combinations of
Scale
Rotation
Shear, and
Mirror
Properties of linear transformations
Satisfies:
Origin maps to origin
Lines map to lines
Parallel lines remain parallel
Ratios are preserved
) ( ) ( ) (
2 2 1 1 2 2 1 1
p T s p T s p s p s T + = +
(
(
(

(
(
(

=
(
(
(

'
'
'
w
y
x
d c
b a
w
y
x
1 0 0
0
0
Matrix Composition
Transformations can be combined by matrix
multiplication




Efficiency with premultiplication
Matrix multiplication is associative
(
(
(

|
|
|
.
|

\
|
(
(
(

(
(
(

(
(
(

=
(
(
(

'
'
'
w
y
x
1 0 0
0 sy 0
0 0 sx
1 0 0
0 cos in
0 sin - cos
1 0 0
1 0
0 1
s ty
tx
w
y
x
=
'
p
ty) T(tx, ) R(u
sy) S(sx,
p
p))) S ( R ( T ( p =
'
p S) R T ( p =
'
Matrix Composition
Rotate by u around arbitrary point (a,b)



Scale by sx, sy around arbitrary point (a,b)

T(-a,-b) ) R( b) T(a, M =
T(-a,-b) sy) S(sx, b) T(a, M =
(a,b)
(a,b)
Pivot-Point Rotation
(
(
(


+
=
(
(
(

(
(
(

(
(
(

1 0 0
sin ) cos 1 ( cos sin
sin ) cos 1 ( sin cos
1 0 0
1 0
0 1
1 0 0
0 cos sin
0 sin cos
1 0 0
1 0
0 1
u u u u
u u u u
u u
u u
r r
r r
r
r
r
r
x y
y x
y
x
y
x
( ) ( ) ( ) ( ) u u , , , ,
r r r r r r
y x R y x T R y x T =
Translate Rotate Translate
(x
r
,y
r
) (x
r
,y
r
) (x
r
,y
r
) (x
r
,y
r
)
General Fixed-Point Scaling
(
(
(

=
(
(
(

(
(
(

(
(
(

1 0 0
) 1 ( 0
) 1 ( 0
1 0 0
1 0
0 1
1 0 0
0 0
0 0
1 0 0
1 0
0 1
y f y
x f x
f
f x
f
f
s y s
s x s
y
x
s
s
y
x
y
Translate Scale Translate
(x
f
,y
f
) (x
f
,y
f
) (x
f
,y
f
) (x
f
,y
f
)
( ) ( ) ( ) ( )
y x f f f f y x f f
s s y x S y x T s s S y x T , , , ,
, ,
=
Reflection
Reflection with respect to the axis

(
(
(

1 0 0
0 1 0
0 0 1
(
(
(

1 0 0
0 1 0
0 0 1
(
(
(

1 0 0
0 1 0
0 0 1
x
y 1
3 2
1
3 2
x
y
1
3 2
1
3 2
x
y
3
1
3 2
1
2
Reflection with respect to a Line








Reflection
(
(
(

1 0 0
0 0 1
0 1 0
x
y
Shear





x = x + shx y, y = y






(
(
(

1 0 0
0 1 0
0 1
x
sh
(Shx=2)
x
y
x
y
(0,0) (1,0)
(1,1)
(0,1)
(0,0) (1,0)
(3,1) (2,1)
Clipping
Avoid Drawing Parts of Primitives Outside
Window
Window defines part of scene being viewed
Must draw geometric primitives only inside window
World
Coordinates
Clipping
Avoid Drawing Parts of Primitives Outside
Window
Window defines part of scene being viewed
Must draw geometric primitives only inside window
Clipping
Avoid Drawing Parts of Primitives Outside
Window
Points
Lines
Polygons
Circles
etc.
Point Clipping
Is Point(x,y) Inside the Clip Window?

(x, y)
wx2 wx1
wy1
wy2
Inside =
(x>=wx1) &&
(x<=wx2) &&
(y>=wy1) &&
(y<=wy2);
Line Clipping
Find the Part of a Line Inside the Clip Window

P
7
P
8
P
10
P
9
P
1
P
2
P
5
P
4
P
3
P
6
Before Clipping
Line Clipping
Find the Part of a Line Inside the Clip Window

After Clipping
P
4
P
3
P
6
P
8
P
7
P
5
Cohen-Sutherland Line
Clipping
Use Simple Tests to Classify Easy Cases First
P
7
P
8
P
10
P
9
P
1
P
2
P
5
P
4
P
3
P
6
Cohen-Sutherland Line
Clipping
Classify Some Lines Quickly by AND of Bit Codes
Representing Regions of Two Endpoints (Must Be 0)
P
10
P
5
P
6
P
9
0001 P
7
P
8
0101
0100
0110 0010
0000
1010
1000
P
1
P
2
1001
P
4
P
3
Bit 4
Bit 3
Bit 2 Bit 1
Cohen-Sutherland Line
Clipping
Classify Some Lines Quickly by AND of Bit Codes
Representing Regions of Two Endpoints (Must Be 0)
P
10
P
5
P
6
P
9
0001 P
7
P
8
0101
0100
0110 0010
0000
1010
1000
P
1
P
2
1001
P
4
P
3
Bit 4
Bit 3
Bit 2 Bit 1
Cohen-Sutherland Line
Clipping
Classify Some Lines Quickly by AND of Bit Codes
Representing Regions of Two Endpoints (Must Be 0)
P
10
P
5
P
6
P
9
0001 P
7
P
8
0101
0100
0110 0010
0000
1010
1000
1001
P
4
P
3
Bit 4
Bit 3
Bit 2 Bit 1
Cohen-Sutherland Line
Clipping
Compute Intersections with Window Boundary for Lines
That Cant be Classified Quickly
P
10
P
5
P
6
P
9
0001 P
7
P
8
0101
0100
0110 0010
0000
1010
1000
1001
P
4
P
3
Bit 4
Bit 3
Bit 2 Bit 1
Cohen-Sutherland Line
Clipping
Compute Intersections with Window Boundary for Lines
That Cant be Classified Quickly

P
10
P
5
P
6
P
9
0001 P
7
P
8
0101
0100
0110 0010
0000
1010
1000
1001
P
4
P
3
Bit 4
Bit 3
Bit 2 Bit 1
Cohen-Sutherland Line
Clipping
Compute Intersections with Window Boundary for Lines
That Cant be Classified Quickly

P
10
P
6
P
9
0001 P
7
P
8
0101
0100
0110 0010
0000
1010
1000
1001
P
4
P
3
Bit 4
Bit 3
Bit 2 Bit 1
P
5
Cohen-Sutherland Line
Clipping
Compute Intersections with Window Boundary for Lines
That Cant be Classified Quickly

P
10
P
6
P
9
0001 P
7
P
8
0101
0100
0110 0010
0000
1010
1000
1001
P
4
P
3
Bit 4
Bit 3
Bit 2 Bit 1
P
5
Cohen-Sutherland Line
Clipping
Compute Intersections with Window Boundary for Lines
That Cant be Classified Quickly
P
10
P
6
P
9
0001 P
7
P
8
0101
0100
0110 0010
0000
1010
1000
1001
P
4
P
3
Bit 4
Bit 3
Bit 2 Bit 1
P
5
Cohen-Sutherland Line
Clipping
Compute Intersections with Window Boundary for Lines
That Cant be Classified Quickly

P
10
P
6
P
9
0001 P
7
P
8
0101
0100
0110 0010
0000
1010
1000
1001
P
4
P
3
Bit 4
Bit 3
Bit 2 Bit 1
P
5
Cohen-Sutherland Line
Clipping
Compute Intersections with Window Boundary for Lines
That Cant be Classified Quickly

P
10
P
6
P
9
0001
P
8
0101
0100
0110 0010
0000
1010
1000
1001
P
4
P
3
Bit 4
Bit 3
Bit 2 Bit 1
P
5
P
7
Cohen-Sutherland Line
Clipping
Compute Intersections with Window Boundary for Lines
That Cant be Classified Quickly

P
10
P
6
P
9
0001
P
8
0101
0100
0110 0010
0000
1010
1000
1001
P
4
P
3
Bit 4
Bit 3
Bit 2 Bit 1
P
5
P
7
Cohen-Sutherland Line
Clipping
Compute Intersections with Window Boundary for Lines
That Cant be Classified Quickly
P
10
P
6
P
9
0001 0101
0100
0110 0010
0000
1010
1000
1001
P
4
P
3
Bit 4
Bit 3
Bit 2 Bit 1
P
5
P
7
P
8
Cohen-Sutherland Line
Clipping
Compute Intersections with Window Boundary for Lines
That Cant be Classified Quickly
P
10
P
6
P
9
0001 0101
0100
0110 0010
0000
1010
1000
1001
P
4
P
3
Bit 4
Bit 3
Bit 2 Bit 1
P
5
P
7
P
8
Cohen-Sutherland Line
Clipping
Compute Intersections with Window Boundary for Lines
That Cant be Classified Quickly

P
10
P
6
P
9
0001 0101
0100
0110 0010
0000
1010
1000
1001
P
4
P
3
Bit 4
Bit 3
Bit 2 Bit 1
P
5
P
7
P
8
Cohen-Sutherland Line
Clipping
Compute Intersections with Window Boundary for Lines
That Cant be Classified Quickly

P
10
P
6
P
9
0001 0101
0100
0110 0010
0000
1010
1000
1001
P
4
P
3
Bit 4
Bit 3
Bit 2 Bit 1
P
5
P
7
P
8
Cohen-Sutherland Line
Clipping
Compute Intersections with Window Boundary for Lines
That Cant be Classified Quickly
P
10
P
6
0001 0101
0100
0110 0010
0000
1010
1000
1001
P
4
P
3
Bit 4
Bit 3
Bit 2 Bit 1
P
5
P
7
P
8
P
9
Cohen-Sutherland Line
Clipping
Compute Intersections with Window Boundary for Lines
That Cant be Classified Quickly

P
6
0001 0101
0100
0110 0010
0000
1010
1000
1001
P
4
P
3
Bit 4
Bit 3
Bit 2 Bit 1
P
5
P
7
P
8
P
10
P
9
Cohen-Sutherland Line
Clipping
Compute Intersections with Window Boundary for Lines
That Cant be Classified Quickly
P
6
0001 0101
0100
0110 0010
0000
1010
1000
1001
P
4
P
3
Bit 4
Bit 3
Bit 2 Bit 1
P
5
P
7
P
8
Polygon Clipping
Find the Part of a Polygon Inside the Clip
Window?
Before Clipping
Polygon Clipping
Find the Part of a Polygon Inside the Clip
Window?
After Clipping
Sutherland-Hodgeman Polygon
Clipping
Clip to Each Window Boundary One at a Time
Sutherland-Hodgeman Polygon
Clipping
Clip to Each Window Boundary One at a Time

Sutherland-Hodgeman Polygon
Clipping
Clip to Each Window Boundary One at a Time

Sutherland-Hodgeman Polygon
Clipping
Clip to Each Window Boundary One at a Time
Sutherland-Hodgeman Polygon
Clipping
Clip to Each Window Boundary One at a Time

Clipping to a Boundary
Do Inside Test for Each Point in Sequence,
Insert New Points When Cross Window Boundary,
Remove Points Outside Window Boundary
P
1
P
2
P
3
P
4
P
5
Inside
Outside
Window
Boundary
Clipping to a Boundary
Do Inside Test for Each Point in Sequence,
Insert New Points When Cross Window Boundary,
Remove Points Outside Window Boundary
P
1
P
2
P
3
P
4
P
5
Inside
Outside
Window
Boundary
Clipping to a Boundary
Do Inside Test for Each Point in Sequence,
Insert New Points When Cross Window Boundary,
Remove Points Outside Window Boundary
P
1
P
2
P
3
P
4
P
5
Inside
Outside
Window
Boundary
Clipping to a Boundary
Do Inside Test for Each Point in Sequence,
Insert New Points When Cross Window Boundary,
Remove Points Outside Window Boundary

P
1
P
2
P
3
P
4
P
5
Inside
Outside
Window
Boundary
Clipping to a Boundary
Do Inside Test for Each Point in Sequence,
Insert New Points When Cross Window Boundary,
Remove Points Outside Window Boundary
P
1
P
2
P
3
P
4
P
5
Inside
Outside
Window
Boundary
P

Clipping to a Boundary
Do Inside Test for Each Point in Sequence,
Insert New Points When Cross Window Boundary,
Remove Points Outside Window Boundary

P
1
P
2
P
3
P
4
P
5
Inside
Outside
Window
Boundary
P

Clipping to a Boundary
Do Inside Test for Each Point in Sequence,
Insert New Points When Cross Window Boundary,
Remove Points Outside Window Boundary
P
1
P
2
P
3
P
4
P
5
Inside
Outside
Window
Boundary
P

Clipping to a Boundary
Do Inside Test for Each Point in Sequence,
Insert New Points When Cross Window Boundary,
Remove Points Outside Window Boundary

P
1
P
2
P
3
P
4
P
5
Inside
Outside
Window
Boundary
P

P

Clipping to a Boundary
Do Inside Test for Each Point in Sequence,
Insert New Points When Cross Window Boundary,
Remove Points Outside Window Boundary

P
1
P
2
Inside
Outside
Window
Boundary
P

P

Viewport Transformation
Transform 2D Geometric Primitives from Screen
Coordinate System (Projection Coordinates) to
Image Coordinate System (Device Coordinates)
Screen Image
Viewport
Window vs. Viewport
Window
World-coordinate area selected for display
What is to be viewed
Viewport
Area on the display device to which a window is
mapped
Where it is to be displayed

Viewport Transformation
Window-to-Viewport Mapping
(wx, wy)
wx2 wx1
wy1
wy2
(vx, vy)
vx2 vx1
vy1
vy2
Window Viewport
Screen Coordinates Image Coordinates
vx = vx1 + (wx wx1) * (vx2 vx1) / (wx2 wx1);
vy = vy1 + (wy wy1) * (vy2 vy1) / (wy2 wy1);
3D Translation
Translation of a Point
z y x
t z z t y y t x x + = + = + = ' , ' , '
(
(
(
(

(
(
(
(

=
(
(
(
(

1 1 0 0 0
1 0 0
0 1 0
0 0 1
1
'
'
'
z
y
x
t
t
t
z
y
x
z
y
x
x
z
y
3D Scaling
Uniform Scaling

z y x
s z z s y y s x x = = = ' , ' , '
(
(
(
(

(
(
(
(

=
(
(
(
(

1 1 0 0 0
0 0 0
0 0 0
0 0 0
1
'
'
'
z
y
x
s
s
s
z
y
x
z
y
x
x
z
y
Relative Scaling
Scaling with a Selected Fixed Position

(
(
(
(

(
(
(
(

(
(
(
(

(
(
(
(

=
(
(
(
(

=
1 1 0 0 0
1 0 0
0 1 0
0 0 1
1 0 0 0
0 0 0
0 0 0
0 0 0
1 0 0 0
1 0 0
0 1 0
0 0 1
1
'
'
'
) , , ( ) , , ( ) , , (
z
y
x
z
y
x
s
s
s
z
y
x
z
y
x
z y x T s s s S z y x T
f
f
f
z
y
x
f
f
f
f f f z y x f f f
x x x
x
z
z
z z
y
y y
y
Original position Translate Scaling Inverse Translate
3D Rotation
Coordinate-Axes Rotations
X-axis rotation
Y-axis rotation
Z-axis rotation

General 3D Rotations
Rotation about an axis that is parallel to one of the
coordinate axes
Rotation about an arbitrary axis
Coordinate-Axes Rotations
Z-Axis Rotation X-Axis Rotation Y-Axis Rotation
(
(
(
(

(
(
(
(


=
(
(
(
(

1 1 0 0 0
0 1 0 0
0 0 cos sin
0 0 sin cos
1
'
'
'
z
y
x
z
y
x
u u
u u
z
y
x
(
(
(
(

(
(
(
(

=
(
(
(
(

1 1 0 0 0
0 cos sin 0
0 sin cos 0
0 0 0 1
1
'
'
'
z
y
x
z
y
x
u u
u u
(
(
(
(

(
(
(
(

=
(
(
(
(

1 1 0 0 0
0 cos 0 sin
0 0 1 0
0 sin 0 cos
1
'
'
'
z
y
x
z
y
x
u u
u u
z
y
x
z
y
x
Order of Rotations
Order of Rotation Affects Final Position
X-axis Z-axis




Z-axis X-axis
General 3D Rotations
Rotation about an Axis that is Parallel to One of
the Coordinate Axes
Translate the object so that the rotation axis
coincides with the parallel coordinate axis
Perform the specified rotation about that axis
Translate the object so that the rotation axis is
moved back to its original position
General 3D Rotations
Rotation about an Arbitrary Axis
Basic Idea

1. Translate (x1, y1, z1) to the origin
2. Rotate (x2, y2, z2) on to the z
axis
3. Rotate the object around the z-axis
4. Rotate the axis to the original
orientation
5. Translate the rotation axis to the
original position
(x2,y2,z2)
(x1,y1,z1)
x
z
y
R
-1

T
-1

R
T
( ) ( ) ( ) ( ) ( ) ( )T R R R R R T R o | u | o u
x y z y x
1 1 1
=
Other Transformations
Reflection Relative to the xy Plane




Z-axis Shear
x
z
y
x
z
y
(
(
(
(

(
(
(
(

=
(
(
(
(

1 1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
1
z
y
x
z
y
x
'
'
'
(
(
(
(

(
(
(
(

=
(
(
(
(

1 1 0 0 0
0 1 0 0
0 1 0
0 0 1
1
'
'
'
z
y
x
b
a
z
y
x

Вам также может понравиться