How do I rotate a rectangular surface by a certain angle and center?
1 view (last 30 days)
Show older comments
Joan Lo
on 18 Jun 2022
Commented: Joan Lo
on 18 Jun 2022
I have a 2D reward function for a reinforcement learning project which is a rectangular surface created by the multiplication of two rectangular pulses, each one built with the substraction with a pair of heaviside functions.
Although I know there's already a "heaviside" function in MATLAB, I needed to approximate it using a mathematical expression from scratch (due to certain functions incompatibilty in Simulink). The approximation is as follows:
The corresponding code is:
% Initial values
close all
xl = 8;
L = 1.4;
D = 1.5;
xvec = linspace(-xl,xl);
yvec = linspace(-D,xl);
[xmat,ymat] = meshgrid(xvec,yvec);
x = xmat/10;
y = ymat/10;
k = 300;
%% 2D rectangle surface plot
% Center coordinates
x0 = L/10; x1 = L/10;
y0 = 3*D/10; y1 = D/10;
% Moved center coordinates
% x0 = (L+5)/10; x1 = -(L+2)/10;
% y0 = 5*D/10; y1 = 3*D/10;
rect2D = ((1+exp(-2*k*(x+x0))).^-1-(1+exp(-2*k*(x-x1))).^-1).*...
((1+exp(-2*k*(y-y0))).^-1-(1+exp(-2*k*(y-y1))).^-1);
contourf(xvec,yvec,rect2D);
axis equal
The objective is to rotate this 2D rectangle by its center (whatever the coordinates of its center are and knowing the four corners) using mathematical transformations (i.e not using default rotation functions as "rotate" or "imrotate"), because in the end I wish to have a single mathematical expression with the variables "x" and "y" to be able to compute the value at any point.
I tried to do the job with rotation matrices, but I'm struggling with the matrix dimensions, since the surface is a 100x100 matrix and the 2D or 3D rotations matrices dimensions don't match and the product can't be done.
0 Comments
Accepted Answer
Abolfazl Chaman Motlagh
on 18 Jun 2022
use the cordinate linear transformation. for this first subtract the center coordinate, rotate the coordinate, and finally shift the coordinate back to center position:
so heres the analytic soltion:
if you increase number of point you get more accurate plot:
% Initial values
close all
xl = 8;
L = 1.4;
D = 1.5;
xvec = linspace(-xl,xl,1000);
yvec = linspace(-D,xl,1000);
[xmat,ymat] = meshgrid(xvec,yvec);
x = xmat/10;
y = ymat/10;
k = 300;
%% 2D rectangle surface plot
% Center coordinates
x0 = L/10; x1 = L/10;
y0 = 3*D/10; y1 = D/10;
% Moved center coordinates
% x0 = (L+5)/10; x1 = -(L+2)/10;
% y0 = 5*D/10; y1 = 3*D/10;
rect2D = ((1+exp(-2*k*(x+x0))).^-1-(1+exp(-2*k*(x-x1))).^-1).*...
((1+exp(-2*k*(y-y0))).^-1-(1+exp(-2*k*(y-y1))).^-1);
subplot(1,2,1)
contourf(xvec,yvec,rect2D);axis equal
theta = pi/6;
x_ = cos(theta)*(x-0) + sin(theta) * (y-D/5) + 0;
y_ = -sin(theta)*(x-0) + cos(theta) * (y-D/5) + (D/5);
rect2D = ((1+exp(-2*k*(x_+x0))).^-1-(1+exp(-2*k*(x_-x1))).^-1).*...
((1+exp(-2*k*(y_-y0))).^-1-(1+exp(-2*k*(y_-y1))).^-1);
subplot(1,2,2)
contourf(xvec,yvec,rect2D);
axis equal
More Answers (0)
See Also
Categories
Find more on Surface and Mesh Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!