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 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
|
## Copyright (C) 2019 Juan Pablo Carbajal
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 3 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program. If not, see <http://www.gnu.org/licenses/>.
## -*- texinfo -*-
## @defun {} drawFilledPolygon (@var{p})
## @defunx {} drawFilledPolygon (@var{hax}, @var{p})
## @defunx {} clipPolygon (@dots{}, @var{prop}, @var{value}, @dots{})
## @defunx {@var{h} =} drawFilledPolygon (@dots{})
## Draw a filled polygon.
##
## Add a patch representing the polygon(s) @var{p} in the current axes.
##
## Multiple property-value pairs may be specified, but they must
## appear in pairs. These arguments are passed to the function @code{patch}.
##
## If the first argument @var{hax} is an axes handle, then plot into this
## axes, rather than the current axes returned by @code{gca}.
##
## If @var{p} is a cell, each element of the cell is processed in sequence.
##
## The optional return value @var{h} is a vector of graphics handles to the
## created patch objects.
##
## For example:
##
## Draw a polygon with default filling color and red edges.
##
## @example
## @group
## pol = [1 2; 7 4; 4 7; 1 2; NaN NaN; 2.5 3; 5.5 4; 4 5.5; 2.5 3];
## h = drawFilledPolygon (pol, 'edgecolor', 'r');
## @end group
## @end example
##
## @seealso{drawPolygon, polygon2patch, patch}
## @end defun
## Author: Juan Pablo Carbajal <ajuanpi+dev@gmail.com>
## Created: 2019-12-15
function h = drawFilledPolygon (px, varargin)
# Check input
if (nargin < 1)
print_usage ();
endif
# Check for empty polygons
if (isempty (px))
return
endif
# Store hold state
state = ishold (gca);
hold on;
# Extract handle of axis to draw on
ax = gca;
if (isAxisHandle (px))
ax = px;
px = varargin{1};
varargin(1) = [];
end
## Manage cell arrays of polygons
# Case of a set of polygons stored in a cell array
if (iscell (px))
np = numel (px);
h_ = zeros(1, np);
for i = 1:np
h_(np - i + 1) = drawFilledPolygon (px{i}, varargin{:});
endfor
else
# Check size vs number of arguments
if (size (px, 2) == 1)
# Case of polygon specified as two N-by-1 arrays with same length
if (nargin < 2 || nargin == 2 && ~isnumeric (varargin{1}))
error('Octave:invalid-input-arg', ...
['drawFilledPolygon: Should specify either a N-by-2 array,' ...
' or 2 N-by-1 vectors']);
endif
# Merge coordinates of polygon vertices
py = varargin{1};
varargin(1) = [];
if (length (py) ~= length (px))
error('Octave:invalid-input-arg', ...
['drawFilledPolygon: X and Y coordinate arrays should have' ...
' same lengths (%d,%d)'], length (px), length (py))
endif
px = [px py];
elseif (size (px, 2) > 2 || size (px, 2) < 1)
error ('Octave:invalid-input-arg', ...
'drawFilledPolygon: Should specify a N-by-2 array');
endif
# Set default format
fmtdef = {'facecolor', [0.7 0.7 0.7], ...
'edgecolor', 'k', ...
'linewidth', 2};
varargin = {fmtdef{:}, varargin{:}};
# if (isempty (varargin))
# varargin = formatdef;
# else # set missing with defaults
# [tfdef, idxarg] = ismember (formatdef(1:2:end), ...
# tolower (varargin(1:2:end)));
# if (any (tfdef))
# idxdef = find (tfdef);
# idxarg = idxarg(idxarg > 0);
# varargin(2 * idxarg) = formatdef(2 * idxdef);
# endif
# endif
pxpatch = polygon2patch (px);
h_ = patch (ax, pxpatch(:,1), pxpatch(:,2), varargin{:});
endif # whether input arg was a cell
if (~state)
hold off
endif
# Avoid returning argument if not required
if (nargout > 0)
h = h_;
endif
endfunction
%!demo
%! figure (1)
%! clf;
%! pol = [1 2; 7 4; 4 7; 1 2; NaN NaN; 2.5 3; 5.5 4; 4 5.5; 2.5 3];
%! subplot(131)
%! drawFilledPolygon(pol)
%! axis tight equal off
%! subplot(132)
%! drawFilledPolygon(pol, 'facecolor', 'c', 'linestyle', '--', 'edgecolor', 'r')
%! axis tight equal off
%! subplot(133)
%! R = createRotation (polygonCentroid (splitPolygons(pol){1}), pi/6);
%! pol2 = transformPoint (pol, R);
%! drawFilledPolygon(pol, 'linestyle', 'none')
%! drawFilledPolygon(gca, pol2, 'facealpha', 0.5)
%! axis tight equal off
%!demo
%! pol = [2 2; 6 2; 6 6; 2 6; 2 2; NaN NaN; 3 3; 3 5; 5 5; 5 3; 3 3];
%! n = 5;
%! alpha = linspace(0.1, 1, n);
%! theta = linspace(pi / 3, 0, n);
%!
%! cpol = cell(n, 1);
%! for i = 1:n
%! cpol{i} = transformPoint (pol, createRotation (theta(i)));
%! endfor
%! h = drawFilledPolygon(cpol, 'linestyle', 'none');
%! for i = 1:n-1
%! set(h(i), 'facealpha', alpha(i))
%! endfor
%! axis tight equal off
|