Reputation: 973
I defined the following rectangle in Matlab:
A = [-4,-4,4,4,-4;-2,2,2,-2,-2;]
And I defined a transformation matrix (Special Euclidean (2)) like this:
function T = se2(x, y, theta)
T = [cosd(theta), -sind(theta), x;
sind(theta), cosd(theta), y;
0, 0, 1];
Now, I want to rotate my shape by 45 degrees counter-clockwise about its center and move it with respect to the new coordinate frame by 2 unit in the new y direction.
First problem is: when doing like the following:...
B = se2(0,2,45)*[A;1 1 1 1 1]
...it will correctly rotate but incorrectly move my shape.
Here is my rectangle(blue), incorrect transformation(red) and correct transformation(green):
Second problem is: Suppose I translated the shape by 6 direction in the y direction. I want to just rotate the rectangle by -30 degrees about it's new center but doing as I've shown, yields rotation about the former center.
How can I get around these problems in Matlab? Is there a predefined function in doing these tasks?
My code for plotting the shapes:
A =
-4 -4 4 4 -4
-2 2 2 -2 -2
plot(A(1,:),A(2,:),'blue')
Upvotes: 4
Views: 4112
Reputation: 1960
Regarding your 1st problem:
Essentially you want to do a translation of A to it's centroid before you do the rotation. This is because the rotation assumes that you're rotating about the origin. Therefore you need to "centre" it about the point you plan to rotate it about before you rotate it. Then you need to translate it back after you finish the rotation. Refer to this reference for details.
% Define A
A = [-2,-2,6,6,-2; -2,2,2,-2,-2; 1 1 1 1 1];
% Define Translation Matrix
trans = @(x,y,z) repmat([x; y; z],[1 5]);
% Define Rotation Matrix
se2 = @(x, y, theta) [
cosd(theta), -sind(theta), x;
sind(theta), cosd(theta), y;
0, 0, 1];
% Calculate Rotated Rect
B = se2(0,0,45) * (A - trans(2,0,0) ) + trans(2,0,0);
% Plot Rectangles
figure; plot(A(1,:),A(2,:),'b')
hold on;
plot(B(1,:),B(2,:),'r')
hold off;
axis equal
The function trans
will translate it before the rotation.
Result:
>> A
A =
-2 -2 6 6 -2
-2 2 2 -2 -2
1 1 1 1 1
>> B
B =
0.5858 -2.2426 3.4142 6.2426 0.5858
-4.2426 -1.4142 4.2426 1.4142 -4.2426
1.0000 1.0000 1.0000 1.0000 1.0000
Here is the A/B if rotated at the centre.
Here is the A/B with the offset.
Regarding your 2nd problem:
Same solution as the first problem except you use the new centroid and -30 deg instead for the parameters of B
.
Upvotes: 4