How to rotate cube in MATLAB.?

A = [0 0 0];
B = [1 0 0];
C = [0 1 0];
D = [0 0 1];
E = [0 1 1];
F = [1 0 1];
G = [1 1 0];
H = [1 1 1];
P = [A;B;F;H;G;C;A;D;E;H;F;D;E;C;G;B];
plot3(P(:,1),P(:,2),P(:,3))
I am creating a cube using this code.
Now i want to rotate this cube according to Roll, pitch and Yaw axis.
If R = -0.3064; P = -1.2258; Y = 9.8066
Then how to rotate this cube.?

 採用された回答

Shoaibur Rahman
Shoaibur Rahman 2014 年 12 月 28 日

2 投票

Add the following lines of code at the bottom of your code:
roll = -0.3064; pitch = -1.2258; yaw = 9.8066;
dcm = angle2dcm(yaw, pitch, roll);
P = P*dcm;
plot3(P(:,1),P(:,2),P(:,3)) % rotated cube

7 件のコメント

Nimisha
Nimisha 2014 年 12 月 29 日
Thanks Shoaibur;
But when i give This code,
roll = 360; pitch = 360; yaw = 360;
dcm = angle2dcm(yaw, pitch, roll);
P = P*dcm;
plot3(P(:,1),P(:,2),P(:,3)) % rotated cube
grid on
xlabel('x')
ylabel('y')
zlabel('z')
figure(1);
it shifts origin also. But i didn't want to shift it. It should remain at 0 everytime.
As well i want to give legends to 3D image, and want to change face colors of cube.!
Jan
Jan 2014 年 12 月 29 日
@Nimisha: No, the origin is not shifted by this rotation. If you want to keep a certain piont fixed, e.g. the center of the cube, subtract it from P before you multiply the rotation matrix dcm and add it afterwards again. bsxfun(@plus, ...) will help you.
The legends and the colors are new details, so please ask a new question or append it to the original question and mark the modifications clearly.
Nimisha
Nimisha 2014 年 12 月 31 日
編集済み: Nimisha 2015 年 1 月 2 日
I can't modify code.! i searched many codes, but for this function
bsxfun(@plus,
i didnt get much help. please help me with modifying code"
Can anyone mofify my code.?
Shoaibur Rahman
Shoaibur Rahman 2015 年 1 月 2 日
編集済み: Shoaibur Rahman 2015 年 1 月 2 日
I don't understand, what modification you do want. As mentioned, the origin is not shifted by the above code. To check this, plot the origin and the original cube on the same figure of rotated cube as shown below:
A = [0 0 0];
B = [1 0 0];
C = [0 1 0];
D = [0 0 1];
E = [0 1 1];
F = [1 0 1];
G = [1 1 0];
H = [1 1 1];
P = [A;B;F;H;G;C;A;D;E;H;F;D;E;C;G;B];
plot3(P(:,1),P(:,2),P(:,3),'g'), hold on % original cube
roll = -0.3064; pitch = -1.2258; yaw = 9.8066;
dcm = angle2dcm(yaw, pitch, roll);
P = P*dcm;
plot3(P(:,1),P(:,2),P(:,3)) % rotated cube
plot3(0,0,0,'or')
Nimisha
Nimisha 2015 年 1 月 2 日
You check this following code..
clear all;clc
A = [0 0 0];
B = [1 0 0];
C = [0 1 0];
D = [0 0 1];
E = [0 1 1];
F = [1 0 1];
G = [1 1 0];
H = [1 1 1];
P = [A;B;F;H;G;C;A;D;E;H;F;D;E;C;G;B];
plot3(P(:,1),P(:,2),P(:,3),'g'), hold on % original cube
roll = 360; pitch = 360; yaw = 360;
dcm = angle2dcm(yaw, pitch, roll);
P = P*dcm;
plot3(P(:,1),P(:,2),P(:,3)) % rotated cube
plot3(0,0,0,'or')
In this origin is shifted even all the faces are rotated by 360..
Please correct it.!
Shoaibur Rahman
Shoaibur Rahman 2015 年 1 月 2 日
編集済み: Shoaibur Rahman 2015 年 1 月 2 日
I guess, you are asking that if the rotation is 360 degrees, then both the original and rotated cube should look at the same position, right?
Instead of using 360, use 2*pi for roll, pitch, and yaw, i.e. in radians.
roll = 2*pi; pitch = 2*pi; yaw = 2*pi;
A = [0 0 0];
B = [1 0 0];
C = [0 1 0];
D = [0 0 1];
E = [0 1 1];
F = [1 0 1];
G = [1 1 0];
H = [1 1 1];
P = [A;B;F;H;G;C;A;D;E;H;F;D;E;C;G;B];
plot3(P(:,1),P(:,2),P(:,3),'g'), hold on
roll = 2*pi; pitch = 2*pi; yaw = 2*pi;
dcm = angle2dcm(yaw, pitch, roll);
P = P*dcm;
plot3(P(:,1),P(:,2),P(:,3)) % rotated cube
plot3(0,0,0,'or')
Nimisha
Nimisha 2015 年 1 月 3 日
Thank You So Much,
Finally it works for me :)

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

質問済み:

2014 年 12 月 28 日

コメント済み:

2015 年 1 月 3 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by