-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy patharrow3d.m
100 lines (99 loc) · 2.74 KB
/
arrow3d.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
function [h]=arrow3d(x,y,z,head_frac,radii,radii2,colr)
%
% The function plotting 3-dimensional arrow
%
% h=arrow3d(x,y,z,head_frac,radii,radii2,colr)
%
% The inputs are:
% x,y,z = vectors of the starting point and the ending point of the
% arrow, e.g.: x=[x_start, x_end]; y=[y_start, y_end];z=[z_start,z_end];
% head_frac = fraction of the arrow length where the head should start
% radii = radius of the arrow
% radii2 = radius of the arrow head (defult = radii*2)
% colr = color of the arrow, can be string of the color name, or RGB vector (default='blue')
%
% The output is the handle of the surfaceplot graphics object.
% The settings of the plot can changed using: set(h, 'PropertyName', PropertyValue)
%
% example #1:
% arrow3d([0 0],[0 0],[0 6],.5,3,4,[1 0 .5]);
% example #2:
% arrow3d([2 0],[5 0],[0 -6],.2,3,5,'r');
% example #3:
% h = arrow3d([1 0],[0 1],[-2 3],.8,3);
% set(h,'facecolor',[1 0 0])
%
% Written by Moshe Lindner , Bar-Ilan University, Israel.
% July 2010 (C)
if nargin==5
radii2=radii*2;
colr='blue';
elseif nargin==6
colr='blue';
end
if size(x,1)==2
x=x';
y=y';
z=z';
end
x(3)=x(2);
x(2)=x(1)+head_frac*(x(3)-x(1));
y(3)=y(2);
y(2)=y(1)+head_frac*(y(3)-y(1));
z(3)=z(2);
z(2)=z(1)+head_frac*(z(3)-z(1));
r=[x(1:2)',y(1:2)',z(1:2)'];
N=50;
dr=diff(r);
dr(end+1,:)=dr(end,:);
origin_shift=(ones(size(r))*(1+max(abs(r(:))))+[dr(:,1) 2*dr(:,2) -dr(:,3)]);
r=r+origin_shift;
normdr=(sqrt((dr(:,1).^2)+(dr(:,2).^2)+(dr(:,3).^2)));
normdr=[normdr,normdr,normdr];
dr=dr./normdr;
Pc=r;
n1=cross(dr,Pc);
normn1=(sqrt((n1(:,1).^2)+(n1(:,2).^2)+(n1(:,3).^2)));
normn1=[normn1,normn1,normn1];
n1=n1./normn1;
P1=n1+Pc;
X1=[];Y1=[];Z1=[];
j=1;
for theta=([0:N])*2*pi./(N);
R1=Pc+radii*cos(theta).*(P1-Pc) + radii*sin(theta).*cross(dr,(P1-Pc)) -origin_shift;
X1(2:3,j)=R1(:,1);
Y1(2:3,j)=R1(:,2);
Z1(2:3,j)=R1(:,3);
j=j+1;
end
r=[x(2:3)',y(2:3)',z(2:3)'];
dr=diff(r);
dr(end+1,:)=dr(end,:);
origin_shift=(ones(size(r))*(1+max(abs(r(:))))+[dr(:,1) 2*dr(:,2) -dr(:,3)]);
r=r+origin_shift;
normdr=(sqrt((dr(:,1).^2)+(dr(:,2).^2)+(dr(:,3).^2)));
normdr=[normdr,normdr,normdr];
dr=dr./normdr;
Pc=r;
n1=cross(dr,Pc);
normn1=(sqrt((n1(:,1).^2)+(n1(:,2).^2)+(n1(:,3).^2)));
normn1=[normn1,normn1,normn1];
n1=n1./normn1;
P1=n1+Pc;
j=1;
for theta=([0:N])*2*pi./(N);
R1=Pc+radii2*cos(theta).*(P1-Pc) + radii2*sin(theta).*cross(dr,(P1-Pc)) -origin_shift;
X1(4:5,j)=R1(:,1);
Y1(4:5,j)=R1(:,2);
Z1(4:5,j)=R1(:,3);
j=j+1;
end
X1(1,:)=X1(1,:)*0 + x(1);
Y1(1,:)=Y1(1,:)*0 + y(1);
Z1(1,:)=Z1(1,:)*0 + z(1);
X1(5,:)=X1(5,:)*0 + x(3);
Y1(5,:)=Y1(5,:)*0 + y(3);
Z1(5,:)=Z1(5,:)*0 + z(3);
h=surf(X1,Y1,Z1,'facecolor',colr,'edgecolor','none');
% camlight
% lighting phong