飞道的博客

MATLAB使用鼠标交互案例记录

194人阅读  评论(0)

1、前记:

(1)鼠标交互生成三次样条曲线

(2)鼠标交互调整点的位置

2、代码:

(1)首先看一个指定点,并将点按交互序列链接起来的例子


  
  1. function draw_lines
  2. % Click the left mouse button to define a point
  3. % Drag the mouse to draw a line to the next point and
  4. % left click again
  5. % Right click the mouse to stop drawing
  6. %
  7. figure ('WindowButtonDownFcn',@wbdcb)
  8. ah = axes ('DrawMode','fast');
  9. clc
  10. axis ([ 1 10 1 10])
  11. function wbdcb(src,evnt)
  12. if strcmp(get(src,'SelectionType'),'normal')
  13. [x,y,str] = disp_point(ah);
  14. hl = line( 'XData',x, 'YData',y, 'Marker', '.');
  15. text(x,y,str, 'VerticalAlignment', 'bottom');drawnow
  16. set(src, 'WindowButtonMotionFcn',@wbmcb)
  17. elseif strcmp(get(src, 'SelectionType'), 'alt')
  18. set(src, 'WindowButtonMotionFcn', '')
  19. [x,y,str] = disp_point(ah);
  20. text(x,y,str, 'VerticalAlignment', 'bottom');drawnow
  21. end
  22. function wbmcb(src,evnt)
  23. [ xn, yn, str] = disp_point (ah);
  24. xdat = [x,xn];
  25. ydat = [y,yn];
  26. set(hl, 'XData',xdat, 'YData',ydat);
  27. end
  28. end
  29. function [x,y,str] = disp_point(ah)
  30. cp = get (ah,'CurrentPoint');
  31. x = cp( 1, 1);y = cp( 1, 2);
  32. str = [ '(',num2str(x, '%0.3g'), ', ',num2str(y, '%0.3g'), ')'];
  33. end
  34. end

效果:在figure中左键点击,留下该点位置,继续点击,生成的点序列进行线段连接,如此下去直到右键点击生成点和线段结束。(相当于指定点后生成的路径)

(2)在上一个基础上,生成点并生成点的三次样条曲线


  
  1. function draw_lines1_splinecurve
  2. % Click the left mouse button to define a point
  3. % Drag the mouse to draw a line to the next point and
  4. % left click again
  5. % Right click the mouse to stop drawing
  6. clc
  7. clear
  8. figure ('WindowButtonDownFcn',@wbdcb);
  9. k= 1;
  10. ah = axes( 'DrawMode', 'fast');
  11. clc
  12. grid on;
  13. axis ([- 20 20 - 20 20]);%open the file to save the coordinates into
  14. fileID = fopen( 'coord.txt', 'w');
  15. function wbdcb(src,evnt)
  16. if strcmp(get(src,'SelectionType'),'normal')
  17. [x,y,str] = disp_point(ah); % write the coordinates into the file
  18. A=[x,y];
  19. fprintf(fileID, '%6.2f %6.2f\n',A);
  20. hl = line( 'XData',x, 'YData',y, 'Marker', '.');
  21. text(x,y,str, 'VerticalAlignment', 'bottom');drawnow;
  22. set(src, 'WindowButtonMotionFcn',@wbmcb);
  23. elseif strcmp(get(src, 'SelectionType'), 'alt')
  24. set(src, 'WindowButtonMotionFcn', '');
  25. [x,y,str] = disp_point(ah); % write the coordinates into the file
  26. A=[x,y];
  27. fprintf(fileID, '%6.2f %6.2f\n',A);
  28. text(x,y,str, 'VerticalAlignment', 'bottom');drawnow;
  29. fclose(fileID);
  30. end
  31. A1=importdata( 'coord.txt', '%f');
  32. h = findobj( 'type', 'line', 'tag', 'ax1');
  33. delete(h);
  34. if size(A1, 1)> 2
  35. xx=zeros(size(A1, 1), 1);
  36. yy=zeros(size(A1, 1), 1);
  37. for i= 1:size(A1, 1)
  38. xy=str2num(A1 {i, 1});
  39. xx(i)=xy( 1);
  40. yy(i)=xy( 2);
  41. end
  42. hold on
  43. fnplt(cscvn([xx ';yy']))
  44. end
  45. function wbmcb(src,evnt)
  46. [ xn, yn, str] = disp_point (ah);
  47. xdat = [x,xn];
  48. ydat = [y,yn];
  49. set(hl, 'XData',xdat, 'YData',ydat);
  50. end
  51. end
  52. function [x,y,str] = disp_point(ah)
  53. % P1 = impoint (gca,[]);
  54. cp = get(ah, 'CurrentPoint');
  55. x = cp( 1, 1);y = cp( 1, 2);
  56. str = [ '(',num2str(x, '%0.3g'), ', ',num2str(y, '%0.3g'), ')'];
  57. end
  58. end

效果: 各点之间生成光滑三次样条曲线

(3)生成点再次修改点位


  
  1. % Plot a line and points
  2. figure
  3. plot(1:10,1:10,'-o','buttondownfcn',{@Mouse_Callback,'down'});
  4. % Callback function for each point
  5. function Mouse_Callback(hObj,~,action)
  6. persistent curobj xdata ydata ind
  7. pos = get(gca, 'CurrentPoint');
  8. switch action
  9. case 'down'
  10. curobj = hObj;
  11. xdata = get(hObj, 'xdata');
  12. ydata = get(hObj, 'ydata');
  13. [ ~,ind] = min(sum((xdata-pos( 1)).^ 2+(ydata-pos( 3)).^ 2, 1));
  14. set(gcf,...
  15. 'WindowButtonMotionFcn', {@Mouse_Callback, 'move'},...
  16. 'WindowButtonUpFcn', {@Mouse_Callback, 'up'});
  17. case 'move'
  18. % vertical move
  19. ydata( ind) = pos( 3);
  20. xdata(ind) = pos( 2);
  21. set(curobj, 'xdata',xdata)
  22. set(curobj, 'ydata',ydata)
  23. case 'up'
  24. set(gcf,...
  25. 'WindowButtonMotionFcn', '',...
  26. 'WindowButtonUpFcn', '');
  27. end
  28. end

 


转载:https://blog.csdn.net/weixin_39090239/article/details/116702976
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场