-
Notifications
You must be signed in to change notification settings - Fork 17
/
patchline.m
120 lines (116 loc) · 3.81 KB
/
patchline.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
function p = patchline(xs,ys,varargin)
% Plot lines as patches (efficiently)
%
% SYNTAX:
% patchline(xs,ys)
% patchline(xs,ys,zs,...)
% patchline(xs,ys,zs,'PropertyName',propertyvalue,...)
% p = patchline(...)
%
% PROPERTIES:
% Accepts all parameter-values accepted by PATCH.
%
% DESCRIPTION:
% p = patchline(xs,ys,zs,'PropertyName',propertyvalue,...)
% Takes a vector of x-values (xs) and a same-sized
% vector of y-values (ys). z-values (zs) are
% supported, but optional; if specified, zs must
% occupy the third input position. Takes all P-V
% pairs supported by PATCH. Returns in p the handle
% to the resulting patch object.
%
% NOTES:
% Note that we are drawing 0-thickness patches here,
% represented only by their edges. FACE PROPERTIES WILL
% NOT NOTICEABLY AFFECT THESE OBJECTS! (Modify the
% properties of the edges instead.)
%
% LINUX (UNIX) USERS: One test-user found that this code
% worked well on his Windows machine, but crashed his
% Linux box. We traced the problem to an openGL issue;
% the problem can be fixed by calling 'opengl software'
% in your <http://www.mathworks.com/help/techdoc/ref/startup.html startup.m>.
% (That command is valid at startup, but not at runtime,
% on a unix machine.)
%
% EXAMPLES:
%%% Example 1:
%
% n = 10;
% xs = rand(n,1);
% ys = rand(n,1);
% zs = rand(n,1)*3;
% plot3(xs,ys,zs,'r.')
% xlabel('x');ylabel('y');zlabel('z');
% p = patchline(xs,ys,zs,'linestyle','--','edgecolor','g',...
% 'linewidth',3,'edgealpha',0.2);
%
%%% Example 2: (Note "hold on" not necessary here!)
%
% t = 0:pi/64:4*pi;
% p(1) = patchline(t,sin(t),'edgecolor','b','linewidth',2,'edgealpha',0.5);
% p(2) = patchline(t,cos(t),'edgecolor','r','linewidth',2,'edgealpha',0.5);
% l = legend('sine(t)','cosine(t)');
% tmp = sort(findobj(l,'type','patch'));
% for ii = 1:numel(tmp)
% set(tmp(ii),'facecolor',get(p(ii),'edgecolor'),'facealpha',get(p(ii),'edgealpha'),'edgecolor','none')
% end
%
%%% Example 3 (requires Image Processing Toolbox):
%%% (NOTE that this is NOT the same as showing a transparent image on
%%% of the existing image. (That functionality is
%%% available using showMaskAsOverlay or imoverlay).
%%% Instead, patchline plots transparent lines over
%%% the image.)
%
% img = imread('rice.png');
% imshow(img)
% img = imtophat(img,strel('disk',15));
% grains = im2bw(img,graythresh(img));
% grains = bwareaopen(grains,10);
% edges = edge(grains,'canny');
% boundaries = bwboundaries(edges,'noholes');
% cmap = jet(numel(boundaries));
% ind = randperm(numel(boundaries));
% for ii = 1:numel(boundaries)
% patchline(boundaries{ii}(:,2),boundaries{ii}(:,1),...
% 'edgealpha',0.2,'edgecolor',cmap(ind(ii),:),'linewidth',3);
% end
%
% Written by Brett Shoelson, PhD
% brett.shoelson@mathworks.com
% 5/31/2012
%
% Revisions:
% 6/26 Improved rice.png example, modified FEX image.
% 11/30/30/2020 Fixed an issue on line 113 if varargin is empty.
%
% Copyright 2012 MathWorks, Inc.
%
% See also: patch, line, plot
[zs,PVs] = parseInputs(varargin{:});
if rem(numel(PVs),2) ~= 0
% Odd number of inputs!
error('patchline: Parameter-Values must be entered in valid pairs')
end
% Facecolor = 'k' is (essentially) ignored here, but syntactically necessary
if isempty(zs)
p = patch([xs(:);NaN],[ys(:);NaN],'k');
else
p = patch([xs(:);NaN],[ys(:);NaN],[zs(:);NaN],'k');
end
% Apply PV pairs
for ii = 1:2:numel(PVs)
set(p,PVs{ii},PVs{ii+1})
end
if nargout == 0
clear p
end
function [zs,PVs] = parseInputs(varargin)
if ~isempty(varargin) && isnumeric(varargin{1})
zs = varargin{1};
PVs = varargin(2:end);
else
PVs = varargin;
zs = [];
end