📄 arrow3.m
字号:
function hn=arrow3(p1,p2,s,w,h,ip,alpha,beta)
% ARROW3
% ARROW3(P1,P2) will draw vector lines (2D/3D) from P1 to P2 with
% arrowheads, where P1 and P2 can be either nx2 matrices (for 2D),
% or nx3 matrices (for 3D).
%
% ARROW3(P1,P2,S,W,H,IP,ALPHA,BETA) can be used to specify properties
% of the line and arrowhead. S is a character string made with one
% element from any or all of the following 3 columns:
%
% Color Switches LineStyle LineWidth
% ------------------ ------------------- --------------------
% k black (default) - solid (default) 0.5 points (default)
% y yellow : dotted 0 no lines
% m magenta -. dashdot / LineWidthOrder
% c cyan -- dashed
% r red * LineStyleOrder
% g green
% b blue
% w white __________ __
% a apple green ^ |
% d dark gray / \ |
% e evergreen / \ |
% f fuchsia / \ |
% h honey / \ Height
% i indigo / \ |
% j jade / \ |
% l lilac / \ |
% n nutbrown /______ ______\ __|__
% p pink | | | |
% q kumquat |---- Width ----|
% s sky blue | | | |
% t tan | |
% u umber | |
% v violet | |
% z zinc -->| |<--LineWidth
% x random named color | |
% o ColorOrder
% | magnitude
%
% The components of S may be specified in any order. Invalid
% characters in S will be ignored and replaced by default settings.
%
% Prefixing the color code with '_' produces a darker shade, e.g.
% '_t' is dark tan; prefixing the color code with '^' produces a
% lighter shade, e.g. '^q' is light kumquat. The relative
% brightness of light and dark color shades is controled by the
% scalar parameter BETA. Color code prefixes do not affect the
% basic colors (kymcrgbw) or the special color switches (xo|).
%
% ColorOrder may be achieved in two fashions: The user may either
% set the ColorOrder property (using RGB triples) or define the
% global variable ColorOrder (using a string of valid color codes).
% If the color switch is specified with 'o', and the global variable
% ColorOrder is a string of color codes (color switches less 'xo|',
% optionally prefixed with '_' or '^'), then the ColorOrder property
% will be set to the sequence of colors indicated by the ColorOrder
% variable. If the color switch is specified with 'o', and the
% global variable ColorOrder is empty or invalid, then the current
% ColorOrder property will be used. Note that the ColorOrder
% variable takes precedence over the ColorOrder property.
%
% The magnitude color switch is used to visualize vector magnitudes
% in conjunction with a colorbar. If the color switch is specified
% with '|', colors are linearly interpolated from the current ColorMap
% according to the length of the associated line. This option sets
% CLim to [MinM,MaxM], where MinM and MaxM are the minimum and maximum
% magnitudes, respectively.
%
% The current LineStyleOrder property will be used if LineStyle is
% specified with '*'. MATLAB cycles through the line styles defined
% by the LineStyleOrder property only after using all colors defined
% by the ColorOrder property. If however, the global variable
% LineWidthOrder is defined, and LineWidth is specified with '/',
% then each line will be drawn with sequential color, linestyle, and
% linewidth.
%
% W is a vector of arrowhead widths. For linear plots with equal
% axes, the units of W (default = 1/72 of the PlotBox diagonal) are
% the same as those of the coordinate data (P1,P2). For linear plots
% with unequal axes, the units of W are scaled to fit as if the axes
% were equal. For log plots, the units of W (default = 1) are 1/72
% of the PositionRectangle diagonal.
%
% H (default = 3W) is a vector of arrowhead heights. If vector IP is
% neither empty nor negative, initial point markers will be plotted
% with diameter IP; for default diameter W, use IP = 0. The units of
% W, H, and IP are absolute for linear plots and relative to the
% PositionRectangle for log plots.
%
% ALPHA (default = 1) is a vector of FaceAlpha (MATLAB 6+) values
% ranging between 0 (clear) and 1 (opaque). FaceAlpha is a surface
% (arrowhead and initial point marker) property and does not affect
% lines. FaceAlpha is not supported for 2D rendering.
%
% BETA (default = 0.4) is a scalar that controls the relative
% brightness of light and dark color shades, ranging between 0 (no
% contrast) and 1 (maximum contrast).
%
% Plotting lines with a single color, linestyle, and linewidth is
% faster than plotting lines with multiple colors and/or linestyles.
% Plotting lines with multiple linewidths is slower still. ARROW3
% chooses renderers that produce the best screen images; exported
% or printed plots may benifit from different choices.
%
% HN = ARROW3(P1,P2,...) returns a vector of handles to line and
% surface objects created by ARROW3.
%
% ARROW3 COLORS will plot a table of named colors with default
% brightness. ARROW3('colors',BETA) will plot a table of named
% colors with brightness BETA.
%
% If a particular aspect ratio is required, use DASPECT, PBASPECT,
% AXIS, or XYZLIM commands before calling ARROW3. Changing limits
% or aspect ratios after calling ARROW3 may alter the appearance
% of arrowheads and initial point markers. ARROW3 sets XYZCLimMode
% to manual for all plots, sets DataAspectRatioMode to manual for
% linear plots, and sets PlotBoxAspectRatioMode to manual for log
% plots and 3D plots.
%
% ARROW3 UPDATE will restore the the appearance of arrowheads and
% initial point markers that have become corrupted by changes to
% limits or aspect ratios. ARROW3('update',SF) will redraw initial
% point markers and arrowheads with scale factor SF. If SF has one
% element, SF scales W, H and IP. If SF has two elements, SF(1)
% scales W and IP, and SF(2) scales H. If SF has three elements,
% SF(1) scales W, SF(2) scales H, and SF(3) scales IP.
%
% ARROW3 UPDATE COLORS will update the magnitude coloring of
% arrowheads, initial point markers, and lines to conform to the
% current colormap.
%
% HN = ARROW3('update',...) returns a vector of handles to updated
% objects.
%
% Usage Examples:
%
% % 2D vectors
% Arrow3([0 0],[1 3])
% Arrow3([0 0],[1 2],'-.e')
% Arrow3([0 0],[10 10],'--x2',1)
% Arrow3(zeros(10,2),50*rand(10,2),'x',1,3)
% Arrow3(zeros(10,2),[10*rand(10,1),500*rand(10,1)],'u')
% Arrow3(10*rand(10,2),50*rand(10,2),'x',1,[],1)
%
% % 3D vectors
% Arrow3([0 0 0],[1 1 1])
% Arrow3(zeros(20,3),50*rand(20,3),'--x1.5',2)
% Arrow3(zeros(100,3),50*rand(100,3),'x',1,3)
% Arrow3(zeros(10,3),[10*rand(10,1),500*rand(10,1),50*rand(10,1)],'a')
% Arrow3(10*rand(10,3),50*rand(10,3),'x',[],[],0)
%
% % Just for fun
% Arrow3(zeros(100,3),50*rand(100,3),'x',10,3,[],0.95)
% light('Position',[-10 -10 -10],'Style','local')
% light('Position',[60,60,60]), lighting gouraud
%
% % ColorOrder variable, color code prefixes, and Beta
% global ColorOrder, ColorOrder='^ui^e_hq^v';
% theta=[0:pi/22:pi/2]';
% Arrow3(zeros(12,2),[cos(theta),sin(theta)],'1.5o',0.1,[],[],[],0.5)
%
% % ColorOrder property, LineStyleOrder, and LineWidthOrder
% global ColorOrder, ColorOrder=[];
% set(gca,'ColorOrder',[1,0,0;0,0,1;0.25,0.75,0.25;0,0,0])
% set(gca,'LineStyleOrder',{'-','--','-.',':'})
% global LineWidthOrder, LineWidthOrder=[1,2,4,8];
% w=[5,10,15,20]; h=[20,40,30,40];
% Arrow3(zeros(4,2),[10*rand(4,1),500*rand(4,1)],'o*/',w,h,10)
%
% % Magnitude coloring
% colormap spring
% Arrow3(20*randn(20,3),50*randn(20,3),'|',[],[],0)
% set(gca,'color',0.7*[1,1,1])
% set(gcf,'color',0.5*[1,1,1]), grid on, colorbar
% pause % change the colormap and update colors
% colormap hot
% Arrow3('update','colors')
%
% % LogLog plot
% set(gca,'xscale','log','yscale','log');
% axis([1e2,1e8,1e-2,1e-1]); hold on
% p1=repmat([1e3,2e-2],15,1);
% q1=[1e7,1e6,1e5,1e4,1e3,1e7,1e7,1e7,1e7,1e7,1e7,1e6,1e5,1e4,1e3];
% q2=1e-2*[9,9,9,9,9,7,5,4,3,2,1,1,1,1,1]; p2=[q1',q2'];
% global ColorOrder, ColorOrder=[];
% set(gca,'ColorOrder',rand(15,3))
% Arrow3(p1,p2,'o'), grid on, hold off
%
% % SemiLogX plot
% set(gca,'xscale','log','yscale','linear');
% axis([1e2,1e8,1e-2,1e-1]); hold on
% p1=repmat([1e3,0.05],15,1);
% q1=[1e7,1e6,1e5,1e4,1e3,1e7,1e7,1e7,1e7,1e7,1e7,1e6,1e5,1e4,1e3];
% q2=1e-2*[9,9,9,9,9,7,5,4,3,2,1,1,1,1,1]; p2=[q1',q2'];
% Arrow3(p1,p2,'x'), grid on, hold off
%
% % SemiLogY plot
% set(gca,'xscale','linear','yscale','log');
% axis([2,8,1e-2,1e-1]); hold on
% p1=repmat([3,2e-2],17,1);
% q1=[7,6,5,4,3,7,7,7,7,7,7,7,7,6,5,4,3];
% q2=1e-2*[9,9,9,9,9,8,7,6,5,4,3,2,1,1,1,1,1]; p2=[q1',q2'];
% set(gca,'LineStyleOrder',{'-','--','-.',':'})
% Arrow3(p1,p2,'*',1,[],0), grid on, hold off
%
% % Color tables
% Arrow3('colors') % default color table
% Arrow3('colors',0.3) % low contrast color table
% Arrow3('colors',0.5) % high contrast color table
%
% % Update initial point markers and arrowheads
% Arrow3('update') % redraw same size
% Arrow3('update',2) % redraw double size
% Arrow3('update',0.5) % redraw half size
% Arrow3('update',[0.5,2,1]) % redraw W half size,
% % H double size, and
% % IP same size
% Copyright(c)2002,2003 Version 4.58
% Jeff Chang <cpmame@hotmail.com>
% Tom Davis <tdavis@eng.usf.edu>
% Revision History:
%
% 12/17/03 - Semilog examples, CAXIS support, magnitude
% coloring, and color updating; use CData instead
% of FaceColor; minor bug fixes (TD)
% 07/17/03 - Changed 2D rendering from OpenGL to ZBuffer;
% defined HN for COLORS and UPDATE options (TD)
% 02/27/03 - Replaced calls to RANDPERM, VIEW, REPMAT, SPHERE,
% and CYLINDER; added ZBuffer for log plots, RESET
% for CLA and CLF, and ABS for W and H (TD)
% 02/01/03 - Added UPDATE scale factor and matlab version
% checking, replaced call to CROSS (TD)
% 12/26/02 - Added UserData and UPDATE option (TD)
% 11/16/02 - Added more named colors, color code prefix,
% global ColorOrder, ALPHA , and BETA (TD)
% 10/12/02 - Added global LineWidthOrder,
% vectorized W, H and IP (TD)
% 10/05/02 - Changed CLF to CLA for subplot support,
% added ColorOrder and LineStyleOrder support (TD)
% 04/27/02 - Minor log plot revisions (TD)
% 03/26/02 - Added log plot support (TD)
% 03/24/02 - Adaptive grid spacing control to trade off
% appearance vs. speed based on size of matrix (JC)
% 03/16/02 - Added "axis tight" for improved appearance (JC)
% 03/12/02 - Added initial point marker (TD)
% 03/03/02 - Added aspect ratio support (TD)
% 03/02/02 - Enchance program's user friendliness (JC)
% (lump Color, LineStyle, and LineWidth together)
% 03/01/02 - Replaced call to ROTATE (TD)
% 02/28/02 - Modified line plotting,
% added linewidth and linestyle (TD)
% 02/27/02 - Minor enhancements on 3D appearance (JC)
% 02/26/02 - Minor enhancements for speed (TD&JC)
% 02/26/02 - Optimise PLOT3 and SURF for speed (TD)
% 02/25/02 - Return handler, error handling, color effect,
% generalize for 2D/3D vectors (JC)
% 02/24/02 - Optimise PLOT3 and SURF for speed (TD)
% 02/23/02 - First release (JC&TD)
%==========================================================================
% Error Checking
oldver=0; v=version; if v(1)<'6', oldver=1; end % matlab version
if nargin<8 | isempty(beta), beta=0.4; end
beta=abs(beta(1)); if nargout, hn=[]; end
if strcmpi(p1,'colors') % plot color table
if nargin>1, beta=abs(p2(1)); end
LocalColorTable(1,beta); return
end
fig=gcf; ax=gca;
if strcmpi(p1,'update'), ud=get(ax,'UserData'); % update
if size(ud,2)<13, error('Invalid UserData'), end
set(ax,'UserData',[]); sf=[1,1,1]; flag=0;
if nargin>1
if strcmp(p2,'colors'), flag=1; % update colors
else % update surfaces
sf=p2(1)*sf; n=length(p2(:));
if n>1, sf(2)=p2(2); if n>2, sf(3)=p2(3); end, end
end
end
H=LocalUpdate(fig,ax,ud,sf,flag); if nargout, hn=H; end, return
end
InputError=['Invalid input, type HELP ',upper(mfilename),...
' for usage examples'];
if nargin<2, error(InputError), end
[r1,c1]=size(p1); [r2,c2]=size(p2); n=r1; Zeros=zeros(n,1);
if c1<2 | c1>3, error(InputError), end
if r1~=r2, error('P1 and P2 must have same number of rows'), end
if c1~=c2, error('P1 and P2 must have same number of columns'), end
if c1==2, p1=[p1,Zeros]; p2=[p2,Zeros];
elseif ~any([p1(:,3);p2(:,3)]), c1=2; end
L=get(ax,'LineStyleOrder'); C=get(ax,'ColorOrder');
ST=get(ax,'DefaultSurfaceTag'); LT=get(ax,'DefaultLineTag');
EC=get(ax,'DefaultSurfaceEdgeColor');
if strcmp(get(ax,'nextplot'),'add') & strcmp(get(fig,'nextplot'),'add')
Xr=get(ax,'xlim'); Yr=get(ax,'ylim'); Zr=get(ax,'zlim');
set(ax,'XLimMode','auto','YLimMode','auto','ZLimMode','auto',...
'CLim',get(ax,'CLim'));
xs=strcmp(get(ax,'xscale'),'log');
ys=strcmp(get(ax,'yscale'),'log');
zs=strcmp(get(ax,'zscale'),'log');
if zs, error('Z log scale not supported'), end
xys=xs+ys; restore=1;
if xys & any([p1(:,3);p2(:,3)])
error('3D log plot not supported')
end
else, restore=0; cla reset; xys=0; set(fig,'nextplot','add');
if c1==2, azel=[0,90]; else, azel=[-37.5,30]; end
set(ax,'UserData',[],'nextplot','add','View',azel);
end
%==========================================================================
% Style Control
[vc,cn]=LocalColorTable(0); prefix=''; OneColor=0;
if nargin<3, [c,ls,lw]=LocalValidateCLSW; % default Color, LineStyle/Width
else,
[c,ls,lw]=LocalValidateCLSW(s);
if length(c)>1, if sum('_^'==c(1)), prefix=c(1); end, c=c(2); end
if c=='x' % random named color (less white)
[ignore,i]=sort(rand(1,23)); c=cn(i,:);
elseif c=='o', global ColorOrder % ColorOrder
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -