MATLAB Answers

Screen (2D) projection of 3D plot

70 views (last 30 days)
How can I get the screen projection (2D) of the following 3D plot:
h = ezplot3('t', 'sin(t)', '20*cos(t)', [0 10*pi]);
The following solution as in the example in viewmtx() does NOT achieve the expected result (although it does for e.g. h = ezplot3('cos(t)', 'sin(t)', 'sin(5*t)', [-pi pi])):
% Original data
data = get(h,{'XData','YData','Zdata'});
data = [cat(1,data{:})', ones(numel(data{1}),1)];
% Projection matrix
[az,el] = view();
A = viewmtx(az,el);
% Projected data
data_transformed = A*data';
plot(data_transformed(1,:), data_transformed(2,:))


Sign in to comment.

Accepted Answer

Mike Garrity
Mike Garrity on 13 Oct 2015
I talked about the theory behind this in this recent post on the MATLAB Graphics blog.
The basic idea is that you want to take the data from your object like this:
h = ezplot3('t', 'sin(t)', '20*cos(t)', [0 10*pi]);
pts = [h.XData; h.YData; h.ZData; ones(size(h.XData))];
and then multiply it by the view transform. Unfortunately accurately reproducing the view transform for an arbitrary 3D MATLAB plot can be a bit tricky. It is pretty close to this:
mat = viewmtx(-37.5,30) * makehgtform('scale',1./[20 1 20])
The first half of that is the azimuth and elevation. The second half is the DataAspectRatio.
Once you have that matrix, you can transform your points like this:
pt2 = mat * pts;
x = pt2(1,:) ./ pt2(4,:);
y = pt2(2,:) ./ pt2(4,:);
If we plot those, we'll see that I'm close, but slightly off:
axis equal
I think I've neglected to account for the PlotBoxAspectRatio, but I'm not 100% sure of that.
Hope that helps.

  1 Comment

Oleg Komarov
Oleg Komarov on 13 Oct 2015
Haha that was sync thinking. I just posted on your blog :)

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by