46
2D Transformations 2D Transformations

2D Transformations. Translation x’ = x + tx y’ = y + ty translation vector: T = P = P’ = P’ = P + T x y (x,y) (x’,y’) tx ty xyxy x’ y’

Embed Size (px)

Citation preview

2D Transformations2D Transformations

Translation x’ = x + tx

y’ = y + ty

translation vector: T =

P = P’ =

P’ = P + T

x

y

(x,y)

(x’,y’)txty

xy

x’y’

Rotation

Rotation about the originx’ = r.cos(+)

y’ = r.sin(+)

Rotation matrix:

R =

P’ = R . P

x

y

(x,y)

(x’,y’)

cos -sin sin cos

r

Rotation

Rotation about an arbitrary point (xr, yr)

x’ = xr + (x-xr).cos - (y-yr).sin y’ = yr + (x-xr).sin - (y-yr).cos

x

y

(x,y)

(x’,y’)

(xr,yr)

Scaling

x’ = x . sx

y’ = y . sy

Scaling matrix : S =

P’ = S . P

x

y

sx 0 0 sy

Scaling

Fixed point scaling

x’ = x . sx + xf (1-sx)

y’ = y . sy + yf (1-sy)

x

y (xf,yf)

Homogeneous Coordinates

(x, y) (x, y, h)

h = 1

= 1/h

(x, y, h) = (x/h, y/h, 1)

(x, y, h)(0,0,1)

(x, y, h)

x

h

y

Homogeneous Coordinates

(x,y) (xh, yh, h)

xh= h.x

yh= h.y

(xh, yh, h) = (h.x, h.y, h)

Translation

P = P’ =

translation matrix: T(tx, ty) =

P’ = T(tx, ty) . P

1 0 tx0 1 ty0 0 1

xy1

x’y’1

Rotation

Rotation matrix: R =

P’ = R() . P

cos -sin 0sin cos 00 0 1

Scaling

Scaling matrix : S =

P’ = S(sx, sy) . P

sx 0 0 0 sy 00 0 1

Inverse Transformations

T-1 =

R-1 =

S-1 =

0 0 -tx 0 0 -ty0 0 1

1/sx 0 0 0 1/sy 00 0 1

cos sin 0-sin cos 00 0 1

Composite Transformations

P’ = M3 . M2 . M1 . P= M . P

M: composite matrix

T(t2x, t2y) . T(t1x, t1y) = T( t1x+t2x, t1y+t2y )

R(2) . R(1) = R( 1+2 )

S(s2x, s2y) . S(s1x, s1y) = S( s1x.s2x, s1y.s2y )

Pivot-point Rotation

x

y(xr, yr)

Pivot-point Rotation

x

y

1. Translate the object so that pivot-point is moved to the originT(-xr, -yr) . P

Pivot-point Rotation

x

y

1. Translate the object so that pivot-point is moved to the originT(-xr, -yr) . P

2. Rotate object about the originR() . T(-xr, -yr) . P

Pivot-point Rotation

1. Translate the object so that pivot-point is moved to the originT(-xr, -yr) . P

2. Rotate object about the originR() . T(-xr, -yr) . P

3. Translate the object so that pivot-point is returned to its original positionT(xr, yr) . R() . T(-xr, -yr) . P

= R(xr, yr, )

x

y

Fixed-point Scaling

x

y(xf, yf)

Fixed-point Scaling

x

y

1. Translate the object so that fixed-point is moved to the originT(-xf, -yf) . P

Fixed-point Scaling

x

y

1. Translate the object so that fixed-point is moved to the originT(-xf, -yf) . P

2. Scale the object with respect to the originS(sx, sy) . T(-xf, -yf) . P

Fixed-point Scaling

x

y

1. Translate the object so that fixed-point is moved to the originT(-xf, -yf) . P

2. Scale the object with respect to the originS(sx, sy) . T(-xf, -yf) . P

3. Use inverse translation in step (1)

T(xf, yf) . S(sx, sy) . T(-xf, -yf) . P = S(xf, yf, sx, sy)

Scaling Directions

x

y

1. Rotate2. Scale 3. Rotate back

R-1() . S(s1, s2) . R()

s2

s1

Rigid-Body Transformation

Reflection

x

y

1 0 0 0 -1 00 0 1

Reflection

x

y

-1 0 0 0 1 00 0 1

Reflection

x

y

-1 0 0 0 -1 00 0 1

Reflection

x

y

0 1 0 1 0 00 0 1

Shear

x

y

1 shx 0 0 1 00 0 1

x

y

Raster Methods

Translation

Block transfer of the refresh buffer area

Raster Methods

Rotation

900 counterclockwise: 1. reverse pixel values in each row2. interchange rows and columns

1800: 1. reverse the order of elements in each row2. reverse the order of rows

Raster Methods

Rotation

Other cases:

1. calculate the amount of overlap with the rotated pixel areas

2. compute the color by averaging the colors of the overlapped source pixels, weighted by their percentage of area overlap.

Raster Methods

Scaling

1. scale pixel areas in the original block using sx, sy

2. assign color of the destination pixel according to the overlap area.

Raster Methods

Reftection

1. reverse row and column values2. translate

Shear

Shift the position of array values along rows or columns

Transformations between 2D Coordinate Systems

x

y

y’

x’

y0

x0

x

y

y’

x’

M = R(-) . T(-x0, -y0)

Transformations between 2D Coordinate Systems

x

yy’

x’

v

V

v = (vx, vy)

u = (vy, -vx)

R = u ux uy 0

vx vy 00 0 1

OpenGL

glTranslate* (tx, ty, tz)• f (float)• d (double)

glRotate* (theta, vx, vy, vz)• theta: rotation angle in degrees• (vx, vy, vz) vector defines the orientation of the

rotation axis that passes through the coordinate origin

glScale* (sx, sy, sz)

OpenGL

glMatrixMode(GL_MODELVIEW);

glLoadIdentity ( );

glMultMatrixf(M2);

glMultMatrixf(M1);

/* M = M2 . M1 */

OpenGL glMatrixMode(GL_MODELVIEW)• sets up the matrix for transformations (4x4 modelview

matrix)

glLoadIdentity ( )• assigns identity matrix to the current matrix

glLoadMatrix*(16-element array)• assigns a 16-element array (in column major order) to the

current matrix

glMultMatrix*(16-element array)• postmultiplies a 16-element array (M’) with the current

matrix (M) : M ← M.M’

OpenGL

Matrix Stack

Top matrix on the stack is the “current matrix” Initially stack contains identity matrix Maximum stack depth is 32 glGetIntegerv (GL_MAX_MODELVIEW_STACK_DEPTH, stackSize)• returns the number of positions available in the modelview stack

glGetIntegerv (GL_MODELVIEW_STACK_DEPTH, numMats)• returns the number of matrices currently in the stack

glPushMatrix()• copies the current matrix at the top of the stack

glPopMatrix()• destroys the matrix at the top of the stack

OpenGL glMatrixMode(GL_MODELVIEW);

glColor3f(0.0, 0.0, 1.0);Recti(50, 100, 200, 150);

glColor3f(1.0, 0.0, 0.0);glTranslatef(125.0, 125.0, 0.0);glRotatef(90.0, 0.0, 0.0, 1.0);glTranslatef(-125.0, -125.0, 0.0);Recti(50, 100, 200, 150);

/* T(125,125).R(90).T(-125,-125).P */

OpenGL glMatrixMode(GL_MODELVIEW);

glColor3f(0.0, 0.0, 1.0);Recti(50, 100, 200, 150);

glColor3f(1.0, 0.0, 0.0);glTranslatef(125.0, 125.0, 0.0);glRotatef(90.0, 0.0, 0.0, 1.0);glTranslatef(-125.0, -125.0, 0.0);Recti(50, 100, 200, 150);

/* T(125,125).R(90).T(-125,-125).P */

OpenGL glMatrixMode(GL_MODELVIEW);

glColor3f(0.0, 0.0, 1.0);Recti(50, 100, 200, 150);

glColor3f(1.0, 0.0, 0.0);glTranslatef(125.0, 125.0, 0.0);glRotatef(90.0, 0.0, 0.0, 1.0);glTranslatef(-125.0, -125.0, 0.0);Recti(50, 100, 200, 150);

/* T(125,125).R(90).T(-125,-125).P */

OpenGL glMatrixMode(GL_MODELVIEW);

glColor3f(0.0, 0.0, 1.0);Recti(50, 100, 200, 150);

glColor3f(1.0, 0.0, 0.0);glTranslatef(-200.0, -50.0, 0.0);Recti(50, 100, 200, 150);

OpenGL glMatrixMode(GL_MODELVIEW);

glColor3f(0.0, 0.0, 1.0);Recti(50, 100, 200, 150);

glColor3f(1.0, 0.0, 0.0);glTranslatef(-200.0, -50.0, 0.0);Recti(50, 100, 200, 150);

glLoadIdentity ( ); glRotatef(90.0, 0.0, 0.0, 1.0);Recti(50, 100, 200, 150);

OpenGL glMatrixMode(GL_MODELVIEW);

glColor3f(0.0, 0.0, 1.0);Recti(50, 100, 200, 150);

glColor3f(1.0, 0.0, 0.0);glTranslatef(-200.0, -50.0, 0.0);Recti(50, 100, 200, 150);

glLoadIdentity ( ); glRotatef(90.0, 0.0, 0.0, 1.0);Recti(50, 100, 200, 150);

glLoadIdentity ( );glScalef(-0.5, 1.0, 1.0);Recti(50, 100, 200, 150);

OpenGL glMatrixMode(GL_MODELVIEW);

glColor3f(0.0, 0.0, 1.0);Recti(50, 100, 200, 150);

glPushMatrix();glColor3f(1.0, 0.0, 0.0);glTranslatef(-200.0, -50.0, 0.0);Recti(50, 100, 200, 150);

glPopMatrix();glPushMatrix();

glRotatef(90.0, 0.0, 0.0, 1.0);Recti(50, 100, 200, 150);

glPopMatrix();glScalef(-0.5, 1.0, 1.0);Recti(50, 100, 200, 150);