Skip to content

Commit 9536dac

Browse files
committed
Add more robust argument processing for arrow3.
Add return of graphics handle TODO: should use tb_optparse to create arrow3's cryptic options.
1 parent 383c78d commit 9536dac

File tree

1 file changed

+33
-18
lines changed

1 file changed

+33
-18
lines changed

plot_arrow.m

+33-18
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,25 @@
11
%PLOT_ARROW Draw an arrow in 2D or 3D
22
%
3-
% PLOT_ARROW(P1, P2, OPTIONS) draws an arrow from P1 to P2 (2x1 or 3x1).
3+
% PLOT_ARROW(P1, P2, OPTIONS) draws an arrow from P1 to P2 (2x1 or 3x1). For 3D
4+
% case the arrow head is a cone.
45
%
5-
% PLOT_ARROW(P, OPTIONS) as above where the columns of P (2x2 or 3x2) define where P=[P1 P2].
6+
% PLOT_ARROW(P, OPTIONS) as above where the columns of P (2x2 or 3x2) define the
7+
% start and end points, ie. P=[P1 P2].
8+
%
9+
% H = PLOT_ARROW(...) as above but returns the graphics handle of the arrow.
610
%
711
% Options::
812
% - All options are passed through to arrow3.
9-
% - MATLAB colorspec such as 'r' or 'b--'
13+
% - MATLAB LineSpec such as 'r' or 'b--'
14+
%
15+
% Example::
16+
% plot_arrow([0 0 0]', [1 2 3]', 'r') % a red arrow
17+
% plot_arrow([0 0 0], [1 2 3], 'r--3', 4) % dashed red arrow big head
18+
%
19+
% Notes::
20+
% - Requires https://www.mathworks.com/matlabcentral/fileexchange/14056-arrow3
21+
% - ARROW3 attempts to preserve the appearance of existing axes. In
22+
% particular, ARROW3 will not change XYZLim, View, or CameraViewAngle.
1023
%
1124
% See also ARROW3.
1225

@@ -33,21 +46,23 @@
3346
%
3447
% https://github.com/petercorke/spatial-math
3548
function plot_arrow(p1, varargin)
36-
37-
if min(size(p1)) == 1
38-
% p1 is a vector
39-
p1 = p1(:);
40-
p2 = varargin{1};
41-
p2 = p2(:);
42-
assert(numrows(p1) == numrows(p2), 'SMTB:plot_arrow', 'P1 and P2 must be the same length');
43-
varargin = varargin{2:end};
49+
if isvec(p1, 2) || isvec(p1, 3)
50+
p1 = p1(:)'; % force row vector
4451
else
45-
% p1 is a 2-column matrix
46-
assert(numcols(p1) == 2, 'SMTB:plot_arrow', 'P1 must have 2 columns');
47-
p2 = p1(:,2);
48-
p1 = p1(:,1);
52+
error('SMTB:plot_arrow', 'P1 must have 2 or 3 elements')
53+
end
54+
55+
if nargin > 1
56+
p2 = varargin{1};
57+
if isnumeric(p2)
58+
if isvec(p2, 2) || isvec(p2, 3)
59+
p2 = p2(:)'; % force row vector
60+
varargin = varargin(2:end);
61+
else
62+
error('SMTB:plot_arrow', 'P2 must have 2 or 3 elements')
63+
end
64+
end
4965
end
50-
51-
assert(any(numrows(p1) == [2 3]), 'SMTB:plot_arrow', '2D or 3D points only');
66+
assert(numcols(p1) == numcols(p2), 'SMTB:plot_arrow', 'P1 and P2 must be the same length');
5267

53-
arrow3(p1', p2', varargin{:});
68+
arrow3(p1, p2, varargin{:});

0 commit comments

Comments
 (0)