Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Assuming a cylinder aligned with the <code>z</code>-axis, radii <code>R</code> linearly spaced along the unit height above the <code>XY</code>-plane (same assumptions as built-in <code>cylinder</code>):</p> <pre><code>function [x,y,z] = solidCylinder(varargin) %// Basic checks assert(nargin &gt;= 1, 'Not enough input arguments.'); assert(nargin &lt;= 3, 'Too many input arguments.'); assert(nargout &lt;= 3, 'Too many output arguments.'); %// Parse input N = 20; Ax = []; switch nargin case 1 %// R R = varargin{1}; case 2 %// Ax, R or R, N if ishandle(varargin{1}) Ax = varargin{1}; R = varargin{2}; else R = varargin{1}; N = varargin{2}; end case 3 %// Ax, R, N Ax = varargin{1}; R = varargin{2}; N = varargin{3}; end %// Check input arguments if ~isempty(Ax) assert(ishandle(Ax) &amp;&amp; strcmp(get(Ax, 'type'), 'axes'),... 'Argument ''Ax'' must be a valid axis handle.'); else Ax = gca; end assert(isnumeric(R) &amp;&amp; isvector(R) &amp;&amp; all(isfinite(R)) &amp;&amp; all(imag(R)==0) &amp;&amp; all(R&gt;0),... 'Argument ''R'' must be a vector containing finite, positive, real values.'); assert(isnumeric(N) &amp;&amp; isscalar(N) &amp;&amp; isfinite(N) &amp;&amp; imag(N)==0 &amp;&amp; N&gt;0 &amp;&amp; round(N)==N,... 'Argument ''N'' must be a finite, postive, real, scalar integer.'); %// Compute cylinder coords (mostly borrowed from builtin 'cylinder') theta = 2*pi*(0:N)/N; sintheta = sin(theta); sintheta(N+1) = 0; M = length(R); if M==1 R = [R;R]; M = 2; end x = R(:) * cos(theta); y = R(:) * sintheta; z = (0:M-1).'/(M-1) * ones(1,N+1); %' if nargout == 0 oldNextPlot = get(Ax, 'NextPlot'); set(Ax, 'NextPlot', 'add'); %// The side of the cylinder surf(x,y,z, 'parent',Ax); %// The bottom patch(x(1,:) , y(1,:) , z(1,:) , z(1,:) ); %// The top patch(x(end,:), y(end,:), z(end,:), z(end,:)); set(Ax, 'NextPlot', oldNextPlot); end end </code></pre> <p>To check whether points are inside a cylinder of height <code>L</code> (note: assuming a true 'cylinder' as created with <code>[R R]</code>, and NOT some compound object (cones with cylinders) as created by <code>[R1 R2 ... RN]</code> with at least two different values):</p> <pre><code>function p = pointInCylinder(x,y,z) %// These can also be passed by argument of course R = 10; L = 5; %// Basic checks assert(isequal(size(x),size(y),size(z)), ... 'Dimensions of the input arguments must be equal.'); %// Points inside the circular shell? Rs = sqrt(x.^2 + y.^2 + z.^2) &lt;= R; %// Points inside the top and bottom? Os = z&gt;=0 &amp; z&lt;=L; p = Rs &amp; Os; end </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload