-
Notifications
You must be signed in to change notification settings - Fork 37
/
cylinder2P.m
64 lines (51 loc) · 1.56 KB
/
cylinder2P.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
function [X Y Z] = cylinder2P(P1,P2,R,N)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Construct a cylinder connecting two center points: P1 and P2.
% R = [R1; R2], where R1 and R2 are radiuses of the cylinder's two ends.
% If R1 = R2, R can be a variable.
% N is the number of grid points for plotting the cylinder.
%
% By V.C. Chen
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if length(R)==1
R = [R;R];
end
% Calculating the length of the cylinder
Lc = norm(P2-P1);
% angle array
theta = linspace(0,2*pi,N)';
X = zeros(length(R), N);
Y = zeros(length(R), N);
Z = zeros(length(R), N);
% cylinder axis definedd by: P1+V*[0:Lc]
V = (P2-P1)/Lc; %normalized cylinder's axis-vector;
U = rand(1,3); %linear independent vector
U = V-U/(U*V'); %orthogonal vector to V
U = U/sqrt(U*U'); %orthonormal vector to V
W = cross(V,U); %vector orthonormal to V and U
W = W/sqrt(W*W'); %orthonormal vector to V and U
P1x = P1(1);
P1y = P1(2);
P1z = P1(3);
P2x = P2(1);
P2y = P2(2);
P2z = P2(3);
Vx = V(1);
Vy = V(2);
Vz = V(3);
Ux = U(1);
Uy = U(2);
Uz = U(3);
Wx = W(1);
Wy = W(2);
Wz = W(3);
Nrad = linspace(0,1,2);
for k=1:length(R)
r = Nrad(k);
X(k, :) = P1x + (P2x-P1x)*r + R(k)*sin(theta)*Ux-R(k)*cos(theta)*Wx;
Y(k, :) = P1y + (P2y-P1y)*r + R(k)*sin(theta)*Uy-R(k)*cos(theta)*Wy;
Z(k, :) = P1z + (P2z-P1z)*r + R(k)*sin(theta)*Uz-R(k)*cos(theta)*Wz;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%