MAT 343 Laboratory 4 Plotting and computer animation in MATLAB

Similar documents
Math 7 Elementary Linear Algebra PLOTS and ROTATIONS

To graph the point (r, θ), simply go out r units along the initial ray, then rotate through the angle θ. The point (1, 5π 6

To graph the point (r, θ), simply go out r units along the initial ray, then rotate through the angle θ. The point (1, 5π 6. ) is graphed below:

Answers to practice questions for Midterm 1

On Your Own. ). Another way is to multiply the. ), and the image. Applications. Unit 3 _ _

COMP30019 Graphics and Interaction Transformation geometry and homogeneous coordinates

AMS 27L LAB #2 Winter 2009

Jim Lambers MAT 169 Fall Semester Lecture 33 Notes

Graphics and Interaction Transformation geometry and homogeneous coordinates

PARAMETRIC EQUATIONS AND POLAR COORDINATES

1 Introduction to Matlab

Computer Graphics. Chapter 5 Geometric Transformations. Somsak Walairacht, Computer Engineering, KMITL

MAT 275 Laboratory 1 Introduction to MATLAB

Computer Graphics Hands-on

1 Affine and Projective Coordinate Notation

Section 10.1 Polar Coordinates

Computer Science 336 Fall 2017 Homework 2

Lecture 7: Arnold s Cat Map

Guided Problem Solving

Introduction to Matlab

UNIT 2 2D TRANSFORMATIONS

Matrix Transformations The position of the corners of this triangle are described by the vectors: 0 1 ] 0 1 ] Transformation:

Complex Numbers, Polar Equations, and Parametric Equations. Copyright 2017, 2013, 2009 Pearson Education, Inc.

Introduction to Homogeneous coordinates

Lecture 4: Transforms. Computer Graphics CMU /15-662, Fall 2016

2D Geometric Transformations and Matrices

COMP30019 Graphics and Interaction Three-dimensional transformation geometry and perspective

2D TRANSFORMATIONS AND MATRICES

Classes 7-8 (4 hours). Graphics in Matlab.

Plotting x-y (2D) and x, y, z (3D) graphs

MATHEMATICS FOR ENGINEERING TUTORIAL 5 COORDINATE SYSTEMS

3D Mathematics. Co-ordinate systems, 3D primitives and affine transformations

Polynomials. Math 4800/6080 Project Course

QUICK INTRODUCTION TO MATLAB PART I

Matlab notes Matlab is a matrix-based, high-performance language for technical computing It integrates computation, visualisation and programming usin

For each question, indicate whether the statement is true or false by circling T or F, respectively.

Lesson 1. Unit 2 Practice Problems. Problem 2. Problem 1. Solution 1, 4, 5. Solution. Problem 3

Differential Geometry: Circle Patterns (Part 1) [Discrete Conformal Mappinngs via Circle Patterns. Kharevych, Springborn and Schröder]

Game Engineering CS S-05 Linear Transforms

Goals: Course Unit: Describing Moving Objects Different Ways of Representing Functions Vector-valued Functions, or Parametric Curves

9.1 POLAR COORDINATES

ARRAY VARIABLES (ROW VECTORS)

Dr Richard Greenaway

18.02 Multivariable Calculus Fall 2007

PC-MATLAB PRIMER. This is intended as a guided tour through PCMATLAB. Type as you go and watch what happens.

Laboratory 1 Octave Tutorial

APPM 2460 PLOTTING IN MATLAB

Algebra II Trigonometric Functions

Presented, and Compiled, By. Bryan Grant. Jessie Ross

We can conclude that if f is differentiable in an interval containing a, then. f(x) L(x) = f(a) + f (a)(x a).

4. Two Dimensional Transformations

Isometries. 1 Identifying Isometries

Computer Vision, Assignment 1 Elements of Projective Geometry

Trigonometry Review Version 0.1 (September 6, 2004)

An interesting related problem is Buffon s Needle which was first proposed in the mid-1700 s.

10.1 Curves Defined by Parametric Equations

Computer Graphics Geometric Transformations

Computer Graphics with OpenGL ES (J. Han) Chapter IV Spaces and Transforms

Math 144 Activity #4 Connecting the unit circle to the graphs of the trig functions

6 Appendix B: Quick Guide to MATLAB R

What and Why Transformations?

Representing 2D Transformations as Matrices

2D transformations: An introduction to the maths behind computer graphics

Computer Graphics: Geometric Transformations

Physics 326 Matlab Primer. A Matlab Primer. See the file basics.m, which contains much of the following.

Appendix D Trigonometry

Basics of Computational Geometry

There are a few links in the text to other parts of the text. These function correctly only if all sections are expanded.

Given ABC with A(-1, 1), B(2, 4), and C(4, 1). Translate ABC left 4 units and up 1 unit. a) Vertex matrix: b) Algebraic (arrow) rule:

In a right triangle, the sum of the squares of the equals the square of the

521493S Computer Graphics Exercise 2 Solution (Chapters 4-5)

Part 3: 2D Transformation

6.1 Polar Coordinates

2D Image Transforms Computer Vision (Kris Kitani) Carnegie Mellon University

MAT 271 Recitation. MAT 271 Recitation. Sections 10.1,10.2. Lindsey K. Gamard, ASU SoMSS. 22 November 2013

INTRODUCTION TO MATLAB PLOTTING WITH MATLAB

Matlab Tutorial. The value assigned to a variable can be checked by simply typing in the variable name:

Review of Trigonometry

Computer Graphics and Linear Algebra Rebecca Weber, 2007

2D Object Definition (1/3)

Junior Circle Meeting 9 Commutativity and Inverses. May 30, We are going to examine different ways to transform the square below:

Game Engineering: 2D

Function f. Function f -1

Polar Coordinates. OpenStax. 1 Dening Polar Coordinates

Midterm Exam Fundamentals of Computer Graphics (COMP 557) Thurs. Feb. 19, 2015 Professor Michael Langer

Ch. 7.4, 7.6, 7.7: Complex Numbers, Polar Coordinates, ParametricFall equations / 17

Lesson 28: When Can We Reverse a Transformation?

1 Trigonometry. Copyright Cengage Learning. All rights reserved.

Lesson 27: Angles in Standard Position

R(-14, 4) R'(-10, -2) S(-10, 7) S'(-6, 1) T(-5, 4) T'(-1, -2)

Lesson 3. Investigation. Name: a. Use the matrix to find the rotation image of the point (-1, 5).

AQA GCSE Further Maths Topic Areas

Hello, welcome to the video lecture series on Digital Image Processing. So in today's lecture

MAT 275 Laboratory 3 Numerical Solutions by Euler and Improved Euler Methods (scalar equations)

L2 Translations, Reflections, and Rotations Pre-Assessment Per Date

(Refer Slide Time: 00:04:20)

CS-321 Thursday 12 September 2002 Quiz (3 pts.) What is the purpose of a control grid in a cathode ray tube (CRT)?

Polar Coordinates

Writing MATLAB Programs

MATLAB Tutorial. Mohammad Motamed 1. August 28, generates a 3 3 matrix.

Transcription:

MAT 4 Laboratory 4 Plotting and computer animation in MATLAB In this laboratory session we will learn how to. Plot in MATLAB. The geometric properties of special types of matrices (rotations, dilations, reflections).. Perform simple computer animations in MATLAB Plotting in MATLAB If x and y are vectors of the same size, the command plot(x,y) will produce a plot of all the (x i, y i ) pairs, and each point will be connected to the next by a line segment. If the x-coordinates are taken close enough together, the graph should resemble a smooth curve. The command plot(x,y, o ) will mark the ordered pairs with o s, but will not connect the points. For example, to plot the function f(x) = ex/ sin x on the interval [, ], set x + x = :.:; y = exp(x/).*sin(x)./(x+); (note the. before the * and /, as these operations must be done component-wise). The command plot(x,y) will generate the graph of the function. To compare the graph with that of sin x, we could set z = sin(x); and use the command plot(x,y,x,z) to plot both curves at the same time. It is also possible to do more sophisticated types of plots in MATLAB, including polar coordinates, contour plots, and three-dimensional surfaces. Given the points (x i, y i ), rather than creating the vectors x and y, an equivalent approach, which often is more convenient, is to create a n matrix of data: x x A =... x n y y... y n then typing plot(a(,:),a(,:)) In other words, the first row of A gives the x s and the second row the y s. EXAMPLE Consider the square whose vertices are (, ), (, ), (, ), (, ). (a) Enter the x and y coordinates of these points as MATLAB vectors by setting x = [,,,, ] y = [,,,, ] The coordinates of the first points are repeated in the fourth column of x and y because we want to connect the fourth point back to the first. The command plot(x,y) draws this square in the graph window. The command axis([-,,-,]) will rescale the axes so that the square does not take up the entire figure.

(b) Now let s store the entire figure in a matrix S by storing the x-coordinate in the first row and the y-coordinate in the second row: S = [,,,, ;,,,, ] (if x and y have already been entered we can simply type S = [x; y] ). The square can then be plotted entering plot(s(,:),s(,:)) Again, set axis([-,,-,]), and add a grid by entering grid on. Rotation Matrices in R Any -dimensional matrix of the form [ cos θ sin θ Q = sin θ cos θ ] is a rotation matrix. If v is a vector in R, the product Qv is the vector v rotated θ radians in the counterclockwise direction around the origin (, ). To rotate the vector in the clockwise direction we simply replace θ with θ, thus Q cos( θ) sin( θ) =. sin( θ) cos( θ) Since cos( θ) = cos θ and sin( θ) = sin(θ), we have [ Q cos θ sin θ = sin θ cos θ ] = Q T. EXAMPLE We can rotate the square S from Example by an angle of π/4 in the counterclockwise direction by multiplying the matrix S by cos(π/4) sin(π/4) Q = sin(π/4) cos(π/4) % clear all settings for the plot S=[,,,,;,,,,]; plot(s(, :), S(, :), linewidth,) % plot the square theta= pi/4; % define the angle theta Q = [cos(theta), -sin(theta); sin(theta), cos(theta)]; %define the rotation matrix QS = Q*S; % rotate the square plot(qs(, :),QS(, :), -r, linewidth,) % plot the rotated square title( Example of Rotation ) % add a title leg( original square, rotated square ) % add a leg axis([-,,-,]); % set the window grid on % add a grid axis equal % set the axis equal

Example of Rotation.5 original triangle rotated triangle.5 -.5 - -.5 - - -.5 - -.5.5.5 Figure L4a: Original square and rotated square Remarks: The command concatenates all subsequent plot commands on the same graph. Type hold off to start a new plot. The -r plots the figure in red. grid on adds a grid to the axis axis equal sets the same scale on the x and y axes. linewidth, sets the width of the line to points = /7 inch (the default is.5 points). Dilation and Contraction Consider the matrix D = αx. α y If we multiply this matrix by any vector v = [ x y ] T we obtain the vector [ αx x α y y ] T which scales the vector by a factor α x in the x-direction and by a factor α y in the y-direction. The scalings are a dilation or contraction, deping on whether α x and α y are greater or smaller than one. EXAMPLE Consider the case where the dilation factors are α x = and α y =. We can plot the original square S together with the dilated version by executing the following commands: S=[,,,,;,,,,]; plot(s(,:),s(,:), linewidth,) D = [, ;, ]; % dilation matrix DS = D*S; plot(ds(,:),ds(,:), -r, linewidth,) title( Example of Dilation ) leg( original square, dilated square, location, southeast ) grid on axis equal, axis([-4,4,-4,4]) %adjust the axis and the window

4 Example of dilation - - - original triangle dilated triangle -4-4 - - - 4 Figure L4b: Original square and dilated square Shear Matrices tx A matrix of the form T = is a horizontal shear transformation. When we apply this transformation, the y coordinates are unaffected, [ but ] the x coordinates are translated linearly with y. Similarly, a matrix of the form T = is a vertical shear transformation: the x coordinates are t y unaffected, but the y coordinates are translated linearly with x. EXAMPLE 4 We will [ apply ] a horizontal shear transformation of units. The matrix that accomplishes that is T =. We can plot the original square together with the sheared version by executing the following commands: S=[,,,,;,,,,]; plot(s(,:),s(,:), linewidth,) T=[,;,]; % shear transformation matrix TS=T*S; plot(ts(,:),ts(,:), -r, linewidth,); title( Example of horizontal shear ) leg( original square, sheared square, location, southeast ) axis equal, axis([-,,-,]); grid on %adjust the axis and the window Example of horizontal shear - - original square sheared square - - - - Figure L4c: Original square and sheared square 4

Composition of Transformations One can perform a composition of two (or more) transformations by multiplying by the corresponding matrices in the appropriate order. EXAMPLE 5 We can see the effect of first rotating the square 45 degrees counterclockwise and then applying the shear from EXAMPLE, by plotting the product T*Q*S. Note the order of the matrices; here we first apply the rotation (Q is the matrix that multiplies S first) and then we apply the shear. S=[,,,,;,,,,]; plot(s(,:),s(,:), linewidth,) Q = [cos(pi/4), -sin(pi/4); sin(pi/4), cos(pi/4)]; T = [, ;, ]; TQS = T*Q*S; plot(tqs(,:),tqs(,:), -r, linewidth,) title( Example of rotation and shear ) leg( original square, modified square ) axis equal, axis([-,,-,]); grid on Example of rotation and shear.5 original square modified square.5.5 -.5 - - -.5.5.5.5 Figure L4d: Original square and square first rotated and then sheared Animation EXAMPLE 6 The goal of this example is to draw the square in its original size, then cause it to disappear, and finally to redraw it as it looks after it is dilated by a factor of 9/8. If this operation is repeated ten times in succession, the square will appear to be expanding. To do this we need to first plot the square and store its handle in p: p = plot(s(,:),s(,:)) After we transform the matrix by multiplying by the appropriate transformation matrix, we can erase the original figure and draw the new figure with the command set(p, xdata,s(,:), ydata,s(,:)); 5

Since we are repeating the same operation ten times, we will use a for loop. After we have gone through this loop, we create another one that contracts the matrix by a factor of 8/9, thereby returning it to its original size. The following are the commands that produce the effects. Enter them in MATLAB (you need not enter the comments that follow the % signs). The pause(.) command is used to adjust the speed of the plots so that we can see the graphs before they are erased. You might need to change the. to suit the speed of your machine. % clear all settings for the plot S=[,,,,;,,,,]; D = 9/8*eye(); % dilation matrix p = plot(s(,:),s(,:)); % plot the square axis([-,4,-,4]) % set size of the graph axis square, grid on % make the display square and add grid % hold the current graph for i = : S = D*S; % dilate the square set(p, xdata,s(,:), ydata,s(,:)); % erase original figure and plot % the transformed figure pause(.) % adjust this pause rate to suit your computer. D = 8/9*eye(); % contraction matrix for i = : S = D*S; % contract the square set(p, xdata,s(,:), ydata,s(,:)); % erase original figure and plot % the transformed figure pause(.) % adjust this pause rate to suit your computer. EXERCISES Instructions: For each of the following exercises, create an M-file to store the MATLAB commands. Copy and paste the M-file into a text document. For problems and, include in the text document the pictures produced by MATLAB. Resize and crop the pictures so that they do not take up too much space. If the question requires written answers, include them in the text file in the appropriate location. Make sure you clearly label and separate all the Exercises. For problems and 4 you do not need to include the picture.. Determine the shearing transformation matrix that shears units in the vertical direction. Plot the original square together with the sheared square. Use axis([-,4,-,4]);. Add a grid, a leg and a title (similarly to EXAMPLE 4). Include the M-file as well as the figure.. Consider the original square S. First apply the shear transformation from EXAMPLE 4 and then rotate the square 45 counterclockwise. Plot the resulting figure (together with the original square) and compare with the plot in Example 5. Are the results the same? Does the order of the transformations matter? Include the M-file as well as the figure and don t forget to answer all questions. Hint: Here you want to apply the shear first, then rotate, whereas in Example 5 the square is first rotated and then the shear is applied. To do this, TQS = T*Q*S, where Q is the rotation matrix and T is the shearing transformation matrix. So, if you want to first shear and then rotate, in what order will you multiply the matrices?. Adapt the procedure developed in Example 6 to rotate the square counterclockwise by increments of π/ about the origin. Stop when the square is in its original location and then rotate it in the clockwise direction until the square is in its original location again. You may want to rescale the 6

axis to axis([-,,-,]) Include the M-file. Do not include the figure. Hint: Since you are performing a computation several times, you will want to use two for loops: one loop for the counterclockwise rotation and another one for the clockwise rotation. Think about how many times you will need to go through the loops, keeping in mind that you are rotating the square counterclockwise for a full circle by increments of π/ and then rotating the square clockwise back again. 4. Adapt the procedure developed in Example 6 to show the square rotating in a counterclockwise direction about the origin by increments of π/ for a total angle of π and expanding at the same time by a factor of /, then stopping and rotating in the clockwise direction as it shrinks to its original size (with a contraction factor of /). At the of the program, the figure should have returned to its original size and original location. You may want to rescale the axis to axis([-8,8,-8,8]) Include the M-file. Do not include the figure. Hint: Similarly to Exercise, you might want to use two for loops: one loop for the counterclockwise rotation + dilation, and another one for the clockwise rotation + contraction. Think about how many times you will need to go through the loops. Homogeneous coordinates Translations are not linear transformations and consequently cannot be accomplished via matrix multiplication using a matrix. To circumvent this problem we use homogeneous coordinates. The homogeneous coordinates for a point (x, x ) T are (x, x, ) T. However, when the point represented by the homogeneous coordinates (x, x, ) T is plotted, only the x and x are taken into consideration. The is ignored. This allows us to produce the translated coordinates for a figure by matrix multiplication. Suppose we want to translate the vector c units horizontally and c units vertically. In homogeneous coordinates the translated vector is (x + c, x + c, ) T. We need a matrix M such that M(x, x, ) T = (x + c, x + c, ) T It is easy to verify that the matrix has the desired property. M = c c EXAMPLE 7 We will apply a horizontal translation of units and a vertical translation of unit. The matrix that accomplishes that is M =. To apply the transformation to the square S, we will need to convert the square to homogeneous coordinates. This is done by adding a row of ones to the matrix S so that the new matrix is S =. We can plot the original square together with the translated version by executing the following commands: S=[,,,,;,,,,;,,,,]; %square in homogeneous coordinates M=[,,;,,;,,]; %translation matrix MS=M*S; % apply the translation to the square plot(s(,:),s(,:), k, linewidth,); %plot the original square in black plot(ms(,:),ms(,:), r, linewidth,); % plot the translated square in red 7

leg( original square, translated square ); axis([-,5,-,5]), grid on %adjust the axis 5 original square translated square 4 - - 4 5 Figure L4e: Original square and translated square EXAMPLE 8 Consider the matrix S representing the original square in homogeneous coordinates. In the following M-file we translate the square horizontally using c =. and c = for times. We then translate the square vertically using c = and c =. and iterations. S=[,,,,;,,,,;,,,,]; % define the square in homogeneous coordinates c =.; c =.; M = [,,c;,,;,,]; % define the first translation matrix M = [,,;,,c;,,]; % define the second translation matrix p = plot(s(,:),s(,:)); % plot the original square axis([-,5,-,5]), grid on axis square figure(gcf) for i = : S = M*S; % translate the square horizontally set(p, xdata,s(,:), ydata,s(,:)); % plot the translated square pause(.) for i = : S=M*S; % translate the square vertically set(p, xdata,s(,:), ydata,s(,:)); % plot the translated square pause(.) Reflection Matrices The reflection matrix about the line L with direction the unit vector u is given by R = uu T I, where I is the identity matrix. ( For instance, to obtain the reflection matrix about the line at 45 degrees we can take u =, ) T. / / Then uu T = and R =. (Note that this answer makes perfect sense, since the / / 8

reflection of the vector x = (x, x ) T about the line at 45 degrees is the vector (x, x ) T ). In homogeneous coordinates, the reflection matrix becomes: R = The other types of basic transformations (rotations, dilations) can all be adapted to homogeneous coordinates by augmenting the matrix by the row (,, ) and the column (,, ) T. For instance, in homogeneous coordinates, the dilation matrix that dilates by a factor α becomes EXERCISES D = α α 5. Consider the translated square MS in EXAMPLE 7. Apply to this square the reflection matrix that reflects about the line at 45 degrees. Plot the translated square in black and the reflected square in red. Use axis([-,5,-,5]) and plot the line y = x using the command plot([-,5],[-,5]); (this command plots the line connecting the points (, ) and (5, 5)). Add a leg and a grid. You should reproduce the figure below. Include the M-file and the figure.. 5 4 translated square reflected square line y=x - - 4 5 6. Modify the M-file in EXAMPLE 8 adding translations that bring the square to its original position using iterations and a single for loop. Include the M-file. You do not need to include the figure. Rotations about a point different than the origin cos θ sin θ The rotation matrix Q = performs a rotation about the origin, but what if we want to sin θ cos θ rotate about a point P = (P x, P y ) different than the origin? To accomplish this we need to translate the object so that P will coincide with the origin, i.e. translate P x units horizontally and P y units vertically rotate the object translate the object back, i.e. translate P x units horizonally and P y units vertically 9

To compose the three transformations above, we multiply the appropriate matrices: P x cos θ sin θ P x P y sin θ cos θ P y Note that the rotation matrix is written in homogeneous coordinates. EXAMPLE 9 In this example we first translate the square horizontally units using increments of.. We then rotate the resulting square clockwise π/ radians around the vertex (, ) using increments of π/ radians. S=[,,,,;,,,,;,,,,]; % define the square in homogeneous coordinates M = [,,.;,,;,,]; % define the first translation matrix theta = pi/; % define the angle theta Q=[cos(theta),-sin(theta),;sin(theta),cos(theta),;,,]; % rotation matrix about the origin QP=[,,;,,;,,]*Q *[,,-;,,;,,]; % define the rotation matrix around (,) p = plot(s(,:),s(,:)); % plot the original tsquare axis([-.5,5,-.5,]), grid on axis equal figure(gcf) for i = : S = M*S; % compute the translated square set(p, xdata,s(,:), ydata,s(,:)); % plot the translated square pause(.) for i = :5 S=QP*S; % compute the rotated square set(p, xdata,s(,:), ydata,s(,:)); % plot the rotated square pause(.) EXERCISES 7. Consider the square in EXAMPLE 9. The goal of this exercise is to bring back the square to its original position by first translating it horizontally to the left two units using iterations, and then rotating it counterclockwise π/ radians around the point (, ) using 5 iterations. This can be done by modifying the code in EXAMPLE 9 by adding two for loops. The first loop should translate the square while the second should rotate it around the point (, ). Note that the rotation is counterclockwise, while in EXAMPLE 9 it was clockwise. Include the M-file. You do not need to include the figure.