24
7 February 2017 Week 5-2D Transformations 1 2D Transformations

2D Transformations - WordPress.com · 2/2/2017  · 7 February 2017 Week 5-2D Transformations 9 Combining transformations We have a general transformation of a point: P' = M • P

  • Upload
    others

  • View
    3

  • Download
    0

Embed Size (px)

Citation preview

Page 1: 2D Transformations - WordPress.com · 2/2/2017  · 7 February 2017 Week 5-2D Transformations 9 Combining transformations We have a general transformation of a point: P' = M • P

7 February 2017 Week 5-2D Transformations 1

2D Transformations

Page 2: 2D Transformations - WordPress.com · 2/2/2017  · 7 February 2017 Week 5-2D Transformations 9 Combining transformations We have a general transformation of a point: P' = M • P

7 February 2017 Week 5-2D Transformations 2

Matrix math

Is there a difference between possible representations?

dfce

bfae

f

e

dc

ba

dfbecfaedc

bafe

dfcebfaedb

cafe

Page 3: 2D Transformations - WordPress.com · 2/2/2017  · 7 February 2017 Week 5-2D Transformations 9 Combining transformations We have a general transformation of a point: P' = M • P

7 February 2017 Week 5-2D Transformations 3

Pick a convention

We’ll use the column-vector representation for a point.

Which implies that we use pre-multiplication of the

transformation – it appears before the point to be

transformed in the equation.

What if we needed to switch to the other convention (to use

some library, for instance)? How could we do that?

DyCx

ByAx

y

x

DC

BA

Page 4: 2D Transformations - WordPress.com · 2/2/2017  · 7 February 2017 Week 5-2D Transformations 9 Combining transformations We have a general transformation of a point: P' = M • P

7 February 2017 Week 5-2D Transformations 4

Translation

A translation moves all points in

an object along the same

straight-line path to new

positions.

The path is represented by a

vector, called the translation

or shift vector.

We can write the components:

p'x = px + tx

p'y = py + ty

or in matrix form:

P' = P + T

Page 5: 2D Transformations - WordPress.com · 2/2/2017  · 7 February 2017 Week 5-2D Transformations 9 Combining transformations We have a general transformation of a point: P' = M • P

7 February 2017 Week 5-2D Transformations 5

Rotation

A rotation repositions all

points in an object along a

circular path in the plane

centered at the pivot point.

First, we’ll assume the pivot is

at the origin.

We can write the components:

p'x = px cos – py sin

p'y = px sin + py cos

or in matrix form:

P' = R • P

Page 6: 2D Transformations - WordPress.com · 2/2/2017  · 7 February 2017 Week 5-2D Transformations 9 Combining transformations We have a general transformation of a point: P' = M • P

7 February 2017 Week 5-2D Transformations 6

More rotation

Another convention, we’ll take to be counterclockwise, as in

our example.

R, the rotation matrix, looks like:

cossin

sincosR

cossin

sincos

cossin

sincos

yx

yx

y

x

90

)1,5(),(

yx

0115

1105

5

1

90cos190sin5

90sin190cos5

Page 7: 2D Transformations - WordPress.com · 2/2/2017  · 7 February 2017 Week 5-2D Transformations 9 Combining transformations We have a general transformation of a point: P' = M • P

7 February 2017 Week 5-2D Transformations 7

Scaling

Scaling alters the size of an object. Scales are about the the origin.

Scale factors between 0 and 1 shrink objects. Scale factors greater than 1 enlarge objects.

We can write the components:

p'x = sx • px

p'y = sy • py

or in matrix form:

P' = S • P

The scale factors need not be the same in each direction.

Page 8: 2D Transformations - WordPress.com · 2/2/2017  · 7 February 2017 Week 5-2D Transformations 9 Combining transformations We have a general transformation of a point: P' = M • P

7 February 2017 Week 5-2D Transformations 8

More scaling

We write a scale matrix as:

Scaling also translates objects; away from the origin if the

scale factor is greater than 1, or towards the origin if the

scale factor is less than 1.

What does scaling by 1 do?

What is that matrix called?

What does scaling by a negative value do?

y

x

s

sS

0

0

Page 9: 2D Transformations - WordPress.com · 2/2/2017  · 7 February 2017 Week 5-2D Transformations 9 Combining transformations We have a general transformation of a point: P' = M • P

7 February 2017 Week 5-2D Transformations 9

Combining transformations

We have a general transformation of a point:

P' = M • P + A

When we scale or rotate, we set M, and A is the additive identity.

When we translate, we set A, and M is the multiplicative identity.

To combine multiple transformations, we must explicitly compute each transformed point.

It’d be nicer if we could use the same matrix operation all the time. But we’d have to combine multiplication and addition into a single operation.

Page 10: 2D Transformations - WordPress.com · 2/2/2017  · 7 February 2017 Week 5-2D Transformations 9 Combining transformations We have a general transformation of a point: P' = M • P

7 February 2017 Week 5-2D Transformations 10

A less than obvious solution

Let’s move our problem into 3D.

Let point (x, y) in 2D be represented by

point (x, y, 1) in the new space.

Scaling our new point by any value a

puts us somewhere along a

particular line: (ax, ay, a).

We can always map back to the

original 2D point by dividing by

the last coordinate.

The fact that all the points along each

line can be mapped back to the

same point in 2D gives this

coordinate system its name –

homogeneous coordinates.

x,y

w

(0,0,0)

w = 1 (x,y,1)

Page 11: 2D Transformations - WordPress.com · 2/2/2017  · 7 February 2017 Week 5-2D Transformations 9 Combining transformations We have a general transformation of a point: P' = M • P

7 February 2017 Week 5-2D Transformations 11

So what?

Well, now we can wedge some addition into our multiplicative

matrix.

Our point now has three coordinates. So our matrix is needs

to be 3x3.

We want a matrix which gives us:

y

x

tyy

txx

Page 12: 2D Transformations - WordPress.com · 2/2/2017  · 7 February 2017 Week 5-2D Transformations 9 Combining transformations We have a general transformation of a point: P' = M • P

7 February 2017 Week 5-2D Transformations 12

Now what?

1100

10

01

1

y

x

t

t

y

x

y

x

11001

110

101

yx

tyxy

tyxx

y

x

Page 13: 2D Transformations - WordPress.com · 2/2/2017  · 7 February 2017 Week 5-2D Transformations 9 Combining transformations We have a general transformation of a point: P' = M • P

7 February 2017 Week 5-2D Transformations 13

And?

Rotations:

Scales:

1100

0cossin

0sincos

1

y

x

y

x

1100

00

00

1

y

x

s

s

y

x

y

x

Page 14: 2D Transformations - WordPress.com · 2/2/2017  · 7 February 2017 Week 5-2D Transformations 9 Combining transformations We have a general transformation of a point: P' = M • P

7 February 2017 Week 5-2D Transformations 14

What of it?

We can represent any of our three transformations as a single

matrix.

No special cases when transforming a point – matrix • vector.

Composite transformations – matrix • matrix.

Composite transformations:

Rotate about an arbitrary point – translate, rotate, translate

Scale about an arbitrary point – translate, scale, translate

Change coordinate systems – translate, rotate, scale

Does the order of operations matter?

Page 15: 2D Transformations - WordPress.com · 2/2/2017  · 7 February 2017 Week 5-2D Transformations 9 Combining transformations We have a general transformation of a point: P' = M • P

7 February 2017 Week 5-2D Transformations 15

Is matrix multiplication associative?

dhlcfldgjcejdhkcfkdgicei

bhlaflbgjaejbhkafkbgiaei

lk

ji

dhcfdgce

bhafbgae

lk

ji

hg

fe

dc

ba

dhldgjcflcejdhkdgicfkcei

bhlbgjaflaejbhkbgiafkaei

hlgjhkgi

flejfkei

dc

ba

lk

ji

hg

fe

dc

ba

Page 16: 2D Transformations - WordPress.com · 2/2/2017  · 7 February 2017 Week 5-2D Transformations 9 Combining transformations We have a general transformation of a point: P' = M • P

7 February 2017 Week 5-2D Transformations 16

Is matrix multiplication commutative?

dhcfdgce

bhafbgae

hg

fe

dc

ba

hdgbhcga

fdebfcea

dc

ba

hg

fe

Page 17: 2D Transformations - WordPress.com · 2/2/2017  · 7 February 2017 Week 5-2D Transformations 9 Combining transformations We have a general transformation of a point: P' = M • P

7 February 2017 Week 5-2D Transformations 17

Order of operations

So, it does matter. Let’s look at an example:

Page 18: 2D Transformations - WordPress.com · 2/2/2017  · 7 February 2017 Week 5-2D Transformations 9 Combining transformations We have a general transformation of a point: P' = M • P

7 February 2017 Week 5-2D Transformations 18

Useful compositions

Rotate about a pivot point:

T(pivot) • R() • T(–pivot) • P

Scale about a fixed point:

T(fixed) • S(scale) • T(–fixed) • P

General scaling directions:

R(–) • S(scale) • R() • P

Page 19: 2D Transformations - WordPress.com · 2/2/2017  · 7 February 2017 Week 5-2D Transformations 9 Combining transformations We have a general transformation of a point: P' = M • P

7 February 2017 Week 5-2D Transformations 19

Other transformations

Reflection:

x-axis y-axis

100

010

001

100

010

001

Page 20: 2D Transformations - WordPress.com · 2/2/2017  · 7 February 2017 Week 5-2D Transformations 9 Combining transformations We have a general transformation of a point: P' = M • P

7 February 2017 Week 5-2D Transformations 20

Other transformations

Reflection:

origin line x=y

100

010

001

100

001

010

Page 21: 2D Transformations - WordPress.com · 2/2/2017  · 7 February 2017 Week 5-2D Transformations 9 Combining transformations We have a general transformation of a point: P' = M • P

7 February 2017 Week 5-2D Transformations 21

Other transformations

Shear:

x-direction y-direction

100

010

01 xsh

100

01

001

ysh

Page 22: 2D Transformations - WordPress.com · 2/2/2017  · 7 February 2017 Week 5-2D Transformations 9 Combining transformations We have a general transformation of a point: P' = M • P

7 February 2017 Week 5-2D Transformations 22

Coordinate system transformations

We often need to transform points from one coordinate

system to another:

1. We might model an object in non-Cartesian space (polar)

2. Objects may be described in their own local system

3. Other reasons: textures, display, etc

Page 23: 2D Transformations - WordPress.com · 2/2/2017  · 7 February 2017 Week 5-2D Transformations 9 Combining transformations We have a general transformation of a point: P' = M • P

7 February 2017 Week 5-2D Transformations 23

Matrix multiplication

So we can do a lot with one basic operation.

We’d better make this operation as fast as possible.

Let’s start with the form of the matrix:

Why haven’t we used the bottom row of the matrix?

100

1 fed

cba

M

Page 24: 2D Transformations - WordPress.com · 2/2/2017  · 7 February 2017 Week 5-2D Transformations 9 Combining transformations We have a general transformation of a point: P' = M • P

7 February 2017 Week 5-2D Transformations 24

Matrix multiplication

Since we don’t use the bottom row of the 2D transformation

matrix, we could have a special transform composition

operation:

100

1 fed

cba

M

100

2 lkj

ihg

M

100100100

21 feldiekdhejdg

cblaibkahbjag

lkj

ihg

fed

cba

MM