Creating a set of equally spaced cylinders with common centre
댓글 수: 2
답변 (1개)
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
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.
참고 항목
카테고리
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!