问题
I'm looking for a way to draw a cone at a specific location in a 3D surf plot. Would it also be possible to have the cone 50% transparent?
Right now I'm drawing a basic 3d environment using the surf function. I'm trying to use the function plot3 to draw a cone with specific dimensions at a specific location.
回答1:
The following code creates a cone by pinching a cylinder between zero and one with t=[0;1]. The alpha(...) function fan then be used to set the transparency. To reposition a cylinder you must add a value to the x,y, or z or perform a rotation (beyond the scope of this answer).
t = [0;1];
[X,Y,Z] = cylinder(t);
figure;
clf;
surf(X,Y,Z);
alpha(.5)
hold all
surf(X+1,Y,Z);
alpha(.5);
axis equal
回答2:
I found this function to work exactly how I needed it to:
http://www.mathworks.com/matlabcentral/fileexchange/21951-cone/content//Cone.m
function [Cone,EndPlate1,EndPlate2] = Cone(X1,X2,R,n,cyl_color,closed,lines)
%
% This function constructs a cylinder connecting two center points
%
% Usage :
% [Cone,EndPlate1,EndPlate2] = Cone(X1,X2,R,n,cyl_color,closed,lines)
%
% Cone-------Handle of the cone
% EndPlate1------Handle of the Starting End plate
% EndPlate2------Handle of the Ending End plate
% X1 and X2 are the 3x1 vectors of the two points
% R is the radius of the cylinder/cone R(1) = start radius, R(2) = end radius
% n is the no. of elements on the cylinder circumference (more--> refined)
% cyl_color is the color definition like 'r','b',[0.52 0.52 0.52]
% closed=1 for closed cylinder or 0 for hollow open cylinder
% lines=1 for displaying the line segments on the cylinder 0 for only
% surface
%
% Typical Inputs
% X1=[10 10 10];
% X2=[35 20 40];
% r=[1 5];
% n=20;
% cyl_color='b';
% closed=1;
%
% NOTE: There is a MATLAB function "cylinder" to revolve a curve about an
% axis. This "Cylinder" provides more customization like direction and etc
Here is how I ended up using the function...
Given a center point c and a unit vector uv, and a height of the cylinder h and a radius r, here is the end usage:
Cone(c,c+uv*h,[0,r],20,'r',0,0);
The last four parameters are 20 faces, the color red, not closed on the end, and don't draw the lines.
UPDATE: Example with pictures
clearvars
close all
format compact
C = {'k','b','r','g','y',[.5 .6 .7],[.8 .2 .6]}; % Cell array of colros.
% rng(17);% set a seed
num_faces = 20;
closed = 0;
draw_edges = 1;
figure
hold on
axis equal
set(gca, 'Projection', 'orthographic');
for i = 1:5
radius = rand(1);
% get two end points
end_pts = rand(2,3);
% draw the cone
Cone(end_pts(1,:), end_pts(2,:),[0, radius], ...
20, C{mod(i,7)+1},closed, draw_edges)
alpha(0.3)
end
The Cone function is a variation on the Cylinder function someone also posted on fileexchange:
http://www.mathworks.com/matlabcentral/fileexchange/13995-cylinder/content/Cylinder.m
Hope that helps.
来源:https://stackoverflow.com/questions/18641487/using-plot3-for-drawing-cones-in-matlab