Uploaded by nivoberg83

2D3D transofrmation

advertisement
60012501-3 Computer Graphics
Lecture 6: 2D and 3D
Transformations
Second Semester 1440-1441
1
Transformations
 Geometric transformations like rotation, translation, scaling,
and
projection
can
be
accomplished
with
matrix
multiplication
 Transformation means changes in orientation, size, and
shape.
Second Semester 1440-1441
2
Transformations (Cont.)
 Translation
– moving things
 Rotation
– moving about a point
by a given angle
• rotation point may be origin
or some other chosen point
 Scaling
– changing size
• relative to origin
or some other chosen point
Second Semester 1440-1441
3
2D Translation
 Repositioning an object along a straight line path from one
coordinate location to another
 (x,y)
(x’,y’)
 To translate a 2D position, we add translation distances tx
and ty to the original coordinates (x,y) to obtain the new
coordinate position (x’,y’)
x’= x + tx , y’= y + ty
y
Matrix form
 x   x   t x 
      t 
 y   y  y 
P  P  Second
T Semester 1440-1441
T
x4
2D Translation (cont.)
Example:
Let P = (5, 9), T = (tx, ty) = ( 6, 8) , so P' = (11, 17)
Note: - To translate a line translate its endpoints.
- To translate polygon translate its vertices.
- Circle or Ellipse: Translate boundary points.
Second Semester 1440-1441
5
2D Translation (cont.)
 Moving a polygon from position (a) to position (b) with
the translation vector (-5, 10), i.e.
 5 
T  
 10 
y
y
20
20
15
15
10
10
5
5
5
10
15
20
x
(a)
5
10
15
20
x
(b)
Second Semester 1440-1441
6
2D Translation (cont.)
 We think of a point as a column vector, written as a column
of numbers between parentheses.
 3 
3,2   
  2
 We add vectors by adding corresponding coordinates.
 3   1  3 1   4
      
   
  2   7  [2]  7   5 
Second Semester 1440-1441
7
2D Rotation
 Repositioning an object along a
y
circular path in the xy-plane
(x’,y’)
r
θ
r
φ
(x,y)
x
x  r cos(   )  r cos  cos   r sin  sin 
y  r sin(   )  r cos  sin   r sin  cos 
The original coordinates are: x  r cos( )
y  r sin( )
Second Semester 1440-1441
8
2D Rotation (cont.)
 Substituting x = r cos φ and y = r sin φ gives:
y
x  x cos   y sin 
y  x sin   y cos 
(x’,y’)
r
θ
r
φ
(x,y)
x
Matrix form
 x   cos 
   
 y   sin 
 sin   x 
 
cos   y 
P  R  P
Second Semester 1440-1441
9
2D Rotation Example (1)
A rotation by 45 degrees:
Note that the rotation is counterclockwise and that
cos(45◦) = sin(45◦) ≈ 0.707.
Second Semester 1440-1441
10
2D Rotation Example (2)
Rotate the line endpoints (1,2) and ( 4,3) about origin by 90
degree.
p(1,2) >>>>> p’(-2,1)
x’=1*cos 90 – 2*sin 90 = -2
Cos 90 = 0
Sin 90 = 1
y’=1*sin 90 + 2*cos 90= 1
P(4,3) >>>>>>> p’(-3,4)
x’=4*cos 90 – 3*sin 90= -3
y’=4*sin 90 + 3*cos 90= 4
Second Semester 1440-1441
11
2D Rotation about a Pivot position
 Rotating about pivot position (xr, yr)
y
r
θ
yr
xr
(x’,y’)
φ
r
(x,y)
x
x  xr  ( x  xr ) cos   ( y  yr )sin 
y  yr  ( x  xr )sin   ( y  yr ) cos 
Second Semester 1440-1441
12
Example
Rotate the line endpoints (2,1) about point (3,3) by 90
x’=3+(2-3) cos 90 – (1-3) sin 90
x’=3- (-2) =5
y’=3+(2-3) sin 90 + (1-3) cos 90
= 3-1=2
Second Semester 1440-1441
13
2D Scaling
• Altering the size of an object, Sx and Sy are
the scaling factors. If Sx = Sy then uniform
scaling.
• Sx and Sy can be any positive value.
– Values < 1 reduces the size of the object.
x  xS x
y  yS y
– Values > 1 produces and enlargement.
– If Sx and Sy is 1, then the size is
unchanged.
Second Semester 1440-1441
Matrix form
 x   S x
    0
y  
P  S  P
0  x 
 
Sy  y 
14
2D Scaling relative to Fixed point
 Scaling relative to fixed point (xf, yf)
y
x  x f  ( x  x f ) S x
y  y f  ( y  y f ) S y
(xf , yf)
P1
P1’
P2’
P2
P3’
P3
Sx = ¼ , Sy = ½ x
Second Semester 1440-1441
15
2D Scaling example (1)
 Scaling uniformly by half for each axis:
The axis-aligned scale matrix has the proportion of change in
each of the diagonal elements and zeroes in the off-diagonal
elements.
Second Semester 1440-1441
16
2D Scaling example (2)
 Scaling non-uniformly in x and y:
The scaling matrix is diagonal with non-equal elements. Note
that the square outline of the clock becomes a rectangle and
the circular face becomes an ellipse.
Second Semester 1440-1441
17
Matrix Representation
 Use 3×3 matrices to combine transformations
• Translation
• Rotation
• Scaling
 x   1 0 t x  x 
  
 
y

0
1
t
y  y 
  
 1   0 0 1  1 
  
 
 x   cos   sin  0  x 
  
 
y

sin

cos

0
  
 y 
1  0
 1 
0
1
  
 
 x   S x
  
y  0
1  0
  
0
Sy
0
Second Semester 1440-1441
0  x 
 
0  y 

1 
1
 
18
Inverse Transformations
• Translation
 1 0 t x 


T 1   0 1 t y 
0 0 1 


• Rotation
 cos  sin  0 


R 1    sin  cos  0 
 0
0
1 

• Scaling
 S1x

1
S  0

0
0
1
Sy
0
Second Semester 1440-1441
0

0

1
19
Example
 Consider the line with endpoints (10, 10) and (30, 25).
1)Translate it by tx = -20, ty = -10 and then rotate it by θ =
90º.
y
(30, 25)
(10, 10)
x
Second Semester 1440-1441
20
Result
y
(30, 25)
(-15, 10)
(10, 10)
(0, -10)
x
y
y
(10, 15)
(-15, 10)
(-10, 0)
x
(0, -10)
x
R(90º)
T(-20, -10)
Second Semester 1440-1441
21
Exercises (1)
 Consider the following object:
y
25
10
10
45
x
1.Apply a rotation by 145º then scale it by Sx=2 and Sy=1.5
and then translate it by tx=20 and ty=-30.
2.Scale it by Sx=½ and Sy=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º.
Second Semester 1440-1441
22
Exercises (2)
 Consider the following object:
y
25
10
10
45
x
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 tx = -20 and ty = 5. Display the final result.
Second Semester 1440-1441
23
Exercises (3)
 Consider the following object:
y
150
50
50
125
200
x
1. Scale it by sx = 2 and sy = ½ 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 sx = sy = 2 relative to the Fixed Point (0, 200).
Display the result.
3. Scale it sx = sy = ½ relative to the Fixed Point (50, 60) and then
rotate it by 180° on the Pivot Point (50, 60). Display the final
result.
Second Semester 1440-1441
24
Reflection
 We can reflect a vector across either of the coordinate axes by
using a scale with one negative scale factor
• A reflection about the y-axis is achieved by multiplying all
x-coordinates by -1.
Second Semester 1440-1441
25
Reflection (cont.)
• A reflection about the x-axis is achieved by multiplying all
y-coordinates by -1.
Second Semester 1440-1441
26
3D Transformations
 Methods for object modeling transformation in three
dimensions (3D) are extended from two dimensional (2D)
methods by including consideration for the z coordinate.
 3D Point:
We will consider points as column
vectors. Thus, a typical point with
coordinates (x, y, z) is represented as:
Second Semester 1440-1441
 x
 y
 
 z 
27
3D Transformations (cont.)
 The linear 3D transforms are an extension of the 2D transforms.
For example, a scale along Cartesian axes is:
Second Semester 1440-1441
28
3D Transformations (cont.)
 Rotation is considerably more complicated in 3D than in
2D, because there are more possible axes of rotation.
Second Semester 1440-1441
29
3D Transformations (cont.)
 Similarly we can construct matrices to rotate about the
x-axis and the y-axis:
Second Semester 1440-1441
30
Coordinate Axis Rotations
 The easiest rotation axes are those that parallel to the coordinate
axis.
 Positive rotation angles produce counterclockwise rotations
about a coordinate axes, if we are looking along the positive
half of the axis toward the coordinate origin.
Second Semester 1440-1441
31
3D transformations- homogeneous
coordinates
 In homogeneous coordinates, 3D transformations are
represented by 4×4 matrices:
a b
d e

g h

0 0
c
f
i
0
Second Semester 1440-1441
tx 

ty 
tz 

1
32
Coordinate Axis Rotations (cont.)
x’= x cosθ – y sinθ
y’= x sinθ + y cosθ
x’= x cosθ – y sinθ
 Z-axis rotation:
y’= x sinθ + y cosθ
z’=z
P  R z ( )  P
 x' cos 
 y '  sin 
 
 z'  0
  
1  0
Second Semester 1440-1441
 sin 
cos 
0
0
0 0  x 
0 0  y 

1 0  z 
  
0 1  1 
33
Coordinate Axis Rotations (cont.)
x’=x
 X-axis rotation:
y’= y cosθ – z sinθ
z’= y sinθ + z cosθ
P  R x ( )  P
0
 x' 1
 y ' 0 cos 
 
 z '  0 sin 
  
0
 1  0
Second Semester 1440-1441
0
 sin 
cos 
0
0  x 
0  y 

0  z 
  
1  1 
34
Coordinate Axis Rotations (cont.)
x’= z sinθ + x cosθ
 Y-axis rotation:
y’=y
z’= z cosθ – x sinθ
P  R y ( )  P
 x'  cos 
 y '  0
 
 z '   sin 
  
1  0
Second Semester 1440-1441
0 sin  0  x 
1
0
0  y 
 
0 cos  0  z 
  
0
0
1  1 
35
Download