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

Computer Graphics

Chapter 5
Geometric Transformations
Andreas Savva
2
2D Translation
Repositioning an object along a straight line path from
one co-ordinate location to another
(x,y) (x,y)
To translate a 2D position, we add translation distances t
x

and t
y
to the original coordinates (x,y) to obtain the
new coordinate position (x,y)
x= x + t
x
, y= y + t
y
T
x
y
Matrix form
x
y
t
x x
t
y y
P P T
'
| |
| | | |
= +
|
| |
'
\ . \ .
\ .
'
= +
3
2D Translation
Moving a polygon from position (a) to position (b) with
the translation vector (-5, 10), i.e.

x
y
5 10 15 20
5
10
15
20
(a)
x
y
5 10 15 20
5
10
15
20
(b)
5
10
T

| |
=
|
\ .
4
Translating a Polygon
class Point2D {
public:
GLfloat x, y;
};

void translatePoly(Point2D P[], GLint n,
GLfloat tx, GLfloat ty)
{
GLint i;
for (i=0; i<n; i++) {
P[i].x = P[i].x + tx;
P[i].y = P[i].y + ty;
}
glBegin (GL_POLYGON);
for (i=0; i<n; i++)
glVertex2f(P[i].x, P[i].y);
glEnd();
}
5
2D Rotation
Repositioning an object along a circular path in the xy-
plane
cos( ) cos cos sin sin
sin( ) cos sin sin cos
x r r r
y r r r
0 0 0
0 0 0
'
= + =
'
= + = +
x
y
(x,y)
(x,y)


r
r
The original coordinates are:
cos( )
sin( )
x r
y r

=
=
6
2D Rotation
Substituting
Matrix form
cos sin
sin cos
x x
y y
P R P
0 0
0 0
'

| | | || |
=
| | |
'
\ . \ .\ .
'
=
cos sin
sin cos
x x y
y x y
0 0
0 0
'
=
'
= +
x
y
(x,y)
(x,y)


r
r
7
2D Rotation about a Pivot position
Rotating about pivot position (x
r
, y
r
)
( ) cos ( ) sin
( ) sin ( ) cos
r r r
r r r
x x x x y y
y y x x y y
0 0
0 0
'
= +
'
= + +
x
y
(x,y)
(x,y)

r
r
x
r
y
r
8
Translating a Polygon
class Point2D {
public:
GLfloat x, y;
};
void rotatePoly(Point2D P[], GLint n,
Point2D pivot, GLdouble theta)
{
Point2D *V;
V = new Point2D[n];
GLint i;
for (i=0; i<n; i++) {
V[i].x = pivot.x + (P[i].x pivot.x) * cos(theta)
- (P[i].y pivot.y) * sin(theta);
V[i].y = pivot.y + (P[i].x pivot.x) * sin(theta)
- (P[i].y pivot.y) * cos(theta);
}
glBegin (GL_POLYGON);
for (i=0; i<n; i++
glVertex2f(V[i].x, V[i].y);
glEnd();
delete[] V;
}
9
2D Scaling
Altering the size of an object. S
x
and S
y
are the scaling
factors. If S
x
= S
y
then uniform scaling.
x
y
x xS
y yS
'
=
'
=
Matrix form
0
0
x
y
S
x x
S
y y
P S P
'
| |
| | | |
=
|
| |
'
\ . \ .
\ .
'
=
x
y
S
x
= S
y
=
S
x
= S
y
=
x x
Reduced in size and moved
closer to the origin
10
2D Scaling relative to Fixed point
Scaling relative to fixed point (x
f
, y
f
)
( )
( )
f f x
f f y
x x x x S
y y y y S
'
= +
'
= +
x
y
S
x
= , S
y
=
P
1
P
2
P
3
P
1

P
2

P
3

(x
f
, y
f
)
(1 )
(1 )
x f x
y f y
x xS x S
y yS y S
'
= +
'
= +
OR
where the additive terms x
f
(1-S
x
) and y
f
(1-S
y
) are
constants for all points in the object.
11
Translating a Polygon
class Point2D {
public:
GLfloat x, y;
};
void scalePoly(Point2D P[], GLint n, Point2D fixedPt,
GLfloat Sx, GLfloat Sy)
{
Point2D *V;
V = new Point2D[n];
GLfloat addx = fixedPt.x * (1 Sx);
GLfloat addy = fixedPt.y * (1 Sy);
GLint i;
for (i=0; i<n; i++) {
V[i].x = P[i].x * Sx + addx;
V[i].y = P[i].y * Sy + addy;
}
glBegin (GL_POLYGON);
for (i=0; i<n; i++
glVertex2f(V[i].x, V[i].y);
glEnd();
delete[] V;
}
12
Matrix Representation
Use 33 matrices to combine transformations

Translation


Rotation


Scaling
1 0
0 1
1 0 0 1 1
x
y
x t x
y t y
'
| | | || |
| | |
'
=
| | |
| | |
\ . \ .\ .
cos sin 0
sin cos 0
1 0 0 1 1
x x
y y
0 0
0 0
'

| | | || |
| | |
'
=
| | |
| | |
\ . \ .\ .
0 0
0 0
1 0 0 1 1
x
y
x S x
y S y
'
| | | || |
| | |
'
=
| | |
| | |
\ . \ .\ .
13
Inverse Transformations
Translation


Rotation


Scaling
1
1 0
0 1
0 0 1
x
y
t
T t


| |
|
=
|
|
\ .
1
cos sin 0
sin cos 0
0 0 1
R
0 0
0 0

| |
|
=
|
|
\ .
1
1
1
0 0
0 0
0 0 1
x
y
S
S
S

| |
|
=
|
|
\ .
14
Example
Consider the line with endpoints (10, 10) and (30, 25).
Translate it by t
x
= -20, t
y
= -10 and then rotate it by = 90.
x
y
(10, 10)
(30, 25)
Right-to-left
cos90 sin90 0 1 0 20
sin90 cos90 0 0 1 10
1 0 0 1 0 0 1 1
x x
y y
'

| | | || || |
| | | |
'
=
| | | |
| | | |
\ . \ .\ .\ .
15
Solution
x
y
(10, 10)
(30, 25)
cos90 sin90 0 1 0 20
sin90 cos90 0 0 1 10
1 0 0 1 0 0 1 1
0 1 0 1 0 20
1 0 0 0 1 10
0 0 1 0 0 1 1
0 1 10
1 0 20
0 0 1 1
x x
y y
x
y
x
y
'

| | | || || |
| | | |
'
=
| | | |
| | | |
\ . \ .\ .\ .

| || || |
| | |
=
| | |
| | |
\ .\ .\ .

| || |
| |
=
| |
| |
\ .\ .
16
Solution (continue)
0 1 10
1 0 20
1 0 0 1 1
x x
y y
'

| | | || |
| | |
'
=
| | |
| | |
\ . \ .\ .
Point (10, 10)
0 1 10 10 0
1 0 20 10 10
1 0 0 1 1 1
x
y
'

| | | || | | |
| | | |
'
= =
| | | |
| | | |
\ . \ .\ . \ .
Point (30, 25)
0 1 10 30 15
1 0 20 25 10
1 0 0 1 1 1
x
y
'

| | | || | | |
| | | |
'
= =
| | | |
| | | |
\ . \ .\ . \ .
x
y
(10, 10)
(30, 25)
(0, -10)
(-15, 10)
17
Result
x
y
(10, 10)
(30, 25)
(0, -10)
(-15, 10)
x
y
(10, 15)
(-10, 0)
x
y
(0, -10)
(-15, 10)
Step-by-step
T(-20, -10) R(90)
18
Exercises
Consider the following object:






1. Apply a rotation by 145 then scale it by S
x
=2 and S
y
=1.5
and then translate it by t
x
=20 and t
y
=-30.
2. Scale it by S
x
= and S
y
=2 and then rotate it by 30.
3. Apply a rotation by 90 and then another rotation by 45.
4. Apply a rotation by 135.
x
y
10
10
25
45
19
Exercises
Composite 2D Transformations
1. Translation: Show that:


2. Rotation: Show that:


3. Scaling: Show that:
1 1 2 2 1 2 1 2
( , ) ( , ) ( , )
x y x y x x y y
T t t T t t T t t t t = + +
1 2 1 2
( ) ( ) ( ) R R R u u u u = +
1 1 2 2 1 2 1 2
( , ) ( , ) ( , )
x y x y x x y y
S s s S s s S s s s s =
20
General 2D Pivot-Point Rotation
x
y
x
y
Original position
and Pivot Point
Translate Object so that
Pivot Point is at origin
Rotation about origin Translate object so that Pivot Point
is return to position (x
r
, y
r
)
x
y
(x
r ,
y
r
)
x
y
(x
r ,
y
r
)
21
General Pivot-point Rotation
1 0 cos sin 0 1 0
0 1 sin cos 0 0 1
1 0 0 1 0 0 1 0 0 1 1
( , ) ( ) ( , )
r r
r r
r r r r
x x x x
y y y y
T x y R T x y P
0 0
0 0
0
'

| | | || || || |
| | | | |
'
=
| | | | |
| | | | |
\ . \ .\ .\ .\ .
=
Using Matrices
22
Exercises
Consider the following object:






1. Apply a rotation by 60 on the Pivot Point (-10, 10) and
display it.
2. Apply a rotation by 30 on the Pivot Point (45, 10) and
display it.
3. Apply a rotation by 270 on the Pivot Point (10, 0) and
then translate it by t
x
= -20 and t
y
= 5. Display the final
result.
x
y
10
10
25
45
23
General 2D Fixed-Point Scaling
x
y
Original position
and Fixed Point
Translate Object so that
Fixed Point is at origin
Scale Object with
respect to origin
Translate Object so that Fixed Point
is return to position (x
f
, y
f
)
x
y
(x
f ,
y
f
)
x
y
(x
f ,
y
f
)
x
y
24
General 2D Fixed-Point Scaling
1 0 0 0 1 0
0 1 0 0 0 1
1 0 0 1 0 0 1 0 0 1 1
( , ) ( , ) ( , )
r x r
r y r
r r x y r r
x x s x x
y y s y y
T x y S s s T x y P
'

| | | || || || |
| | | | |
'
=
| | | | |
| | | | |
\ . \ .\ .\ .\ .
=
Using Matrices
25
Exercises
Consider the following object:






1. Scale it by s
x
= 2 and s
y
= relative to the fixed point
(140, 125) and display it.
2. Apply a rotation by 90 on the Pivot Point (50, 60) and
then scale it by s
x
= s
y
= 2 relative to the Fixed Point
(0, 200). Display the result.
3. Scale it s
x
= s
y
= relative to the Fixed Point (50, 60)
and then rotate it by 180 on the Pivot Point (50, 60).
Display the final result.
x
y
60
50
125
220
26
Order of Transformations
Object is first translated in the x direction
and then rotated by 90
x
y
Object is first rotated by 90 and then
translated in the x direction
x
y
27
Reflection
Reflection of an object about the x axis
x
y
x
y
Reflection of an object about the y axis
1 0 0
0 1 0
1 0 0 1 1
x x
y y
'
| | | || |
| | |
'
=
| | |
| | |
\ . \ .\ .
About the x axis
1 0 0
0 1 0
1 0 0 1 1
x x
y y
'

| | | || |
| | |
'
=
| | |
| | |
\ . \ .\ .
About the y axis
28
Reflection
Same as a rotation with 180
x
y
1 0 0
0 1 0
1 0 0 1 1
x x
y y
'

| | | || |
| | |
'
=
| | |
| | |
\ . \ .\ .
Relative to the coordinate origin
0 1 0
1 0 0
1 0 0 1 1
x x
y y
'
| | | || |
| | |
'
=
| | |
| | |
\ . \ .\ .
With respect to the line y =x
x
y
y =x
29
2D Shear
x-direction shear
x
x x sh y
y y
'
= +
'
=
Matrix form
1 0
0 1 0
1 0 0 1 1
x
x sh x
y y
'
| | | || |
| | |
'
=
| | |
| | |
\ . \ .\ .
x
y
Initial object
1
1
x
y
sh
x
= 2
1
1 2 3
30
2D Shear
x-direction relative to other reference line
( )
x ref
x x sh y y
y y
'
= +
'
=
Matrix form
1
0 1 0
1 0 0 1 1
x x ref
x sh sh y x
y y
'

| | | || |
| | |
'
=
| | |
| | |
\ . \ .\ .
y
x
y
1
1
y
ref
= -1
x
sh
x
= , y
ref
= -1
1
1 2 3
y
ref
= -1
31
2D Shear
y-direction shear
y
x x
y y sh x
'
=
'
= +
Matrix form
1 0 0
1 0
1 0 0 1 1
y
x x
y sh y
'
| | | || |
| | |
'
=
| | |
| | |
\ . \ .\ .
x
y
Initial object
1
1
x
y
sh
y
= 2
1
1
2
3
32
2D Shear
y-direction relative to other reference line
( )
y ref
x x
y y sh x x
'
=
'
= +
Matrix form
1 0 0
1
1 0 0 1 1
x y ref
x x
y sh sh x y
'
| | | || |
| | |
'
=
| | |
| | |
\ . \ .\ .
x
y
1
1
x
ref
= -1
sh
y
= , x
ref
= -1
y
x
1
1
2
x
ref
= -1
33
Transformations between 2D
Coordinate Systems
To translate object descriptions from xy coordinates to xy
coordinates, we set up a transformation that superimposes
the xy axes onto the xy axes. This is done in two steps:
1. Translate so that the origin (x
0
, y
0
) of the xy system is
moved to the origin (0, 0) of the xy system.
2. Rotate the x axis onto the x axis.
x
y
x
0
y
0
u
34
Transformations between 2D
Coordinate Systems
i.e.
1)


2)

Concatenating:
0
0 0 0
1 0
( , ) 0 1
0 0 1
x
T x y y

| |
|
=
|
|
\ .
cos sin 0
( ) sin cos 0
0 0 1
R
u u
u u u
| |
|
=
|
|
\ .
, 0 0
( ) ( , )
xy x y
M R T x y u
' '
=
35
Example
Find the xy-coordinates of the xy points (10, 20) and (35,
20), as shown in the figure below:
,
cos30 sin30 0 1 0 30
sin30 cos30 0 0 1 10
0 0 1 0 0 1
0.866 0.5 0 1 0 30 0.866 0.5 31
0.5 0.866 0 0 1 10 0.5 0.866 6.34
0 0 1 0 0 1 0 0 1
xy x y
M
' '

| || |
| |
=
| |
| |
\ .\ .

| || | | |
| | |
= =
| | |
| | |
\ .\ . \ .
x
y
30

10

30
(10, 20)
(35, 20)
36
(10,20)
(35,20)
0.866 0.5 31 10 12.34
0.5 0.866 6.34 20 18.66 (-12.38, 18.66)
0 0 1 1 1
0.866 0.5 31 35 9.31
0.5 0.866 6.34 20 6.16 (9.31, 6.1
0 0 1 1 1
P
P

| || | | |
| | |
' = =
| | |
| | |
\ .\ . \ .

| || | | |
| | |
' = =
| | |
| | |
\ .\ . \ .
6)
x
y
30

10

30
(10, 20)
(35, 20)
x
y
(-12.38, 18.66)
(9.31, 6.16)
37
Exercise
Find the xy-coordinates of the rectangle shown in the
figure below:
x
y
10

10

60
20

38
3D Translation
Repositioning an object along a straight line path from
one co-ordinate location to another
(x,y,z) (x,y,z)
To translate a 3D position, we add translation distances t
x

t
y
and t
z
to the original coordinates (x,y,z) to obtain
the new coordinate position (x,y)
x= x + t
x
, y= y + t
y
, z= z + t
z

Matrix form (4 4)
1 0 0
0 1 0
0 0 1
1 1
0 0 0 1
( , , )
x
y
z
x y z
t
x x
t
y y
z z
t
P T t t t P
' | |
| | | |
|
'
| |
=
|
' | |
| |
|
\ . \ .
\ .
'
=
T(t
x
, t
y
, t
z
)
x
y
z
39
3D Rotation
z-axis
The 2D z-axis rotation equations are
extended to 3D.
Matrix form
cos sin 0 0
sin cos 0 0
0 0 1 0
1 1 0 0 0 1
( )
z
x x
y y
z z
P R P
u u
u u
u
'

| | | | | |
' | | |
=
' | | |
| | |
\ . \ . \ .
'
=
x
y
z
x
y
z
cos sin
sin cos
x x y
y x y
z z
u u
u u
'
=
'
= +
'
=
40
3D Rotation
x-axis
Matrix form
1 0 0 0
0 cos sin 0
0 sin cos 0
1 1 0 0 0 1
( )
x
x x
y y
z z
P R P
u u
u u
u
'
| | | | | |
' | | |
=
' | | |
| | |
\ . \ . \ .
'
=
x
y
z
cos sin
sin cos
y y z
z y z
x x
u u
u u
'
=
'
= +
'
=
41
3D Rotation
y-axis
Matrix form
cos 0 sin 0
0 1 0 0
sin 0 cos 0
1 1 0 0 0 1
( )
y
x x
y y
z z
P R P
u u
u u
u
'
| | | | | |
' | | |
=
' | | |
| | |
\ . \ . \ .
'
=
x
y
z
sin cos
sin cos
z z x
x z x
y y
u u
u u
'
=
'
= +
'
=
42
3D Scaling
Matrix form
0 0 0
0 0 0
0 0 0
1 0 0 0 1 1
( , , )
x
y
z
x y z
x s x
y s y
z s z
P S s s s P
'
| | | || |
| | |
'
| | |
=
| | | '
| | |
\ . \ .\ .
'
=
x
y
z
x xs
y ys
z zs
'
=
'
=
'
=
x
y
z
43
Other 3D Transformations
1 0 0
0 1 0
0 0 1 0
1 1
0 0 0 1
x
y
sh
x x
sh
y y
z z
'
| |
| | | |
|
| |
'
|
= | |
'
|
| |
| |
|
\ . \ .
\ .
Reflection z-axis
1 0 0 0
0 1 0 0
0 0 1 0
1 0 0 0 1 1
x x
y y
z z
'
| | | || |
| | |
'
= | | |
'

| | |
| | |
\ . \ .\ .
Shears z-axis
x
y
z

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