matrix 2 d

24
2D Transformations with Matrices

Upload: xyz120

Post on 04-Jul-2015

92 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Matrix 2 d

2D Transformations with Matrices

Page 2: Matrix 2 d

Matrices

=

3,32,31,3

3,22,21,2

3,12,11,1

aaa

aaa

aaa

A

• A matrix is a rectangular array of numbers.

• A general matrix will be represented by an upper-case italicised letter.

• The element on the ith row and jth column is denoted by ai,j. Note that we start indexing at 1, whereas C indexes arrays from 0.

Page 3: Matrix 2 d

• Given two matrices A and B if we want to add B to A (that is form A+B) then if A is (n×m), B must be (n×m), Otherwise, A+B is not defined.

• The addition produces a result, C = A+B, with elements:

Matrices – Addition

jijiji BAC ,,, +=

=

++++

=

+

1210

86

8473

6251

87

65

43

21

Page 4: Matrix 2 d

• Given two matrices A and B if we want to multiply B by A (that is form AB) then if A is (n×m), B must be (m×p), i.e., the number of columns in A must be equal to the number of rows in B. Otherwise, AB is not defined.

• The multiplication produces a result, C = AB, with elements:

(Basically we multiply the first row of A with the first column of B and put this in the c1,1 element of C. And so on…).

Matrices – Multiplication

∑=

=m

kkjikji baC

1,

Page 5: Matrix 2 d

=

×

9666

9555

7644

62

33

86

329

854

762

Matrices – Multiplication (Examples)

2×6+ 6×3+ 7×2=44

×

62

33

86

54

62Undefined!2x2 x 3x2 2!=3

2x2 x 2x4 x 4x4 is allowed. Result is 2x4 matrix

Page 6: Matrix 2 d

• Unlike scalar multiplication, AB ≠ BA

• Matrix multiplication distributes over addition:

A(B+C) = AB + AC

• Identity matrix for multiplication is defined as I.

• The transpose of a matrix, A, is either denoted AT or A’ is obtained by swapping the rows and columns of A:

Matrices -- Basics

=⇒

=

3,23,1

2,22,1

1,21,1

3,22,21,2

3,12,11,1 '

aa

aa

aa

Aaaa

aaaA

Page 7: Matrix 2 d

2D Geometrical Transformations

Translate

Rotate Scale

Shear

Page 8: Matrix 2 d

Translate Points

Recall.. We can translate points in the (x, y) plane to new positions by adding translation amounts to the coordinates of the points. For each point P(x, y) to be moved by dx units parallel to the x axis and by dy

units parallel to the y axis, to the new point P’(x’, y’ ). The translation has the following form:

y

x

dyy

dxx

+=+=

'

'

P(x,y)

P’(x’,y’)

dx

dy

In matrix format:

+

=

y

x

d

d

y

x

y

x

'

'

If we define the translation matrix , then we have P’ =P + T.

=

y

x

d

dT

Page 9: Matrix 2 d

Scale Points

Points can be scaled (stretched) by sx along the x axis and by sy along the y axis into the new points by the multiplications:

We can specify how much bigger or smaller by means of a “scale factor”

To double the size of an object we use a scale factor of 2, to half the size of an obejct we use a scale factor of 0.5

ysy

xsx

y

x

=='

'

P(x,y)

P’(x’,y’)

xsx• x

sy• y

y

=

y

x

s

s

y

x

y

x

0

0

'

'

If we define , then we have P’ =SP

=

y

x

s

sS

0

0

Page 10: Matrix 2 d

Rotate Points (cont.)

Points can be rotated through an angle θ about the origin:

θθθαθα

θαθα

θθθαθα

θαθα

cossin

cossinsincos

)sin()sin(|'|'

sincos

sinsincoscos

)cos()cos(|'|'

|||'|

yx

ll

lOPy

yx

ll

lOPx

lOPOP

+=+=

+=+=

−=−=

+=+=

==

P(x,y)

P’(x’,y’)

xx’

y’

y

θα

l

O

−=

y

x

y

x

θθθθ

cossin

sincos

'

'

P’ =RP

Page 11: Matrix 2 d

Review…

• Translate: P’ = P+T• Scale: P’ = SP• Rotate: P’ = RP

• Spot the odd one out…– Multiplying versus adding matrix…– Ideally, all transformations would be the same..

• easier to code

• Solution: Homogeneous Coordinates

Page 12: Matrix 2 d

Homogeneous Coordinates

For a given 2D coordinates (x, y), we introduce a third dimension:

[x, y, 1]

In general, a homogeneous coordinates for a 2D point has the form:

[x, y, W]

Two homogeneous coordinates [x, y, W] and [x’, y’, W’] are said to be of the same (or equivalent) if

x = kx’ eg: [2, 3, 6] = [4, 6, 12]y = ky’ for some k ≠ 0 where k=2W = kW’

Therefore any [x, y, W] can be normalised by dividing each element by W:[x/W, y/W, 1]

Page 13: Matrix 2 d

Homogeneous Transformations

Now, redefine the translation by using homogeneous coordinates:

Similarly, we have:

+

=

y

x

d

d

y

x

y

x

'

'

=

1100

10

01

1

'

'

y

x

d

d

y

x

y

x

PTP ×='

=

1100

00

00

1

'

'

y

x

s

s

y

x

y

x

−=

1100

0cossin

0sincos

1

'

'

y

x

y

x

θθθθ

Scaling Rotation

P’ = S × P P’ = R × P

Page 14: Matrix 2 d

Composition of 2D Transformations

1. Additivity of successive translations

We want to translate a point P to P’ by T(dx1, dy1) and then to P’’ by another T(dx2, dy2)

On the other hand, we can define T21= T(dx1, dy1) T(dx2, dy2) first, then apply T21 to P:

where

]),()[,('),('' 112222 PddTddTPddTP yxyxyx ==

PTP 21'' =

++

=

=

=

100

10

01

100

10

01

100

10

01

),(),(

21

21

1

1

2

2

112221

yy

xx

y

x

y

x

yxyx

dd

dd

d

d

d

d

ddTddTT

Page 15: Matrix 2 d

T(-1,2) T(1,-1)

(2,1)

(1,3)

(2,2)

=

−=

100

110

001

100

210

101

100

110

101

21T

Examples of Composite 2D Transformations

Page 16: Matrix 2 d

Composition of 2D Transformations (cont.)

2. Multiplicativity of successive scalings

where

PS

PssSssS

PssSssSP

yxyx

yxyx

21

1122

1122

)],(),([

]),()[,(''

=

=

=

=

=

=

100

0*0

00*

100

00

00

100

00

00

),(),(

12

12

1

1

2

2

112221

yy

xx

y

x

y

x

yxyx

ss

ss

s

s

s

s

ssSssSS

Page 17: Matrix 2 d

Composition of 2D Transformations (cont.)

3. Additivity of successive rotations

where

PR

PRR

PRRP

21

12

12

)]()([

])()[(''

===

θθθθ

+++−+

=

−=

=

100

0)cos()sin(

0)sin()cos(

100

0cossin

0sincos

100

0cossin

0sincos

)()(

1212

1212

11

11

22

22

1221

θθθθθθθθ

θθθθ

θθθθ

θθ RRR

Page 18: Matrix 2 d

Composition of 2D Transformations (cont.)

4. Different types of elementary transformations discussed above can be concatenated as well.

where

MP

PddTR

PddTRP

yx

yx

=

=

=

)],()([

]),()[('

θθ

),()( yx ddTRM θ=

Page 19: Matrix 2 d

Consider the following two questions:

1) translate a line segment P1 P2, say, by -1 units in the x direction and -2 units in the y direction.

2). Rotate a line segment P1 P2, say by θ degrees counter clockwise, about P1.

P1(1,2)

P2(3,3)

P’2

P’1

P1(1,2)

P2(3,3)P’2

P’1

θ

Page 20: Matrix 2 d

Other Than Point Transformations…

Translate Lines: translate both endpoints, then join them.

Scale or Rotate Lines: More complex. For example, consider to rotate an arbitrary line about a point P1, three steps are needed:1). Translate such that P1 is at the origin;2). Rotate;3). Translate such that the point at the origin returns to P1.

T(-1,-2) R(θ)

P1(1,2)

P2(3,3)

P2(2,1)T(1,2)P2

P2

P1

P1P1

θ

Page 21: Matrix 2 d

Another Example.

Scale

Translate

Rotate

Translate

Page 22: Matrix 2 d

Order Matters!

As we said, the order for composition of 2D geometrical transformations matters, because, in general, matrix multiplication is not commutative. However, it is easy to show that, in the following four cases, commutativity holds:

1). Translation + Translation2). Scaling + Scaling3). Rotation + Rotation4). Scaling (with sx = sy) + Rotation

just to verify case 4:

if sx = sy, M1 = M2.

−=

=

=

100

0cos*sin*

0sin*cos*

100

0cossin

0sincos

100

00

00

)(),(1

θθθθ

θθθθ

θ

yy

xx

y

x

yx

ss

ss

s

s

RssSM

−=

−=

=

100

0cos*sin*

0sin*cos*

100

00

00

100

0cossin

0sincos

),()(2

θθθθ

θθθθ

θ

yx

yx

y

x

yx

ss

ss

s

s

ssSRM

Page 23: Matrix 2 d

Rigid-Body vs. Affine Transformations

A transformation matrix of the form

where the upper 2×2 sub-matrix is orthogonal, preserves angles and lengths. Such transforms are called rigid-body transformations, because the body or object being transformed is not distorted in any way. An arbitrary sequence of rotation and translation matrices creates a matrix of this form.

The product of an arbitrary sequence of rotation, translations, and scale matrices will cause an affine transformation, which have the property of preserving parallelism of lines, but not of lengths and angles.

1002221

1211

y

x

trr

trr

Page 24: Matrix 2 d

Rigid-Body vs. Affine Transformations (cont.)

Shear transformation is also affine.

Unit cube45º

Scale in x, not in y

Rigid- bodyTransformation

AffineTransformation

Shear in the x direction Shear in the y direction

=

100

010

01 a

SH x

=

100

01

001

bSH y