g = polygong(pointlist) or g = polygong(xlist,ylist) by Pruefert polygong returns a decomposed geometry matrix for a polygon object arguments can be (i) a pointlist of the form [x1 y1 x2 y2 ...] (ii) the koordinates of y and y component [x1 x2 ...] [y1 y2 ...] Examples (i) g = polygong([0 1 0],[0 0 1]) triagle (ii) g = polygong([0 0 1 0 0 1]) the same as (i) g = polygong(cos(linspace(0,2*pi,80)),sin(linspace(0,2*pi,80))) approximation on a circle by a "octadecagon" ;-) it is NOT the same as circleg NOTE the points of the polygon should be aranged in counter clockwise order
0001 function g = polygong(varargin) 0002 % g = polygong(pointlist) or g = polygong(xlist,ylist) by Pruefert 0003 % polygong returns a decomposed geometry matrix for a polygon object 0004 % arguments can be 0005 % (i) a pointlist of the form [x1 y1 x2 y2 ...] 0006 % (ii) the koordinates of y and y component [x1 x2 ...] [y1 y2 ...] 0007 % Examples 0008 % (i) g = polygong([0 1 0],[0 0 1]) triagle 0009 % (ii) g = polygong([0 0 1 0 0 1]) the same as (i) 0010 % g = polygong(cos(linspace(0,2*pi,80)),sin(linspace(0,2*pi,80))) 0011 % approximation on a circle by a "octadecagon" ;-) it is NOT the same as 0012 % circleg 0013 % NOTE the points of the polygon should be aranged in counter clockwise 0014 % order 0015 switch length(varargin) 0016 case 1 0017 pointlist = varargin{1}; 0018 if mod(length(pointlist),2) == 1, 0019 error('number of x and y coordinates must be the same') 0020 end 0021 case 2 0022 if ~(length(varargin{1})==length(varargin{2})), 0023 error('xlist and ylist must have the same length') 0024 end 0025 nop = length(varargin{1}); 0026 pointlist(1:2:2*nop) = varargin{1}; 0027 pointlist(2:2:2*nop) = varargin{2}; 0028 otherwise 0029 error('wrong number of input arguments') 0030 end 0031 0032 0033 % remove endpoint if it is also starting point 0034 if norm(pointlist(1:2)-pointlist(end-1:end))<1e-5, 0035 pointlist=pointlist(1:end-2); 0036 end 0037 nop = length(pointlist)/2; 0038 nokoords = length(pointlist); 0039 xpoints = pointlist(1:2:nokoords); 0040 ypoints = pointlist(2:2:nokoords); 0041 g = 2*ones(1,nop); 0042 g = [g;xpoints;xpoints(2:end),xpoints(1)]; 0043 g = [g;ypoints;ypoints(2:end),ypoints(1)]; 0044 g = [g;ones(1,nop);zeros(1,nop)];