Instructions
Objective
Write a program to rotate coordinates in matlab.
Requirements and Specifications
Description: Create a function, or program(.m file), or command line entries that takes a list of (𝑥, 𝑦) coordinate points that represent a closed object when lines are drawn between successive points. Then perform a rotation of 𝜃 degrees on these points to create a new list of coordinate points (see page 223/303 in the text). Then plot the figures represented by the original and modified coordinate points. Try a few different objects and values of 𝜃. Optional challenge: can you make a single function to do this more generally with two inputs: an 𝑚𝑥2 matrix of the original 𝑚 points and the angle 𝜃?Create a function, or program(.m file), or command line entries that takes a list of (𝑥, 𝑦) coordinate points that represent a closed object when lines are drawn between successive points. Then perform a rotation of 𝜃 degrees on these points to create a new list of coordinate points (see page 223/303 in the text). Then plot the figures represented by the original and modified coordinate points. Try a few different objects and values of 𝜃. Optional challenge: can you make a single function to do this more generally with two inputs: an 𝑚𝑥2 matrix of the original 𝑚 points and the angle 𝜃?Create a function, or program(.m file), or command line entries that takes a list of (𝑥, 𝑦) coordinate points that represent a closed object when lines are drawn between successive points. Then perform a rotation of 𝜃 degrees on these points to create a new list of coordinate points (see page 223/303 in the text). Then plot the figures represented by the original and modified coordinate points. Try a few different objects and values of 𝜃. Optional challenge: can you make a single function to do this more generally with two inputs: an 𝑚𝑥2 matrix of the original 𝑚 points and the angle 𝜃?Create a function, or program(.m file), or command line entries that takes a list of (𝑥, 𝑦) coordinate points that represent a closed object when lines are drawn between successive points. Then perform a rotation of 𝜃 degrees on these points to create a new list of coordinate points (see page 223/303 in the text). Then plot the figures represented by the original and modified coordinate points. Try a few different objects and values of 𝜃. Optional challenge: can you make a single function to do this more generally with two inputs: an 𝑚𝑥2 matrix of the original 𝑚 points and the angle 𝜃?
Source Code
MAIN
clc, clear all, close all
%% Points for a square
x = [1,1,1,1,1,1,2,3,4,5,5,5,5,5,5,1,2,3,4,5];
y = [1,2,3,4,5,1,1,1,1,1,1,2,3,4,5,5,5,5,5,5];
[xp,yp] = rotate(x,y,45);
figure
subplot(1,2,1)
scatter(x, y, 'filled')
hold on
scatter(xp,yp, 'filled')
axis equal
legend('Original', 'Rotated')
%% Now test the function for Optinal Challenge
P = [x', y'];
[xp,yp] = rotate2(P,45);
subplot(1,2,2)
scatter(x, y, 'filled')
hold on
scatter(xp,yp, 'filled')
axis equal
legend('Original', 'Rotated')
ROTATE
function [xp, yp] = rotate(x, y, theta)
% This function takes two vectors x and y containing points in
% the x-y plane, and an angle 'theta' in the degrees.
% The function then rotates the points in the counter-clockwise
% direction and returns the new vector of points
% Convert angle to radians
theta = theta*pi/180;
% Declare rotation matrix
R = [cos(theta), sin(theta); -sin(theta), cos(theta)];
% Perform rotation
xp = x;
yp = y;
for i = 1:length(x)
points = [x(i);y(i)];
points_rot = R*points;
xp(i) = points_rot(1);
yp(i) = points_rot(2);
end
ROTATE 2
function [xp, yp] = rotate2(P, theta)
% This function takes a matrix P of size mx2 containing m points in
% the x-y plane, where the first column are the x-points and the second
% column are the y-points, and an angle 'theta' in the degrees.
% The function then rotates the points in the counter-clockwise
% direction and returns the new vector of points
% Get x and y
x = P(:,1);
y = P(:,2);
% Convert angle to radians
theta = theta*pi/180;
% Declare rotation matrix
R = [cos(theta), sin(theta); -sin(theta), cos(theta)];
% Perform rotation
xp = x;
yp = y;
for i = 1:length(x)
points = [x(i);y(i)];
points_rot = R*points;
xp(i) = points_rot(1);
yp(i) = points_rot(2);
end