computergraphics

27
Computer Graphics Spring 2007, #5 2D Transformations

Upload: rajyalakshmi-jammalamadaka

Post on 01-Feb-2016

213 views

Category:

Documents


0 download

DESCRIPTION

cg material

TRANSCRIPT

Page 1: ComputerGraphics

Computer Graphics

Spring 2007, #52D Transformations

Page 2: ComputerGraphics

Contents

• Coordinate systems• 2D transformations

– Translation– Rotation

– Scaling

• Homogeneous coordinates• Composite transformations

Page 3: ComputerGraphics

Coordinate Systems

• Computer graphics uses several coordinate systems and we need to move from one coordinate system to another

• There is one fixed ”world” coordinate system where everything is defined

• A viewer has ”view” coordinates

xworld

yworld

y0

x0

x view

y view

Page 4: ComputerGraphics

2D Translation• Assume objects are constrcuted from

points (x,y)• An point is translated in the 2D plane if the

coordinates change according tox’ = x + tx , y’ = y + ty

• T = (tx, ty) is the translation vector

T(x,y)

T

(x,y)-T

(x’,y’)

(x’,y’)

x

Y

Page 5: ComputerGraphics

2D Translation

• Translations are rigid body motions• Linear equations stay linear, hence

straight lines remain straight:

a∗x + b∗y +c = 0 −> a’∗x’ + b’∗y’ + c’ = 0• Repositon objects according to T and

redraw the object (points, equations etc. have changed)

Page 6: ComputerGraphics

2D Rotation• A rotation R repositions points in the plane

by moving them along a circular arc centered at the rotation point (xr,yr) by a given positive rotation angle θ:

θ

(x,y)

(x’,y’)

xr

yr

X

Y

Page 7: ComputerGraphics

2D Rotation, Rotation point = Origin

• Using polar coodinates we easily obtainx’ = R∗cos(φ + θ)

= R∗cos(φ)∗cos(θ) – R∗sin(φ)∗sin(θ)y’ = R∗sin(φ + θ)

= R∗cos(φ)∗sin(θ) + R∗sin(φ)∗cos(θ)• But x = R∗cos(φ) and y = R∗sin(φ), hence

x’ = x∗cos(θ) – y∗sin(θ)y’ = x∗sin(θ) + y∗cos(θ)

(x,y)

(x’,y’)

φθ

x’

y’=

x

y

cos(θ)

cos(θ)

-sin(θ)

sin(θ)

Rsin(θ)

Rcos(θ)

Page 8: ComputerGraphics

2D Rotation, Rotation Point (xr,yr)

• For a rotation with rotation point (xr,yr)

x’ = xr + (x-xr)∗cos(θ) – (y-yr)∗sin(θ)y’ = yr + (x-xr)∗sin(θ) + (y-yr)∗cos(θ)

(x,y)

(x’,y’)

φθ

(xr,yr)

Page 9: ComputerGraphics

2D Scaling

• Scaling is done with respect to a fixed point using the scaling parameters (sx,sy). For the origin as the fixed point

x’ = sx∗x, y’ = sy∗y• Scaling changes the size and sometimes the

position(!) of objects. Let (sx,sy) = (0.5,0.5):4

2

10.5

1.5 3

x’

y’=

x

y

sx

sy

0

0

Page 10: ComputerGraphics

2D Scaling

• For an arbitrary pixed point (xfp, yfp)

x’ = xfp + (x-xfp)∗sx

y’ = yfp + (y-yfp)∗sy

• Special cases:– Uniform scaling: sx = sy

– Mirroring w.r.t. y-axis: sx = -1, sy = 1– Mirroring w.r.t x-axis: sx = 1, sy = -1

– Reflection w.r.t. the origin: sx = -1, sy = -1

Page 11: ComputerGraphics

Mirroring wrt. Axes and a Point

Y- axis X-axis Origin

x

y

x

y

x

y

Page 12: ComputerGraphics

Transformation matrices

• For the origin as fixed point, both rotations R(q) and scalings S(sx,sy) can be written as transformation matrices acting on points, but how about translations T(tx,ty)?

• It would be nice to formulate T as a matrix operation since then any combination of rotations, translations and scaling could then be calculated very easily by associative matrix multiplication (very useful in 3D!)

• Solution: homogeneous coordinates!

Page 13: ComputerGraphics

Homogeneous Coordinates

• Idea: represent any point (x,y) in 2D by three coodinates (x’,y’,w) such that

x’ = x∗w y’ = y∗w• w can be chosen in almost any way: w ≠ 0• For any point (x,y) in 2D, form (x,y,1), do

necessary matrix calculations with (x,y,1) and in the end from (x’,y’,w’) extract x = x’/w’, y = y’/w’

• Next we write matrices for T, R and S using homogeneous coordinates.

Page 14: ComputerGraphics

Homogenoeus coordinates: Translation

• Translation by t = (tx,ty): X’ = T ∗ X

x’ 1 0 tx xy’ 0 1 ty y1 0 0 1 1

• Check if T(t2)T(t1) = T(t1+t2) !

=

Page 15: ComputerGraphics

Homogenoeus coordinates: Rotation

• Rotation R by θ, origin fixed :

x’ cos(θ) -sin(θ) 0 xy’ sin(θ) cos(θ) 0 y1 0 0 1 1

• Check if R(θ2)R(θ1) = R(θ1+θ2)

=

Page 16: ComputerGraphics

Homogenoeus coordinates: Scaling

• Scaling by s = (sx,sy): X’ = S ∗ X

x’ sx 0 0 xy’ 0 sy 0 y1 0 0 1 1

=

Page 17: ComputerGraphics

General Rotation Point Rotation

• A rotation with respect to an arbitrary point r = (xr,yr) in the 2D plane is equivalent to a sequence of three transformations:

1. a translation of (xr,yr) to the origin: T(-r)2. a simple rotation at the origin: R(θ)3. a translation back to (xr,yr): T(r)

(x,y)

(x’,y’)

φθ

(xr,yr)

(x,y)

(x’,y’)

θ

(xr,yr)

Page 18: ComputerGraphics

General Rotation Point Rotation

• Using matrix transformations

R(r,θ) = T(r) ∗ R(θ) ∗ T(-r)• Similar arguments apply to scalings with

general fixed points fp = (xfp, yfp) :

S(fp,s) = T(fp) ∗ S(s) ∗ T(-fp)

Page 19: ComputerGraphics

Shearing• Well known phenomenon in mechanical

engineering and fluid dynamics• Internal layers are shifted w.r.t. each other

• Mathematically: for fixed y all points are shifted by the same amount in the x-direction:

x’ = x + Hx ∗ y• Reference line: y = 0 (points which stay fixed

under the transformation).

y y

x x

Page 20: ComputerGraphics

Shearing

• Shear matrix in homogeneous coordinatesx’ 1 Hx 0 xy’ 0 1 0 y1 0 0 1 1

• A shear in the y-direction relative to the axis x=0 is given by x’ = x, y’ = y + Hy∗x

=

Page 21: ComputerGraphics

Shearing

• We can also perform a shearing with respect to a reference line, eg. yref = -1 and Hx = 0.5

yref = -1

x

y

(0,1)

(0,0)

(1,1)

(1,0)

yref = -1

x

y

(1,1)

(1/2,0)

(2,1)

(3/2,0)

Page 22: ComputerGraphics

Shearing

• Shearing w.r.t. the line y = yref :

– translate the points on the reference axis y = yref to y = 0

– do a shearing transformation w.r.t y = 0

– translate back to original yref.

T(0,yref)∗H(Hx,0)∗ T(0,yref)-1

Page 23: ComputerGraphics

Computational Aspects

• Combining transformations reduces to matrix multiplication, e.g.

R(r,θ) = T(r) ∗ R(θ) ∗ T(-r)• In general: multiplication of a 3x3 with another

3x3 matrix requires 3∗3∗3 = 27 multiplications and 2∗3∗3 additions.

• In 2D transformations, the third row of the matrices is always [0 0 1] and should never be calculated. In addition, in homogeneous coordinates the third component of the vectors is always one: (x,y,1). Hence never multiply with 1.

Page 24: ComputerGraphics

Computational Aspects

• Final result:– Matrix multiplication:

• 12 multiplications• 8 additions

– Matrix applied to vector:• 4 multiplications• 4 additions

Page 25: ComputerGraphics

Computational Aspects

• Inverse transformations are simple:T-1(t) = T(-t)

R-1(θ) = R(-θ)S-1(sx,sy) = S(1/sx,1/sy)

• Inverse of combined transformations:

(R∗S∗T)-1 = T-1 ∗ S-1 ∗ R-1

Page 26: ComputerGraphics

Coordinate Systems

• Transformation of coordinate systems:– Shift world origin by t and rotate by θ

• Transformation of points– rotate coordinate axes by θ and translate by t:

T(t) ∗ R(θ)– for points Xworld (x,y,1) do the inverse transformation:

Xview = R(-θ) * T(-t) Xworld

xworld

yworld

x viewy vie

w

ty

tx

θ

t

yview

xview

Page 27: ComputerGraphics

Coordinate Systems

• Often the viewing coordinate system is defined from the position of its origin in the world coordinate system and the direction of yview-axis: the view up vector V

• Note: V is the second column in R(θ) and the unit vector u for the x-axis in the first:

x’ cos(θ) -sin(θ) 0 xy’ sin(θ) cos(θ) 0 y1 0 0 1 1

=