Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 376 Introduction to Computer Graphics 02 / 09 / 2007 Instructor: Michael Eckmann.

Similar presentations


Presentation on theme: "CS 376 Introduction to Computer Graphics 02 / 09 / 2007 Instructor: Michael Eckmann."— Presentation transcript:

1 CS 376 Introduction to Computer Graphics 02 / 09 / 2007 Instructor: Michael Eckmann

2 Michael Eckmann - Skidmore College - CS 376 - Spring 2007 Today’s Topics Questions? 2D Transformations –Translation –Scaling –Rotation Homogeneous coordinates Matrices representing the transforms of the homogeneous points Fixed point scaling Fixed point rotation Shearing and reflection

3 Michael Eckmann - Skidmore College - CS 376 - Spring 2007 Geometric Transformations (2D) Reading: 5.1- 5.9 (2D geometric transformations) Translations Rotations Scaling Homogeneous Coordinates Shearing Reflections

4 Michael Eckmann - Skidmore College - CS 376 - Spring 2007 Matrices An m by n matrix is a two dimensional array of values with m rows and n columns. Points are typically represented by column matrices (meaning they have multiple rows, but only 1 column) Matrix multiplication (3x3 times a 3x3 yields a 3x3 matrix) ( a b c ) ( j k l ) ( aj+bm+cp ak+bn+cq al+bo+cr ) ( d e f ) ( m n o ) = ( dj+em+fp dk+en+fq dl+eo+fr ) ( g h i ) ( p q r ) ( gj+hm+ip gk+hn+iq gl+ho+ir ) Matrix multiplication (2x2 times a 2x1 yields a 2x1 column matrix) ( a b ) ( x ) = ( ax+by ) ( c d ) ( y ) ( cx+dy ) A 2x2 matrix times a 2x1 matrix (a point) yields another point. Matrix multiplication is associative. ABC = (AB)C = A(BC).

5 Michael Eckmann - Skidmore College - CS 376 - Spring 2007 Translations (2D) Recall the idea that for the circle algorithm that we did computations assuming the circle's center was the origin. Then when we plotted the points we offset the calculated point by the actual center by just adding the respective coordinates. What we did there was perform a translation. Translation is a transformation on an object that simply moves it to a different position somewhere else within the same coordinate system. To translate an object we translate each of its vertices (points). To translate a 2d point (x 1, y 1 ) by t x in the x direction and t y in the y direction, we simply calculate the new coordinates to be: (x 2, y 2 ) = (x 1 + t x, y 1 + t y )

6 Michael Eckmann - Skidmore College - CS 376 - Spring 2007 Scaling (2D) Scaling is a transformation on an object that changes its size. Just as the translation could have been different amounts in x and y, you can scale x and y by different factors. Scaling is a transformation on an object that changes its size within the same coordinate system. To scale an object we scale each of its vertices (points). To scale a 2d point (x 1, y 1 ) by s x in the x direction and s y in the y direction, we simply calculate the new coordinates to be: (x 2, y 2 ) = (s x x 1, s y y 1 )

7 Michael Eckmann - Skidmore College - CS 376 - Spring 2007 Rotation (2D) Rotation is a transformation on an object that changes its position in a specific way (by rotating the object some angle about an axis). Rotations in the x-y plane are about an axis parallel to z. The point of intersection of the rotation axis with the x-y plane is the pivot point. We need to specify the angle and pivot point about which the object is to be rotated. To rotate an object we rotate each of its vertices (points). Positive angles are in the counterclockwise direction.

8 Michael Eckmann - Skidmore College - CS 376 - Spring 2007 Rotation (2D) To rotate a 2d point (x 1, y 1 ) an arbitrary angle of B, about the origin as a pivot point do the following. From the diagram on the board (also see figure 5-4 in the text) we have: sin(A + B) = y 2 / r => y 2 = r sin(A + B) cos(A + B) = x 2 / r x 2 = r cos(A + B) sin(A) = y 1 / r y 1 = r sin(A) cos(A) = x 1 / r x 1 = r cos(A) Known equalities exist for sin(A+B) and cos(A+B) sin(A + B) = sin(A) cos(B) + cos(A) sin(B) cos(A + B) = cos(A) cos(B) – sin(A) sin(B)

9 Michael Eckmann - Skidmore College - CS 376 - Spring 2007 Rotation (2D) Solve for x 2 and y 2. x 2 = r cos(A + B) = r cos(A) cos(B) – r sin(A) sin(B) = x 1 cos(B) – y 1 sin(B) y 2 = r sin(A + B) = r sin(A) cos(B) + r cos(A) sin(B) = y 1 cos(B) + x 1 sin(B) So, (x 2, y 2 ) = (x 1 cos(B) – y 1 sin(B), y 1 cos(B) + x 1 sin(B) ) This will rotate a point (x 1, y 1 ) an angle of B about the pivot point of the origin.

10 Michael Eckmann - Skidmore College - CS 376 - Spring 2007 Transformation Matrices (2D) TRANSLATION: (x 2, y 2 ) = (x 1 + t x, y 1 + t y ) ( x 2 ) = ( x 1 ) + ( t x ) ( y 2 ) ( y 1 ) ( t y ) SCALING: (x 2, y 2 ) = (s x x 1, s y y 1 ) ( x 2 ) = ( s x 0 ) ( x 1 ) ( y 2 ) ( 0 s y ) ( y 1 ) ROTATION: (x 2, y 2 ) = (x 1 cos(B) – y 1 sin(B), y 1 cos(B) + x 1 sin(B) ) ( x 2 ) = ( cos(B) – sin(B) ) ( x 1 ) ( y 2 ) ( sin(B) cos(B) ) ( y 1 )

11 Michael Eckmann - Skidmore College - CS 376 - Spring 2007 Homogeneous Coordinates Translations, Scales and Rotations can be done as described in the previous slides. What was not described was rotation about an arbitrary pivot point. In addition to the matrix multiplication involved, a vector (column matrix) add would need to be added in. So, both translations and rotations have an add of a column matrix. In addition, rotations and scales have matrix multiplications as well. To be able to represent all transformations only with matrix multiplications (without additions of column matrices), we use homogeneous coordinates.

12 Michael Eckmann - Skidmore College - CS 376 - Spring 2007 Homogeneous Coordinates To represent a 2d point in homogeneous coordinates instead of just an x and a y to represent a 2d point, we use an x, a y and an additional value, the homogeneous parameter. (x,y) => (cx, cy, c) where c is the homogeneous parameter. There are an infinite number of equivalent homogeneous representations for each coordinate point (x,y). When c = 1, we have (x,y) => (x, y, 1) Using homogeneous coordinates allows us to represent 2d transformations as matrix multiplications exclusively.

13 Michael Eckmann - Skidmore College - CS 376 - Spring 2007 Homogeneous Coordinates Transformations on homogeneous coordinates TRANSLATION: (x 2, y 2 ) = (x 1 + t x, y 1 + t y ) ( x 2 ) = ( 1 0 t x ) ( x 1 ) ( y 2 ) ( 0 1 t y ) ( y 1 ) ( 1 ) ( 0 0 1 ) ( 1 ) SCALING: (x 2, y 2 ) = (s x x 1, s y y 1 ) ( x 2 ) = ( s x 0 0 ) ( x 1 ) ( y 2 ) ( 0 s y 0 ) ( y 1 ) ( 1 ) ( 0 0 1 ) ( 1 )

14 Michael Eckmann - Skidmore College - CS 376 - Spring 2007 Homogeneous Coordinates Transformations on homogeneous coordinates ROTATION: (x 2, y 2 ) = (x 1 cos(B) – y 1 sin(B), y 1 cos(B) + x 1 sin(B) ) ( x 2 ) = (cos(B) – sin(B) 0 ) ( x 1 ) ( y 2 ) (sin(B) cos(B) 0 ) ( y 1 ) ( 1 ) ( 0 0 1 ) ( 1 ) These three transform matrices are sometimes written as –T(t x,t y ) –S(s x,s y ) –R(B)

15 Michael Eckmann - Skidmore College - CS 376 - Spring 2007 Scaling a line segment Let's look at this example on the board. Example: Scale a line segment from (3,2) to (5,2) by 2 in the x-direction. ( x 0 ) ( 2 0 0 ) ( 3 ) ( 6 ) ( y 0 ) = ( 0 1 0 ) ( 2 ) = ( 2 ) ( 1 ) ( 0 0 1 ) ( 1 ) ( 1 ) And the other endpoint: ( x end ) ( 2 0 0 ) ( 5 ) ( 10 ) ( y end ) = ( 0 1 0 ) ( 2 ) = ( 2 ) ( 1 ) ( 0 0 1 ) ( 1 ) ( 1 ) Problem: The line segment (3,2) to (5,2) when scaled by 2 in x-direction gives a line segment from (6,2) to (10, 2). Yes it's original length in the x-direction was 2 and now it is 4, but the line also was translated. Let's see it on the board.

16 Michael Eckmann - Skidmore College - CS 376 - Spring 2007 Fixed point scaling Specify a point that will remain fixed after the scaling. This is the fixed-point. Let's scale the same line segment but this time fix the left endpoint (3,2). To do this, we need to: –a) first translate (3, 2) to the origin –b) scale by 2 in x-direction (like before) –c) then translate the origin back to (3, 2) This will yield a matrix multiplication M c M b M a = M fps which we will then multiply M fps P, where P is a homogeneous point, to get the transformed point. Notice the order: matrix M a is closest to the point, then M b then M c

17 Michael Eckmann - Skidmore College - CS 376 - Spring 2007 Fixed point scaling a) the matrix to translate (3, 2) to the origin is this: (1 0 -3) (0 1 -2) (0 0 1) b) the matrix to scale by 2 in x direction is this: (2 0 0) (0 1 0) (0 0 1) c) the matrix to translate the origin back to (3, 2) is this: (1 0 3) (0 1 2) (0 0 1)

18 Michael Eckmann - Skidmore College - CS 376 - Spring 2007 Fixed point scaling a) first translate (3, 2) to the origin b) scale by 2 in x-direction (like before) c) then translate the origin back to (3, 2) (1 0 3) (2 0 0) (1 0 -3) ( 2 0 3) ( 1 0 -3 ) (2 0 -3 ) (0 1 2) (0 1 0) (0 1 -2) = ( 0 1 2) ( 0 1 -2) = (0 1 0 ) (0 0 1) (0 0 1) (0 0 1) ( 0 0 1) ( 0 0 1 ) (0 0 1 ) Multiply this matrix by each of the vertices (endpoints of the line segment): (2 0 -3 ) ( 3 ) ( 3 ) (0 1 0 ) ( 2 ) = ( 2 ) (0 0 1 ) ( 1 ) ( 1 ) (2 0 -3 ) ( 5 ) ( 7 ) (0 1 0 ) ( 2 ) = ( 2 ) (0 0 1 ) ( 1 ) ( 1 ) Notice: initial endpoint (the fixed-point) stayed the same after transformation and the new width in x-direction is 4 (which is 2 the scale factor * 2 the original length).

19 Michael Eckmann - Skidmore College - CS 376 - Spring 2007 Fixed point scaling Let's scale the same line segment but this time fix the midpoint (4,2). To do this, we need to: –a) first translate (4, 2) to the origin –b) scale by 2 in x-direction (like before) –c) then translate the origin back to (4, 2)

20 Michael Eckmann - Skidmore College - CS 376 - Spring 2007 Fixed point scaling a) first translate (4, 2) to the origin b) scale by 2 in x-direction (like before) c) then translate the origin back to (4, 2) (1 0 4) (2 0 0) (1 0 -4) ( 2 0 4) ( 1 0 -4 ) (2 0 -4 ) (0 1 2) (0 1 0) (0 1 -2) = ( 0 1 2) ( 0 1 -2) = (0 1 0 ) (0 0 1) (0 0 1) (0 0 1) ( 0 0 1) ( 0 0 1 ) (0 0 1 ) Multiply this matrix by each of the vertices (endpoints of the line segment): (2 0 -4 ) ( 3 ) ( 2 ) (0 1 0 ) ( 2 ) = ( 2 ) (0 0 1 ) ( 1 ) ( 1 ) (2 0 -4 ) ( 5 ) ( 6 ) (0 1 0 ) ( 2 ) = ( 2 ) (0 0 1 ) ( 1 ) ( 1 ) Notice: the line segment stretched equally left and right from the middle. The new width in x-direction is 4 (which is 2 the scale factor * 2 the original length).

21 Michael Eckmann - Skidmore College - CS 376 - Spring 2007 Fixed point rotation To rotate an object some angle B about a fixed point (x p, y p ) we do: –a) first translate (x p, y p ) to the origin –b) rotate by angle B –c) then translate the origin back to (x p, y p ) This will yield a matrix multiplication M c M b M a = M fpr which we will then multiply M fpr P, where P is a homogeneous point, to get the transformed point. (1 0 x p ) (cos(B) – sin(B) 0 ) (1 0 -x p ) ( cos(B) -sin(B) x p ) ( 1 0 -x p ) (0 1 y p ) (sin(B) cos(B) 0 ) (0 1 -y p ) = ( sin(B) cos(B) y p ) ( 0 1 -y p ) (0 0 1 ) (0 0 1) (0 0 1 ) ( 0 0 1 ) ( 0 0 1 ) ( cos(B) -sin(B) -x p cos(B) + y p sin(B) + x p ) = ( sin(B) cos(B) -x p sin(B) – y p cos(B) + y p ) ( 0 0 1 )

22 Michael Eckmann - Skidmore College - CS 376 - Spring 2007 Other transformations Shearing Y shear: (1 0 0) (sh y 1 0) (0 0 1) X shear: (1 sh x 0) (0 1 0) (0 0 1) Examples of what these do on the board.

23 Michael Eckmann - Skidmore College - CS 376 - Spring 2007 Other transformations Reflections About the y-axis: (-1 0 0) (0 1 0) (0 0 1) About the x-axis: (1 0 0) (0 -1 0) (0 0 1) About the z-axis: (-1 0 0) (0 -1 0) (0 0 1) Examples of what these do on the board. About the line y=x: (0 1 0) (1 0 0) (0 0 1) About the line y=-x: (0 -1 0) (-1 0 0) (0 0 1)


Download ppt "CS 376 Introduction to Computer Graphics 02 / 09 / 2007 Instructor: Michael Eckmann."

Similar presentations


Ads by Google