I have the code to plot a convex hull using my data including 3 points. My question is that how I can have a transparent convex hull with its projection on surfaces x-y, x-z, and y-z?
x=[10 10 40 60 60 60];
y=[0 10 40 30 10 0];
z=[10 13 40 60 57 50];
tri = delaunay(x,y);
trisurf(tri,x,y,z)

 Akzeptierte Antwort

Mike Garrity
Mike Garrity am 16 Jun. 2015

0 Stimmen

Something like this?
x=[10 10 40 60 60 60];
y=[0 10 40 30 10 0];
z=[10 13 40 60 57 50];
tri = delaunay(x,y);
trisurf(tri,x,y,z)
n = length(x);
dx = 60+zeros(n,1) + 5;
dy = 40+zeros(n,1) + 5;
dz = 10+zeros(n,1) - 5;
px = patch('Faces',tri,'Vertices',[dx y' z']);
py = patch('Faces',tri,'Vertices',[x' dy z']);
pz = patch('Faces',tri,'Vertices',[x' y' dz]);
set([px,py,pz],'FaceAlpha',.5,'EdgeColor','none');

6 Kommentare

rela rela
rela rela am 16 Jun. 2015
Bearbeitet: rela rela am 16 Jun. 2015
Thanks Mike! I also need the transparent figure to see the corners clearly, and each point coordinate with dashed line connecting the corners (including 6 point) to their 2D projected points, is that possible?
I'm not following the part about the transparent figure, but the lines are easy to add:
for i=1:n
line([x(i) dx(i)],repmat(y(i),[1 2]),repmat(z(i),[1 2]),'LineStyle','--')
line(repmat(x(i),[1 2]),[y(i) dy(i)],repmat(z(i),[1 2]),'LineStyle','--')
line(repmat(x(i),[1 2]),repmat(y(i),[1 2]),[z(i) dz(i)],'LineStyle','--')
end
rela rela
rela rela am 17 Jun. 2015
Thank you. May I ask how you calculated the coefficients in dx, dy, and dz? I want to use this method for some other data.
I just made them up. You want an array of the right size which contains constant values for where you want the shadow to be. For example, if I wanted to push the bottom down to Z=-10, I would want this:
dz = [-10; -10; -10; ...]
The numbers I used were just the defaults when I drew your 3D patch, with an extra 5 added (or subtracted) to push things out a little bit.
rela rela
rela rela am 18 Jun. 2015
Bearbeitet: rela rela am 18 Jun. 2015
I use the "K = convhulln(X);" "trimesh(K,X(:,1),X(:,2),X(:,3))" instead of "tri = delaunay(x,y);" for another set of data, the values are different from previous data and also they are 3 points instead of 6, can I used the same dx, dy, and dz for them also? This is the code:
x=[2 7.5 10.5];
y=[8 18.4 25];
z=[16 28.9 44];
% x=[10 10 40 60 60 60];
% y=[0 10 40 30 10 0];
% z=[10 13 40 60 57 50];
X = [x; y; z]'; %# involves a 3D point on each row
K = convhulln(X);
trimesh(K,X(:,1),X(:,2),X(:,3))
% 2D projection
n=length(x);
dx=60+zeros(n,1)+5;
dy=40+zeros(n,1)+5;
dz=10+zeros(n,1)-5;
px=patch('Faces',K,'Vertices',[dx y' z']);
py=patch('Faces',K,'Vertices',[x' dy z']);
pz=patch('Faces',K,'Vertices',[x' y' dz]);
set([px,py,pz],'FaceAlpha',.5,'EdgeColor','none');
% 2D coordinate (Dashed lines)
for i=1:n
line([x(i) dx(i)],repmat(y(i),[1 2]),repmat(z(i),[1 2]),'LineStyle','--')
line(repmat(x(i),[1 2]),[y(i) dy(i)],repmat(z(i),[1 2]),'LineStyle','--')
line(repmat(x(i),[1 2]),repmat(y(i),[1 2]),[z(i) dz(i)],'LineStyle','--')
end
%But it doesnt work, it says
%"Error using cgprechecks (line 40)
%Not enough unique points specified.
% Error in convhulln (line 41)
% cgprechecks(x, nargin, cg_opt);
% Error in Untitled5 (line 12)
% K = convhulln(X);"
% Does it work for you? Because I have a problem,
% when change the folder of my code it gives me an error
% "Attempt to execute SCRIPT trimesh as a function:
%C:\Users\...(?)\Desktop\trimesh.m
% Error in Untitled (line 11)
% trimesh(tri,x,y,z)"
% I should add also I could run the code with previous data (6 points)
% once and it game me the figure, but again when I open and run it,
% it gives the % last error
% ("Attempt to execute SCRIPT trimesh as a function:
%C:\Users\...(?)\Desktop\trimesh.m
% Error in Untitled (line 11)
% trimesh(tri,x,y,z)" ")
There are a couple of different issues here.
This message:
Error using cgprechecks (line 40)
Not enough unique points specified.
is trying to say that 3 points isn't enough to define a convex hull in 3D. You need at least one more than the dimension of the space you're in.
I'm not sure what caused this message:
Attempt to execute SCRIPT trimesh as a function
but it looks like you have a file named "trimesh.m" on your path. If you do this:
which trimesh
it should point you at the "toolbox/matlab/specgraph" directory. If it points somewhere else, you've probably got to delete or rename this extra copy.
It also looks like you've changed the shape of your x,y,z arrays from Nx1 to 1xN. When I created the constant dx,dy,dz arrays, I assumed the first orientation. You'll probably want to fix that, then you can remove some of the transposes. It probably would have been better if I'd created them like this:
dx = 65 + zeros(size(x));
dy = 45 + zeros(size(y));
dz = 5 + zeros(size(z));
px=patch('Faces',K,'Vertices',[dx; y; z]');
py=patch('Faces',K,'Vertices',[x; dy; z]');
pz=patch('Faces',K,'Vertices',[x; y; dz]');
set([px,py,pz],'FaceAlpha',.5,'EdgeColor','none');
for i=1:n
line([x(i) dx(i)], ...
repmat(y(i),[1 2]), ...
repmat(z(i),[1 2]),'LineStyle','--')
line(repmat(x(i),[1 2]), ...
[y(i) dy(i)], ...
repmat(z(i),[1 2]),'LineStyle','--')
line(repmat(x(i),[1 2]), ...
repmat(y(i),[1 2]), ...
[z(i) dz(i)],'LineStyle','--')
end

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by