forked from petercorke/spatialmath-matlab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot_point.m
145 lines (133 loc) · 4.52 KB
/
plot_point.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
%PLOT_POINT Draw a point
%
% PLOT_POINT(P, OPTIONS) adds point markers and optional annotation text
% to the current plot, where P (2xN) and each column is a point coordinate.
%
% H = PLOT_POINT(...) as above but return handles to the points.
%
% Options::
% 'textcolor', colspec Specify color of text
% 'textsize', size Specify size of text
% 'bold' Text in bold font.
% 'printf', {fmt, data} Label points according to printf format
% string and corresponding element of data
% 'sequence' Label points sequentially
% 'label',L Label for point
%
% Additional options to PLOT can be used:
% - standard MATLAB LineStyle such as 'r' or 'b---'
% - any MATLAB LineProperty options can be given such as 'LineWidth', 2.
%
% Notes::
% - The point(s) and annotations are added to the current plot.
% - Points can be drawn in 3D axes but will always lie in the
% xy-plane.
% - Handles are to the points but not the annotations.
%
% Examples::
% Simple point plot with two markers
% P = rand(2,4);
% plot_point(P);
%
% Plot points with markers
% plot_point(P, '*');
%
% Plot points with solid blue circular markers
% plot_point(P, 'bo', 'MarkerFaceColor', 'b');
%
% Plot points with square markers and labelled 1 to 4
% plot_point(P, 'sequence', 's');
%
% Plot points with circles and labelled P1, P2, P4 and P8
% data = [1 2 4 8];
% plot_point(P, 'printf', {' P%d', data}, 'o');
%
%
% See also PLOT_SPHERE, PLOT, TEXT.
% Copyright (C) 1993-2019 Peter I. Corke
%
% This file is part of The Spatial Math Toolbox for MATLAB (SMTB).
%
% Permission is hereby granted, free of charge, to any person obtaining a copy
% of this software and associated documentation files (the "Software"), to deal
% in the Software without restriction, including without limitation the rights
% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
% of the Software, and to permit persons to whom the Software is furnished to do
% so, subject to the following conditions:
%
% The above copyright notice and this permission notice shall be included in all
% copies or substantial portions of the Software.
%
% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
% FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
% COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
% IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
% CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
%
% https://github.com/petercorke/spatial-math
function ho = plot_point(p, varargin)
opt.textcolor = 'k';
opt.textsize = 12;
opt.printf = [];
opt.sequence = false;
opt.bold = false;
opt.label = [];
opt.solid = false;
[opt,arglist,ls] = tb_optparse(opt, varargin);
% label is a cell array, one per point (column)
if ~isempty(opt.label) && numcols(p) == 1
% if single point, convert single label to a cell array
opt.label = {opt.label};
end
% default marker style
if isempty(ls)
ls = {'bs'}; % blue square
end
% add stuff to pull .u and .v out of a vector of objects
if ~isnumeric(p) && any(strcmp('u_', properties(p)))
% p is an object with u_ and v_ properties
p = [[p.u_]; [p.v_]];
end
if numrows(p) == 3
error('p must have 2 rows, only supports 2D plotting')
end
holdon = ishold();
hold on
h = zeros(1,numcols(p));
for i=1:numcols(p)
if opt.solid
arglist = [ 'MarkerFaceColor', ls{1}(1), arglist];
end
h(i) = plot(p(1,i), p(2,i), ls{:}, arglist{:});
if opt.sequence
show(p(:,i), '%d', i, opt);
end
if ~isempty(opt.label)
show(p(:,i), opt.label{i}, [], opt);
elseif ~isempty(opt.printf)
show(p(:,i), opt.printf{1}, opt.printf{2}(i), opt);
end
end
if ~holdon
hold off
end
figure(gcf)
if nargout > 0
ho = h;
end
end
function show(p, fmt, val, opt)
if opt.bold
fw = 'bold';
else
fw = 'normal';
end
text(p(1), p(2), sprintf([' ' fmt], val), ...
'HorizontalAlignment', 'left', ...
'VerticalAlignment', 'middle', ...
'FontUnits', 'pixels', ...
'FontSize', opt.textsize, ...
'FontWeight', fw, ...
'Color', opt.textcolor);
end