function [x, y] = symbsafe(symb, x0, y0, theSize, theColor, varargin) % symbsafe -- Plot symbols. % symbsafe('demo') demonstrates itself with stars % or polygons. % symbsafe(N) demonstrates itself with N stars. % symbsafe (no argument) updates the existing symbols in all % the axes of the current figure. The axes are left in % in "manual" mode. % symbsafe(symb, x0, y0, theSize, theColor, ...) draws symbols % as patchs, centered on (x0, y0), with theSize and theColor. % Additional name/value pairs are passed directly to the "patch" % routine. The symb sizes are given in the units of the y-axis. % The symb can be given as a two-column [x(:) y(:)] array of % values, or as a function statement (with or without arguments) % will be called with "[x, y] = eval(symb)". The (x, y) data % are expected to be suitable for input to the Matlab "patch" % routine. % [xx, yy] = symbsafe(...) returns the (x, y) coordinates of % the symbols, one per row, but does not plot them. % h = symbsafe(...) draws the stars and returns their handles. % % Also see: arrowsafe, star (internal), polygon (internal). % Copyright (C) 2000 Dr. Charles R. Denham, ZYDECO. % All Rights Reserved. % Disclosure without explicit written consent from the % copyright owner does not constitute publication. % Version of 13-Jan-2000 16:21:37. % Updated 15-Jan-2000 16:04:58. if nargin < 3, y0 = 0; end if nargin < 4, theSize = 1; end if nargin < 5, theColor = [0 0 0]; end % Update. if nargin < 1 oldGCA = gca; h = findobj(gcf, 'Type', 'patch', 'Tag', mfilename); if any(h) for i = 1:length(h) u = get(h(i), 'UserData'); [xx, yy] = feval(mfilename, u{:}); set(h(i), 'XData', xx, 'YData', yy) end end axes(oldGCA) return end % Demonstration. if nargin == 1 if isequal(symb, 'demo') symb = ceil(rand(1, 1)*20 + 5); end n = symb; if ischar(n), n = eval(n); end f = findobj('Type', 'figure', 'Name', [mfilename ' demo']); if any(f), figure(f(1)), else, figure, end delete(findobj(gcf, 'Type', 'patch', 'Tag', mfilename)) axis auto x0 = (rand(1, n) - 0.5) * n; y0 = rand(1, n) - 0.5; s = rand(1, n)/10; cmap = colormap; index = floor(rand(prod(size(x0)), 1) * size(cmap, 1)) + 1; c = cmap(index, :); fcn = 'star'; if rand(1, 1) > 0.5, fcn = 'polygon'; end k = ceil(rand(1, 1)*8) + 2; if rem(k, 4) == 0, k = k+1; end fcn = [fcn '(' int2str(k) ')']; feval(mfilename, fcn, x0, y0, s, c) set(gca, 'XLim', [-n n], 'Ylim', [-1 1]) set(gcf, 'Name', [mfilename ' demo']) if exist('zoomsafe', 'file') == 2 set(gcf, 'WindowButtonDownFcn', ... ['if zoomsafe(''down''), ' mfilename ', end']) end feval(mfilename) return end if ischar(symb) [xs, ys] = eval(symb); % "symb" may have arguments. else xs = symb(:, 1); ys = symb(:, 2); end if length(x0) == 1 x0 = x0 * ones(size(y0)); elseif length(y0) == 1 y0 = y0 * ones(size(x0)); end if length(theSize) == 1 theSize = theSize * ones(size(x0)); end if size(theColor, 1) == 1 theColor = ones(prod(size(x0)), 1) * theColor; end oldUnits = get(gca, 'Units'); set(gca, 'Units', 'pixels') thePosition = get(gca, 'Position'); set(gca, 'Units', oldUnits) theWidth = thePosition(3); theHeight = thePosition(4); axis('manual') dx = diff(get(gca, 'XLim')); dy = diff(get(gca, 'YLim')); dydx = dy / dx; % Not used. dxdp = dx / theWidth; % sci/pixel. dydp = dy / theHeight; % sci/pixel. scale = dxdp / dydp; f = zeros(1, 10); f(2) = 1; f = ifft(f)*length(f); f(2:2:end) = f(2:2:end) * 0.38; f(end+1) = f(1); xs = -imag(f); ys = real(f); if ischar(symb) [xs, ys] = eval(symb); else xs = symb(:, 1); ys = symb(:, 2); end h = zeros(size(x0)); for i = 1:prod(size(x0)) xsymb = xs * theSize(i) * scale + x0(i); ysymb = ys * theSize(i) + y0(i); csymb = theColor(i, :); usymb = {symb, x0(i), y0(i), theSize(i)}; if nargout == 2 x(i, 1:length(xsymb)) = xsymb; y(i, 1:length(ysymb)) = ysymb; else if length(varargin) > 1 h(i) = patch(xsymb, ysymb, csymb, ... 'Tag', mfilename, ... 'UserData', usymb); else h(i) = patch(xsymb, ysymb, csymb, ... 'Tag', mfilename, ... 'UserData', usymb); end end end set(gcf, 'ResizeFcn', mfilename) % ---------- star ---------- % function [x, y] = star(nPoints, theInnerRadius) % star -- Coordinates of a unit-star. % [x, y] = star(nPoints) returns the (x, y) coordinates % of a closed unit-star of nPoints (default = 5). If % only one output argument is provided, the result % is complex x+i*y. If none, a plot is made. The % points go counterclockwise, starting at (x = 0, y = 1). % [x, y] = star(nPoints, theInnerRadius) sets the radius % of the valley points to theInnerRadius [0 .. 1]. % Copyright (C) 2000 Dr. Charles R. Denham, ZYDECO. % All Rights Reserved. % Disclosure without explicit written consent from the % copyright owner does not constitute publication. % Version of 14-Jan-2000 12:48:41. % Updated 15-Jan-2000 15:43:44. if nargin < 1 & nargout < 1 nPoints = 'demo'; elseif nargin < 1 nPoints = 5; end if isequal(nPoints, 'demo') nPoints = 5; plot(star(nPoints)) set(gca, 'XLim', [-1 1], 'YLim', [-1 1]) axis equal set(gcf, 'Name', [mfilename ' demo']) figure(gcf) return end if nargin < 1, nPoints = 5; end if nargin < 2, theInnerRadius = 1; end if ischar(nPoints), nPoints = eval(nPoints); end zs = exp(sqrt(-1) * linspace(0, 2*pi, 2*nPoints+1)); zs(end) = zs(1); xs = -imag(zs); ys = real(zs); if nargin > 1 if ischar(theInnerRadius), theInnerRadius = eval(theInnerRadius); end theScale = theInnerRadius; else % Make valleys similar to Star of David. k = [1 5]; p = polyfit(xs(k), ys(k), 1); q = polyfit(xs(k+2), ys(k+2), 1); x0 = (q(2) - p(2)) / (p(1) - q(1)); y0 = polyval(p, x0); theScale = norm([x0 y0]); end xs(2:2:end) = xs(2:2:end) * theScale; ys(2:2:end) = ys(2:2:end) * theScale; if nargout > 1 x = xs; y = ys; elseif nargout > 0 x = xs + sqrt(-1)*ys; else plot(xs, ys) end % ---------- polygon ---------- % function [x, y] = polygon(nVertices) % polygon -- Unit-polygon coordinates. % polygon('demo') demonstrates itself. % [x, y] = polygon(nSides) returns the (x, y) % coordinates for a closed unit-polygon of % nVertices (default = 3). If only one output % argument is provided, the result is complex % x+i*y. If none, a plot is made. The points % go counterclockwise, starting at (x = 0, y = 1). % Copyright (C) 2000 Dr. Charles R. Denham, ZYDECO. % All Rights Reserved. % Disclosure without explicit written consent from the % copyright owner does not constitute publication. % Version of 15-Jan-2000 14:05:25. % Updated 15-Jan-2000 15:43:44. if nargin < 1 & nargout < 1 help(mfilename) nVertices = 'demo'; elseif nargin < 1 nVertices = 5; end if isequal(nVertices, 'demo') nVertices = 5; plot(polygon(nVertices)) set(gca, 'XLim', [-1 1], 'YLim', [-1 1]) axis equal set(gcf, 'Name', [mfilename ' demo']) figure(gcf) return end if ischar(nVertices), nVertices = eval(nVertices); end z = exp(sqrt(-1)*linspace(0, 2*pi, nVertices+1)); z(end) = z(1); xx = -imag(z); yy = real(z); if nargout > 1 x = xx; y = yy; elseif nargout > 0 x = xx + sqrt(-1)*yy; else plot(xx, yy) end