|
1 | 1 | %PLOT_ARROW Draw an arrow in 2D or 3D
|
2 | 2 | %
|
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. |
4 | 5 | %
|
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. |
6 | 10 | %
|
7 | 11 | % Options::
|
8 | 12 | % - 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. |
10 | 23 | %
|
11 | 24 | % See also ARROW3.
|
12 | 25 |
|
|
33 | 46 | %
|
34 | 47 | % https://github.com/petercorke/spatial-math
|
35 | 48 | 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 |
44 | 51 | 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 |
49 | 65 | 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'); |
52 | 67 |
|
53 |
| - arrow3(p1', p2', varargin{:}); |
| 68 | + arrow3(p1, p2, varargin{:}); |
0 commit comments