필터 지우기
필터 지우기

Creating a set of equally spaced cylinders with common centre

조회 수: 2 (최근 30일)
Minas Emiris
Minas Emiris 2018년 4월 16일
편집: Jan 2018년 4월 20일
I was wondering how to create a set of equally spaced cylinders around the centre of a circle (like a set of radii) but on 3D plot. I know how to use cylinder function, but I am confused when trying to generate a lot of cylinders without having to change the direction of each manually. Ideally, I wish to have 20 cylinders of height 80, starting not actually form the same point, but from the circumference of a circle with radius 20 (yet I want all of them to point to the centre of the circle.
  댓글 수: 2
Jan
Jan 2018년 4월 16일
The description is not trivial. What exactly is "equally spaces" here? Can you draw a small sketch?
Minas Emiris
Minas Emiris 2018년 4월 16일
Please find it uploaded.

댓글을 달려면 로그인하십시오.

답변 (1개)

Jan
Jan 2018년 4월 16일

You can use cylinder to get the coordinates of one cylinder and apply some arithmetic to translate and rotate them.

This might be easier:

function H = DrawCylinder(P1, P2, R, nVertex, FaceColor, FaceLite)
[RX, RY, RZ] = CylinderCoor(P1, P2, R, nVertex);
H = surface(RX, RY, RZ, ...
  'FaceColor',        FaceColor, ...
  'FaceLighting',     FaceLite, ...
  'EdgeColor',        'none', ...
  'AmbientStrength',  0.5, ...
  'DiffuseStrength',  0.4, ...
  'BackFaceLighting', 'lit', ...
  'Clipping',         'off');
end
function [RX, RY, RZ] = CylinderCoor(P1, P2, R, nVertex)
smallVal = 1.49e-008;  % SQRT(EPS)
% Vector in the center of the cylinder:
nL12 = P2 - P1;
nL12 = nL12 ./ sqrt(sum(nL12 .* nL12, 2));
% Base vectors of a circle around the center with normal nL12:
cX    = [nL12(:, 2), -nL12(:, 1)];
LencX = sqrt(cX(:, 1) .* cX(:, 1) + cX(:, 2) .* cX(:, 2));
badInd        = (not(isfinite(LencX)) | LencX <= smallVal);
LencX(badInd) = 1.0;
cX(badInd, 1) = 1.0;
cX(badInd, 2) = 0.0;
cX       = cX ./ [LencX, LencX];
cX(:, 3) = 0;
% cY as normalized cross product of nL12 and cX:
cY = [-nL12(:, 3) .* cX(:, 2), ...
      nL12(:, 3)  .* cX(:, 1), ...
      nL12(:, 1)  .* cX(:, 2) - nL12(:, 2) .* cX(:, 1)];
cY = cY ./ sqrt(sum(cY .* cY, 2));
% Circle with nVertex points:
% theta = (0:nVertex) * (2 * pi / nVertex);
theta    = 0:(6.283185307179586 / nVertex):6.283185307179586;
costheta = R * cos(theta);
sintheta = R * sin(theta);
sintheta(nVertex + 1) = 0;  % Delete rounding errors (?!)
% Dyadic products: [M x 1] * [1 x N] = [M x N]
DX  = transpose(cX(:, 1) * costheta + cY(:, 1) * sintheta);
DY  = transpose(cX(:, 2) * costheta + cY(:, 2) * sintheta);
DZ  = transpose(cX(:, 3) * costheta + cY(:, 3) * sintheta);
P1T = transpose(P1);
P2T = transpose(P2);  
RX(2, :, :) = bsxfun(@plus, DX, P2T(1, :));
RY(2, :, :) = bsxfun(@plus, DY, P2T(2, :));
RZ(2, :, :) = bsxfun(@plus, DZ, P2T(3, :));
RX(1, :, :) = bsxfun(@plus, DX, P1T(1, :));
RY(1, :, :) = bsxfun(@plus, DY, P1T(2, :));
RZ(1, :, :) = bsxfun(@plus, DZ, P1T(3, :));
end

This draws a cylinder from point P1 to point p2 with radius R, nVertex vertices and the specified colors and face lighting. Now all you have to do is to calculate the wanted P1 and P2.

  댓글 수: 3
Minas Emiris
Minas Emiris 2018년 4월 19일
I have one question on the code actually. I tried to copy and past it on the editor window, by defining points P1 and P2, nVertex, R typing mesh(RX,RY,RZ) right above the code, but the error message I receive is "Undefined function or variable 'RX'". Do you know how i can make the mesh plot work with your code?
Jan
Jan 2018년 4월 20일
편집: Jan 2018년 4월 20일

Copy&paste of function definitions to the command line are not working. You have to define functions inside M-files. I suggest to read the "Getting Started" chapters of the documentation or running the "Onramp" (ask an internet search engine for the link).

Save the function CylinderCoor to a file called "CylinderCoor.m" and store it in a folder of your Matlab path. Then create a new function or script, which contains:

[RX, RY, RZ] = CylinderCoor(P1, P2, R, nVertex)

Now Rx, Ry and RZ contain the values needed for a surface plot. Because mesh replies a surface object also, the inputs should be valid for this command also.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Surface and Mesh Plots에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by