Skip to content

Commit

Permalink
Merge pull request #174 from oqilipo/master
Browse files Browse the repository at this point in the history
Fix of fillPolygon.m
  • Loading branch information
dlegland authored Oct 16, 2023
2 parents a061cde + b47164c commit 7eceb77
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 50 deletions.
9 changes: 9 additions & 0 deletions matGeom/meshes3d/meshBoundary.m
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
% CURVES = meshBoundary(V, F)
%
% Example
% % Example 1
% % create centered icosahedron
% [v, f] = createIcosahedron;
% v(:,3) = v(:,3) - mean(v(:,3));
Expand All @@ -18,6 +19,14 @@
% curves = meshBoundary(vc, fc);
% hold on; drawPolygon3d(curves{1}, 'linewidth', 2, 'color', 'b');
%
% % Example 2
% mesh = readMesh('mushroom.off');
% plane = createPlane([0 0 0.7], [-1 -2 3]);
% mesh = clipMeshVertices(mesh, plane, 'shape', 'plane');
% curves = meshBoundary(mesh);
% figure; drawMesh(mesh); axis equal; view(3);
% cellfun(@(x) drawPolygon3d(x, 'linewidth', 2, 'color', 'b'), curves)
%
% See also
% meshes3d, meshBoundaryEdgeIndices, meshBoundaryVertexIndices
%
Expand Down
78 changes: 28 additions & 50 deletions matGeom/polygons2d/fillPolygon.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,80 +11,58 @@
% fillPolygon(PX, PY);
% Specifies coordinates of the polygon in separate arrays.
%
%
% H = fillPolygon(...);
% Also returns a handle to the created patch
%
% Example
% oRectangle = [0 0;10 0;10 10;0 10];
% iRectangle = flipud(0.5*oRectangle+1);
% pol = {oRectangle, iRectangle};
% figure('color','w')
% fillPolygon(pol,'g')
% drawPolygon(pol,'r')
%
%
% See also
% polygons2d, drawCurve, drawPolygon

% ------
% Author: David Legland
% Author: David Legland, oqilipo
% E-mail: [email protected]
% Created: 2005-04-07
% Copyright 2005-2023 INRA - TPV URPOI - BIA IMASTE

% check input
% Check input
if isempty(varargin)
error('need to specify a polygon');
end

% case of a set of polygons stored in a cell array
var = varargin{1};
if iscell(var)
N = length(var);
h = zeros(N, 1);
for i = 1:N
% check for empty polygons
if ~isempty(var{i})
h(i) = fillPolygon(var{i}, varargin{2:end});
end
end

% setup output values
if nargout > 0
varargout{1} = h;
end
return;
error('Not enough input arguments.');
end

% Extract coordinates of polygon vertices
if size(var, 2) > 1
% first argument is a polygon array
px = var(:, 1);
py = var(:, 2);
varargin(1) = [];
else
% arguments 1 and 2 correspond to x and y coordinate respectively
if length(varargin) < 2
error('should specify either a N*2 array, or 2 N*1 vectors');
% Check if the polygon is given in two separate arrays.
if numel(varargin) > 1
if isnumeric(varargin{2})
varargin{2} = [varargin{1}, varargin{2}];
varargin(1)=[];
end

px = varargin{1};
py = varargin{2};
varargin(1:2) = [];
end

% Convert into a polyShape
polyShape = parsePolygon(varargin{1}, 'polyshape');
varargin(1)=[];

% Find position of breaks, and copies first point of each loop at the end
inds = find(isnan(px(:)));
i1 = [inds ; length(px)+1];
i0 = [1 ; inds+1];
px(i1, :) = px(i0, :);
py(i1, :) = py(i0, :);


% set default line format
% Set default color format if no color is given.
if isempty(varargin)
varargin = {'b'};
varargin = {'FaceColor', 'b'};
end

if ~mod(numel(varargin), 2) == 0
% Assume only the color was given.
varargin = ['FaceColor', varargin];
end

% fill the polygon with desired style
h = fill(px, py, varargin{:}, 'lineStyle', 'none');
% Fill the polygon with desired style.
h = plot(polyShape, varargin{:}, 'LineStyle', 'none');

% output
% Output
if nargout > 0
varargout{1} = h;
end

0 comments on commit 7eceb77

Please sign in to comment.