Screen (2D) projection of 3D plot

3 visualizzazioni (ultimi 30 giorni)
Oleg Komarov
Oleg Komarov il 13 Ott 2015
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,:))

Risposta accettata

Mike Garrity
Mike Garrity il 13 Ott 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:
plot(x,y)
axis equal
I think I've neglected to account for the PlotBoxAspectRatio, but I'm not 100% sure of that.
Hope that helps.
  2 Commenti
Oleg Komarov
Oleg Komarov il 13 Ott 2015
Haha that was sync thinking. I just posted on your blog :)
EMMANUEL VIRATEL
EMMANUEL VIRATEL il 4 Apr 2021
Hello Mike,
I'm emmanuel, an electronic student from TOULOUSE (France) I've seen your post above with the screw and, with homogenous coordinates, i want to draw with the instruction plot a sinus cardinal in 3D like in the end of my code but with plot and not with mesh. it takes months that i search the solution but i hope you will answer me quickly on my personnal email: viratelemmanuel@yahoo.fr
h = ezplot3('t', 'sin(t)', '20*cos(t)', [0 10*pi]);
pts = [h.XData; h.YData; h.ZData; ones(size(h.XData))];
mat = viewmtx(-37.5,30) * makehgtform('scale',1./[20 1 20])
pt2 = mat * pts;
x = pt2(1,:) ./ pt2(4,:);
y = pt2(2,:) ./ pt2(4,:);
plot(x,y)
axis equal
% Now, what i want to draw with plot
x=-pi/2:pi/100:pi/2;
y=x;
[X,Y]=meshgrid(x,y);
Z=sinc(X.^2+Y.^2);
h=mesh(X,Y,Z);
Thanks a lot
Best regard
Emmanuel

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Mathematics in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by