声振论坛

 找回密码
 我要加入

QQ登录

只需一步,快速开始

查看: 2720|回复: 3

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

[复制链接]
发表于 2005-9-5 23:08 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?我要加入

x
<P>function axdrag(action) <BR>%AXDRAG  Pan 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 = [0 zoomFactor*diff(xLim)] + 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 = [0 zoomFactor*diff(yLim)] + 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 = [0 zoomFactor*diff(xLim)] + xLim(1) + (1-zoomFactor)*diff(xLim)/2; <BR>     yLimNew = [0 zoomFactor*diff(yLim)] + 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',[2 1 76 16], ... <BR>               'Visible','off'); <BR>           text(0,1,str, ... <BR>               'Parent',helpTextAxis, ... <BR>               'VerticalAlignment','top', ... <BR>               'BackgroundColor',[1 1 0.8], ... <BR>               'FontName','courier', ... <BR>               'FontSize',6); <BR><BR>       end <BR>        <BR>  end <BR>   <BR>end <BR></P>

评分

1

查看全部评分

回复
分享到:

使用道具 举报

发表于 2006-3-5 21:20 | 显示全部楼层
这个函数太好了
发表于 2007-6-4 12:01 | 显示全部楼层
good
发表于 2007-6-4 17:01 | 显示全部楼层
修改了如下函数
set(gcbf,'WindowButtonMotionFcn','')
set(gcbf,'WindowButtonUpFcn','')
所以这个函数并不好
本来几句代码就可以实现ZOOM的功能
您需要登录后才可以回帖 登录 | 我要加入

本版积分规则

QQ|小黑屋|Archiver|手机版|联系我们|声振论坛

GMT+8, 2024-12-12 15:50 , Processed in 0.068975 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表