ik760218 发表于 2005-9-5 23:08

[转帖]一个非常爽的图形移动,放大缩小等功能的函数

<P>function axdrag(action) <BR>%AXDRAGPan and zoom with simple keystrokes <BR>%   Use this tool to move quickly around the data displayed in a 2-D plot. <BR>%   Make sure the figure has focus, and then press any of the following <BR>%   keys to zoom in or out. Clicking and dragging will pan the data. <BR>% <BR>%   Keys you can use are: <BR>%   z, Z: zoom in, zoom out, in both dimensions <BR>%   x, X: zoom in, zoom out, x dimension only <BR>%   y, Y: zoom in, zoom out, y dimension only <BR>%   arrow keys: pan the data <BR>%   a: axis auto <BR>%   n: axis normal <BR>%   e: axis equal <BR>%   g: toggle grid state <BR>%   spacebar: toggle axis tick display state <BR>%   h: help <BR>% <BR>%   Example <BR>%   c = pi*(1+sqrt(5))/2; <BR>%   x = 0:1000; <BR>%   r = 2.72378; <BR>%   z = cumsum(exp(i*(c*x.*x + r))); <BR>%   plot(real(z),imag(z)); <BR>%   axdrag <BR>%   % Now click, drag, and use special keys ... <BR><BR>%   Ned Gulley, March 2003 <BR><BR>persistent x0 dx <BR><BR>if nargin &lt; 1, <BR>   action = 'initialize'; <BR>end <BR><BR>% Use these variables to change the zoom and pan amounts <BR>zoomFactor = 0.9; <BR>panFactor = 0.02; <BR><BR>% Get rid of the help window if it's being displayed <BR>helpTextAxis = findobj(gcbf,'Type','axes','Tag','axdraghelpaxis'); <BR>if isempty(helpTextAxis) <BR>   helpWasOff = 1; <BR>else <BR>   helpWasOff = 0; <BR>   delete(helpTextAxis); <BR>end <BR><BR>switch action <BR>    <BR>case 'initialize' <BR>   set(gca,'ButtonDownFcn','axdrag start') <BR>   set(gcf,'KeyPressFcn','axdrag keypress') <BR>   set(gcf,'DoubleBuffer','on') <BR>    <BR>case 'start' <BR>   set(gcbf,'Units','pixel'); <BR>   set(gca,'Units','pixel'); <BR>   set(gcbf,'WindowButtonMotionFcn','axdrag move') <BR>   set(gcbf,'WindowButtonUpFcn','axdrag stop') <BR>   currentPoint = get(gcbf,'CurrentPoint'); <BR>   x0 = currentPoint; <BR>   axdrag move <BR><BR>case 'move' <BR>   currentPoint = get(gcbf,'CurrentPoint'); <BR>   dx = currentPoint - x0; <BR>   x0 = currentPoint; <BR>   ap = get(gca,'Position'); <BR>   xLim = get(gca,'XLim'); <BR>   yLim = get(gca,'YLim'); <BR>   set(gca,'XLim',xLim-(diff(xLim)*dx(1)/ap(3)), ... <BR>      'YLim',yLim-(diff(yLim)*dx(2)/ap(4))); <BR>    <BR>case 'stop' <BR>   set(gcbf,'WindowButtonMotionFcn','') <BR>   set(gcbf,'WindowButtonUpFcn','') <BR>   set(gcbf,'Units','normalized'); <BR>   set(gca,'Units','normalized'); <BR>    <BR>case 'keypress' <BR>   currChar = get(gcbf,'CurrentCharacter'); <BR>   if isempty(currChar) <BR>       return <BR>   end <BR>    <BR>   if currChar=='a', <BR>       axis auto <BR>      <BR>   elseif currChar=='e', <BR>       axis equal <BR>      <BR>   elseif currChar=='n', <BR>       axis normal <BR>      <BR>   elseif currChar=='g', <BR>       grid <BR>      <BR>   elseif currChar==28, <BR>       xLim=get(gca,'XLim'); <BR>       xLimNew = xLim + panFactor*diff(xLim); <BR>       set(gca,'XLim',xLimNew) <BR>      <BR>   elseif currChar==29, <BR>       xLim=get(gca,'XLim'); <BR>       xLimNew = xLim - panFactor*diff(xLim); <BR>       set(gca,'XLim',xLimNew) <BR>      <BR>   elseif currChar==30, <BR>       yLim=get(gca,'YLim'); <BR>       yLimNew = yLim - panFactor*diff(yLim); <BR>       set(gca,'YLim',yLimNew) <BR>      <BR>   elseif currChar==31, <BR>       yLim=get(gca,'YLim'); <BR>       yLimNew = yLim + panFactor*diff(yLim); <BR>       set(gca,'YLim',yLimNew) <BR>       <BR>   elseif abs(currChar)==32, <BR>   if isempty(get(gca,'XTick')), <BR>      set(gca,'XTickMode','auto','YTickMode','auto') <BR>   else <BR>      set(gca,'XTick',[],'YTick',[],'Box','on') <BR>   end <BR>            <BR>elseif (currChar=='x') | (currChar=='X'), <BR>   if currChar == 'X', <BR>      zoomFactor=1/zoomFactor; <BR>   end <BR>   xLim=get(gca,'XLim'); <BR>   xLimNew = + xLim(1) + (1-zoomFactor)*diff(xLim)/2; <BR>   set(gca,'XLim',xLimNew) <BR>      <BR>elseif (currChar=='y') | (currChar=='Y'), <BR>   if currChar == 'Y', <BR>      zoomFactor=1/zoomFactor; <BR>   end <BR>   yLim=get(gca,'YLim'); <BR>   yLimNew = + yLim(1) + (1-zoomFactor)*diff(yLim)/2; <BR>   set(gca,'YLim',yLimNew) <BR>      <BR>elseif (currChar=='z') | (currChar=='Z'), <BR>   if currChar == 'Z', <BR>      zoomFactor=1/zoomFactor; <BR>   end <BR>   xLim=get(gca,'XLim'); <BR>   yLim=get(gca,'YLim'); <BR><BR>xLimNew = + xLim(1) + (1-zoomFactor)*diff(xLim)/2; <BR>   yLimNew = + yLim(1) + (1-zoomFactor)*diff(yLim)/2; <BR><BR>set(gca,'XLim',xLimNew,'YLim',yLimNew) <BR>      <BR>   elseif currChar=='h', <BR>       if helpWasOff <BR>         str = { ... <BR>               ' ' <BR>               ' AXDRAG. Keys you can use are:' <BR>               ' ' <BR>               'z, Z: zoom in, zoom out, both dimensions ' <BR>               'x, X: zoom in, zoom out, x dimension only ' <BR>               'y, Y: zoom in, zoom out, y dimension only ' <BR>               'arrow keys: pan the data' <BR>               'a: axis auto' <BR>               'n: axis normal' <BR>               'e: axis equal' <BR>               'g: toggle grid state' <BR>               'spacebar: toggle axis tick display state' <BR>               'h: help' <BR>               ' ' <BR>               ' Press ''h'' again to dismiss this message' <BR>               ' ' ... <BR>         }; <BR>         helpTextAxis = axes( ... <BR>               'Tag','axdraghelpaxis', ... <BR>               'Units','characters', ... <BR>               'Position',, ... <BR>               'Visible','off'); <BR>         text(0,1,str, ... <BR>               'Parent',helpTextAxis, ... <BR>               'VerticalAlignment','top', ... <BR>               'BackgroundColor',, ... <BR>               'FontName','courier', ... <BR>               'FontSize',6); <BR><BR>       end <BR>      <BR>end <BR>   <BR>end <BR></P>

sffei 发表于 2006-3-5 21:20

这个函数太好了

ufo911 发表于 2007-6-4 12:01

good

shunfly 发表于 2007-6-4 17:01

修改了如下函数
set(gcbf,'WindowButtonMotionFcn','')
set(gcbf,'WindowButtonUpFcn','')
所以这个函数并不好
本来几句代码就可以实现ZOOM的功能
页: [1]
查看完整版本: [转帖]一个非常爽的图形移动,放大缩小等功能的函数