How to rotate ellipsoid correctly in MATLAB?

Viewed 85

I am attempting to create, rotate, then translate an ellipsoid to the middle of two points (ex: (0,0,0) and (100,100,100)).

%set up figure
figure
xlabel('x')
ylabel('y')
zlabel('z')
grid on

%create ellipsoid at origin with:
%x semi-axis = 10, y semi-axis = 3, z %semi-axis = 3
[x,y,z] = ellipsoid(0,0,0, 10,3,3,30);

%create surface out of ellipsoid
h = surf(x,y,z);
hold on

%rotate surface -45 degrees on Y axis (Pitch)
rotate(h, [0 1 0], -45);

%rotate surface 45 degrees on Z axis (Yaw)
rotate(h, [0 0 1], 45);


%get X,Y,Z data points for translation
ch = get(gca, 'children');
X = get(ch, 'Xdata');
Y = get(ch, 'Ydata');
Z = get(ch, 'Zdata');

%create two points: one at origin, the other at (100,100,100)
pointA = [0 0 0];
pointB = [100 100 100];

%translate ellipsoid to midpoint between pointA and pointB
X = X + (pointA(1) + pointB(1))/2;
Y = Y + (pointA(2) + pointB(2))/2;
Z = Z + (pointA(3) + pointB(3))/2;

%plot rotated+translated ellipsoid
surf(X,Y,Z);

%remove original ellipsoid from figure
delete(h);
hold on

%create and plot line from (0,0,0) to (100,100,100)
lineX = [ pointA(1) pointB(1)];
lineY = [ pointA(2) pointB(2)];
lineZ = [ pointA(3) pointB(3)];

plot3(lineX,lineY,lineZ);

Here is my result result

As shown, the vertical rotation of the ellipsoid doesn't line up with the line from (0,0,0) to (100,100,100).

What am I doing wrong?

0 Answers
Related