Search code examples
matlab3d2dprojection

project 3D plot to 2D screen plane


I have got the following in Matlab (solution as in the example in http://uk.mathworks.com/help/matlab/ref/viewmtx.html):

enter image description here

subplot(211)
h = ezplot3('cos(t)', 'sin(t)', 'sin(5*t)', [-pi pi]);
data = get(h,{'XData','YData','Zdata'});
data = [cat(1,data{:})', ones(numel(data{1}),1)];

% Projection matrix on screen
[az,el] = view(); A = viewmtx(az,el);
data_transformed = A*data';

subplot(212)
plot(data_transformed(1,:), data_transformed(2,:))

That transformation does not work with:

h = ezplot3('t', 'sin(t)', '20*cos(t)', [0 10*pi]);

enter image description here

How to get the screen projection of the 3rd plot?

Also, any links to the math behind the projection, with examples would be nice too :)


Solution

  • It turns out you need to normalize by the DataAspectRatio, so the viewTransform matrix becomes:

    [az, el] = view(gca);
    A = viewmtx(az,el) * makehgtform('scale',1./get(gca,'DataAspectRatio'));
    

    The full answer can be seen on http://uk.mathworks.com/matlabcentral/answers/248362-screen-2d-projection-of-3d-plot