Vous êtes sur la page 1sur 64

2D TRANSFORMATIONS

What
Given a 2D object, transformation is to change the
o je t s

Position (translation , reflection)


Size (scaling)
Orientation (rotation)
Shapes (shear)

How to implement
2 ways
Geometric Transformation
Alter the coordinates descriptions of an object
Coordinate system unchanged

Coordinate transformation
Produce a different coordinate system

2D Geometrical Transformations

Translate
Shear

Rotate

Scale

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:
X = X+ tx
Y= Y+ ty
or in matrix form: P' = P + T
x
y

x
y

tx
ty

P =

ty
P (x,y)

tx

, ?

Translation
How to translate an object with multiple
vertices?

Translate individual
vertices

Rotation
Repositioning the object along the circular path.
x = r cos (f)
y = r sin (f)

= r cos (f + q)
= r si f + q)
=

cos(q) y sin(q)

cos(q) + x sin(q)

x cos q
y sinq

sinq x

cos q y

P(x,y)
q

r
f

x
P' = R P

Rotation

How to rotate an object with multiple


vertices?

Rotate individual
Vertices

Scaling
Changing the size of an object by two scaling factors, Sx and
Sy for the x- and y- coordinates respectively.

X = sx * X

Y = sy * Y

x sx
y 0

0 x

sy y

Combining transformations
We have a general transformation of a point:
P' = M P + A
When we scale or rotate, M
We set
A
Additive identity.
When we translate,
M
Multiplicative identity
A
We set
To combine multiple transformations,
we must explicitly compute each transformed point.
But it d be nicer if we could use the same matrix operation all the
time.
So e d ha e to combine multiplication and addition into a single
operation.
26 August 2014

Week 5-2D Transformations

10

Homogenous Coordinates
y

y
w
x

L et's move our problem into 3 D.


Every Cartesian coordinate position (x,y) can be
represented by homogeneous coordinate triple (ax, ay, a)
a is any nonzero value
A point in 2D can be represented in many ways in the new
space.
(2, 4) ---------- ( 8, 16, 4) or (6, 12, 3 ) or (2, 4, 1) or etc.

Homogenous Coordinates
We can always map back to the original 2D point
by dividing by the last coordinate
(15 , 6, 3 )
( 5 , 2).
(60, 40, 10) ?.
Now we use 1 for the last coordinate
x
Point in column-vector:
y
1
Our point now has three coordinates.
So our transformation matrix needs to be 3 x 3

Matrix Representation
Translation :

Rotation :

Scaling :

x 1 0 t x x
y 0 1 t y
y

1 0 0 1 1
x cos q
y sin q

1 0
x s x
y 0

1 0

sin q
cos q
0
0

sy
0

0 x

0 y
1 1

0 x
0 y
1 1

Composite Transformation
We can represent any sequence of transformations
as a single matrix called as composite transformation
matrix .

Composite transformations:
Two successive Translation
Two successive Rotation
Two successive Scaling
Rotate about an arbitrary point
Scale about an arbitrary point
General composite transformation
(according to problem)

Two successive Translation is


additive

P' T (t x 2 , t y 2 ) *T (t x1 , t y1 ) * P

T21

1 0 t x 2 1 0 t x1
0 1 t 0 1 t
y2
y1

0 0 1 0 0 1

T (t x1 , t y1 )

1 0 t x1 + t x 2
0 1 t y1 + t y 2
0 0
1

Two successive Rotation


is additive
R21 R(q 2 ) R(q1 )

R21

T (t x 2 , t y 2 )

cos q 2 sin q 2 0 cos q1 sin q1 0


sin q 2 cos q 2 0 sin q1 cos q1 0
0
0
1 0
0
1

q2 q
1
cos(q 2 + q1 ) sin(q 2 + q1 ) 0
sin(q 2 + q1 ) cos(q 2 + q1 ) 0

0
0
1

Two successive Scaling is


multiplicative
P' S (sx 2 , s y 2 ) * S (sx1 , s y1 ) * P

S (sx 2 , s y 2 )
S ( s x1 , s y1 )

sx 2 0 0 sx1 0 0
S 21 0 s y 2 0 0 s y1 0
0 0 1 0 0 1

0
0
sx 2 * sx1
s y 2 * s y1 0
0
0
0
1

Order of operations
So, it does
1.
2.

Translate
Rotate

atter. Let s look at a e a ple:


1.
2.

Rotate
Translate

Rotation about an arbitrary point


To rotate about an arbitrary pivot point P(px,py) by q:
Translate the object so that P will coincide with the
origin: T(-px, -py)
Rotate the object: R(q)
Translate the object back so that pivot point is returned
to its original position : T(px,py)
(px,py)

Scale about an arbitrary point

To scale about an arbitrary pivot point P (px,py):


Translate the object so that P will coincide with
the origin: T(-px, -py)
Rotate the object: S(sx, sy)
Translate the object back so that pivot point is
returned to its original position: T(px,py)
(px,py)

Reflection
Produce mirror image of the object by rotating the
o je t a out a a is of refle tio
a a gle 8
Reflection axis on x-y
plane
X-Axis
Y- Axis
X=y line
Any line

Reflection axis
perpendicular to x-y
plane
Axis passing through origin
Axis passing through any
pivot point

Reflection
Axis on x-y Plane

1. x-axis

1 0 0
Re fx 0 1 0
0 0 1

2. y-axis

1 0 0

Re fy 0 1 0
0 0 1

Reflection
3. About line x=y

0 1 0

Re fx y 1 0 0
0 0 1

Reflection
Axis perpendicular to x-y Plane
1. At origin

2. At any
pivot point

(px,py)

1 0 Px 1 0 0 1 0 Px

1 0 0

Re fpivot 0 1 Py 0 1 0 0 1 Py

Re forigin 0 1 0
0 0 1 0 0 1 0 0 1
0 0 1

Shear
Distorts the shape such that the transformed shape appears
that the object composed of internal layers that cause to slide
over each other

x-direction

x 1 shx
y 0 1

1 0 0

y-direction

0 x
0 * y
1 1

x 1
y sh
y
1 0

0 0 x

1 0 * y
0 1 1

Coordinate Transformation
Translation
P X ,Y

Translation applied over the


coordinate system
We can write the
components:
X = X- tx
Y= Y- ty
or in matrix form: P' = P - T
x
y

x
y

tx
ty

P(X,Y)

Ty
X
Tx

Rotation
x = r cos (f)
y = r sin (f)
= r cos (f q)
= r si f q)
=

cos(q) + y sin(q)

cos(q) - x sin(q)

P X ,Y
P(X,Y)

Y
r

f-q

x cos q sin q x

y sin q cos q y

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

27

Viewport Transformation
Window-to-Viewport Mapping
Window

wy2

vy2

(wx, wy)
wy1
wx1

Viewport

(vx, vy)
vy1
vx1

wx2

Screen Coordinates

vx2
Image Coordinates

vx = vx1 + (wx wx1) * (vx2 vx1) / (wx2 wx1);


vy = vy1 + (wy wy1) * (vy2 vy1) / (wy2 wy1);
28

Clipping
Avoid Drawing Parts of Primitives Outside
Window
Points
Lines
Polygons
Circles
etc.

29

Point Clipping
Is Point(x,y) Inside the Clip Window?
wymax

Inside =
(x>=wxmin)
&&
(x<=wxmax)
&&
(y>=wymin)
&&
(y<=wymax);

(x, y)

wymin
wxmin

wxmax
30

Line Clipping
Find the Part of a Line Inside the Clip Window
P7
P1
P4

P8

P3

P2

P6

P5

P10

P9
Before Clipping
31

Line Clipping
Find the Part of a Line Inside the Clip Window
P7

P4
P3
P6
P5
After Clipping
32

P8

Cohen-Sutherland Line Clipping


Use Simple Tests to Classify Easy Cases First
P7
P1
P4

P8

P3

P2

P6

P10

P5

P9
33

Cohen-Sutherland Line Clipping


Classify Some Lines Quickly by AND of Bit Codes Representing
Regions of Two Endpoints (Must Be 0)
1001
P7 1000
1010
Bit 1
P1
P4
0001

0000

P3

P2

P8
0010

P6

0101
Bit 4

P5

P10

0100
34

P9

0110
Bit 3

Bit 2

Cohen-Sutherland Line Clipping


Classify Some Lines Quickly by AND of Bit Codes Representing
Regions of Two Endpoints (Must Be 0)
1001
P7 1000
1010
Bit 1
P1
P4
0001

0000

P3

P2

P8
0010

P6

0101
Bit 4

P5

P10

0100
35

P9

0110
Bit 3

Bit 2

Cohen-Sutherland Line Clipping


Classify Some Lines Quickly by AND of Bit Codes Representing
Regions of Two Endpoints (Must Be 0)
1001
P7 1000
1010
Bit 1
P4
0001

P8
0010

0000

P3
P6

0101
Bit 4

P5

P10

0100
36

P9

0110
Bit 3

Bit 2

Cohen-Sutherland Line Clipping


Compute Intersections with Window Boundary for Lines That
Ca t e Classified Qui kl
1001
P7 1000
1010
Bit 1
P4
0001

P8
0010

0000

P3
P6

0101
Bit 4

P5

P10

0100
37

P9

0110
Bit 3

Bit 2

Cohen-Sutherland Line Clipping


Compute Intersections with Window Boundary for Lines That
Ca t e Classified Qui kl
1001
P7 1000
1010
Bit 1
P4
0001

P8
0010

0000

P3
P6

0101
Bit 4

P5

P10

0100
38

P9

0110
Bit 3

Bit 2

Cohen-Sutherland Line Clipping


Compute Intersections with Window Boundary for Lines That
Ca t e Classified Qui kl
1001
P7 1000
1010
Bit 1
P4
0001

P8
0010

0000

P3
P6
P5

0101

P10

0100

Bit 4
39

P9

0110
Bit 3

Bit 2

Cohen-Sutherland Line Clipping


Compute Intersections with Window Boundary for Lines That
Ca t e Classified Qui kl
1001
P7 1000
1010
Bit 1
P4
0001

P8
0010

0000

P3
P6
P5

0101

P10

0100

Bit 4
40

P9

0110
Bit 3

Bit 2

Cohen-Sutherland Line Clipping


Compute Intersections with Window Boundary for Lines That
Ca t e Classified Qui kl
1001
P7 1000
1010
Bit 1
P4
0001

P8
0010

0000

P3
P6
P5

0101

P10

0100

Bit 4
41

P9

0110
Bit 3

Bit 2

Cohen-Sutherland Line Clipping


Compute Intersections with Window Boundary for Lines That
Ca t e Classified Qui kl
1001
P7 1000
1010
Bit 1
P4
0001

P8
0010

0000

P3
P6
P5

0101

P10

0100

Bit 4
42

P9

0110
Bit 3

Bit 2

Cohen-Sutherland Line Clipping


Compute Intersections with Window Boundary for Lines That
Ca t e Classified Qui kl 1000
1001
1010
P7
Bit 1
P4
0001

P8
0010

0000

P3
P6
P5

0101

P10

0100

Bit 4
43

P9

0110
Bit 3

Bit 2

Cohen-Sutherland Line Clipping


Compute Intersections with Window Boundary for Lines That
Ca t e Classified Qui kl 1000
1001
1010
17
P
Bit 1
P4
0001

P8
0010

0000

P3
P6
P5

0101

P10

0100

Bit 4
44

P9

0110
Bit 3

Bit 2

Cohen-Sutherland Line Clipping


Compute Intersections with Window Boundary for Lines That
Ca t e Classified Qui kl 1000
1001
1010
P7
Bit 1
P4
0001

0000

P3

P8
0010

P6
P5

0101

P10

0100

Bit 4
45

P9

0110
Bit 3

Bit 2

Cohen-Sutherland Line Clipping


Compute Intersections with Window Boundary for Lines That
Ca t e Classified Qui kl
1001
1000
1010
P7
Bit 1
P4
0001

0000

P3

P8
0010

P6
P5

0101

P10

0100

Bit 4
46

P9

0110
Bit 3

Bit 2

Cohen-Sutherland Line Clipping


Compute Intersections with Window Boundary for Lines That
Ca t e Classified Qui kl
1001
1000
1010
P7
Bit 1
P4
0001

0000

P3

P8
0010

P6
P5

0101

P10

0100

Bit 4
47

P9

0110
Bit 3

Bit 2

Cohen-Sutherland Line Clipping


Compute Intersections with Window Boundary for Lines That
Ca t e Classified Qui kl 1000
1001
1010
P7
Bit 1
P4
0001

0000

P3

P8
0010

P6
P5

0101

P10

0100

Bit 4
48

P9

0110
Bit 3

Bit 2

Cohen-Sutherland Line Clipping


Compute Intersections with Window Boundary for Lines That
Ca t e Classified Qui kl 1000
1001
1010
P7
Bit 1
P4
0001

0000

P3

P8
0010

P6
P5

0101

P10

0100

Bit 4

P90110
Bit 3

49

Bit 2

Cohen-Sutherland Line Clipping


Compute Intersections with Window Boundary for Lines That
Ca t e Classified Qui kl 1000
1001
1010
P7
Bit 1
P4
0001

0000

P3

P8
0010

P6
P5

0101

P10

0100

Bit 4

P90110
Bit 3

50

Bit 2

Cohen-Sutherland Line Clipping


Compute Intersections with Window Boundary for Lines That
Ca t e Classified Qui kl 1000
1001
1010
P7
Bit 1
P4
0001

0000

P3

P8
0010

P6
P5

0101

Bit 2
0100

Bit 4

0110
Bit 3

51

Sutherland-Hodgman
Polygon Clipping

Input each edge (vertex pair) successively.


Output is a new list of vertices.
Each edge goes through 4 clippers.
The rule for each edge for each clipper is:
If first input vertex is outside, and second is inside, output
the intersection and the second vertex
If first both input vertices are inside, then just output
second vertex
If first input vertex is inside, and second is outside, output
is the intersection
If both vertices are outside, output is nothing

Sutherland-Hodgman Polygon Clipping:


Four possible scenarios at each clipper
outside inside
v2

outside inside
v2

outside inside
v2

v1

Inside to inside:
Output: v2

v2
v1

v1

Outside to inside:
Output:
a d

outside inside

Inside to outside:
Output:

v1

Outside to outside:
Output: nothing

Sutherland-Hodgman Clipping
Edge from s to p takes one of four cases:
inside

outside

inside

outside

inside

outside
p

p
p output
54

inside
p

s
i output

no output

i output
p output

outside
s

Sutherland-Hodgman Clipping
Basic idea:
Consider each edge of the viewport individually
Clip the polygon against the edge equation
After doing all planes, the polygon is fully clipped

55

Sutherland-Hodgman Clipping
Basic idea:
Consider each edge of the viewport individually
Clip the polygon against the edge equation
After doing all planes, the polygon is fully clipped

56

Sutherland-Hodgman Clipping
Basic idea:
Consider each edge of the viewport individually
Clip the polygon against the edge equation
After doing all planes, the polygon is fully clipped

57

Sutherland-Hodgman Clipping
Basic idea:
Consider each edge of the viewport individually
Clip the polygon against the edge equation
After doing all planes, the polygon is fully clipped

58

Sutherland-Hodgman Clipping
Basic idea:
Consider each edge of the viewport individually
Clip the polygon against the edge equation
After doing all planes, the polygon is fully clipped

59

Sutherland-Hodgman Clipping
Basic idea:
Consider each edge of the viewport individually
Clip the polygon against the edge equation
After doing all planes, the polygon is fully clipped

60

Sutherland-Hodgman Clipping
Basic idea:
Consider each edge of the viewport individually
Clip the polygon against the edge equation
After doing all planes, the polygon is fully clipped

61

Sutherland-Hodgman Clipping
Basic idea:
Consider each edge of the viewport individually
Clip the polygon against the edge equation
After doing all planes, the polygon is fully clipped

62

Sutherland-Hodgman Clipping
Basic idea:
Consider each edge of the viewport individually
Clip the polygon against the edge equation
After doing all planes, the polygon is fully clipped

63

Sutherland-Hodgman Clipping
Basic idea:
Consider each edge of the viewport individually
Clip the polygon against the edge equation
After doing all planes, the polygon is fully clipped

64

Vous aimerez peut-être aussi