来源:https://blog.csdn.net/weixin_39090239/article/details/80586930
前记:
人机交互的方式--键盘开关、鼠标、触摸屏、体感传感器(Kinect、leap motion)等的使用大大促进人与机器的交流过程。
这里记录鼠标事件与MATLAB的结合,为后期机器人仿真控制打下基础---如鼠标直接拖动机器人运动(像很多机器人仿真软件里的作用一样,如Robotstudio、soldworks、proe\core、Adams等)。
1、鼠标画矩形
代码功能:运行代码在出现的figure窗口,点击鼠标画出矩形
- k = waitforbuttonpress;
- point1 = get(gca,'CurrentPoint'); % button down detected
- finalRect = rbbox; % return figure units
- point2 = get(gca,'CurrentPoint'); % button up detected
- point1 = point1(1,1:2); % extract x and y
- point2 = point2(1,1:2);
- p1 = min(point1,point2); % calculate locations
- offset = abs(point1-point2); % and dimensions
- x = [p1(1) p1(1)+offset(1) p1(1)+offset(1) p1(1) p1(1)];
- y = [p1(2) p1(2) p1(2)+offset(2) p1(2)+offset(2) p1(2)];
- hold on
- axis manual
- plot(x,y)
2、鼠标拖动直线
- function main
- f =figure;
- aH=axes('Xlim',[0 1],'Ylim',[0 1]);
- h=line([0.5 0.5],[0 1],...
- 'color','red',...
- 'linewidth',4,...
- 'ButtonDownFcn',@startDragFcn);
- set(f,'WindowButtonUpFcn',@startDragFcn);
- function startDragFcn(varargin)
- set(f,'WindowButtonMotionFcn',@draggingFcn);
- end
- function draggingFcn(varargin)
- pt=get(aH,'CurrentPoint');
- set(h,'Xdata',pt(1)*[1 1]);
- end
- function stopDragFcn(varargin)
- set(f,'WindowButtonMotionFcn','');
- end
- end
3、显示坐标
- function ff
- x = 5:30;
- y = x.^2-40.*x+400;
- plot(x,y);
- axis([5,30,-50,250]);
- tb = text;
- set(gcf, 'WindowButtonMotionFcn', @callback);
- function callback(hObject, event)
- loc = get(gca, 'CurrentPoint');
- loc = loc([1 3]);
- set(tb, 'string', num2str(loc), 'position', loc);
- end
- end
4、画轨迹并保存点坐标
- function MouseDraw(action)
- % MouseDraw 本例展示如何以Handle Graphics来设定滑鼠事件
- % (MouseDraw Events)的反应指令(Callbacks)
- % 本程序在鼠标移动非常快时,不会造成画“断线”
- % global不能传矩阵
- global InitialX InitialY FigHandle
- if nargin == 0, action = 'start';
- end
- switch(action)
- %%开启图形视窗
- case 'start'
- FigHandle = figure('WindowButtonDownFcn','MouseDraw down');
- axis([-600 600 -600 600]); % 设定图轴范围
- % axis off;
- grid on;
- box on; % 将图轴加上图框
- title('手写体输入窗');
- % % fprintf('start');
- %%设定滑鼠按钮被按下时的反应指令为「MouseDraw down」
- % set(gcf, 'WindowButtonDownFcn', 'MouseDraw down');
- dlmwrite('IXT.txt', 10, 'delimiter', '\t', 'precision', 10);
- dlmwrite('IZT.txt', 10, 'delimiter', '\t', 'precision', 10);
- %%滑鼠按钮被按下时的反应指令
- case 'down'
- if strcmp(get(FigHandle, 'SelectionType'), 'normal') %如果是左键
- set(FigHandle,'pointer','hand');
- CurPiont = get(gca, 'CurrentPoint');
- InitialX = CurPiont(1,1);
- InitialY = CurPiont(1,2);
- dlmwrite('IXT.txt', InitialX, '-append', 'delimiter', '\t', 'precision', 10);
- dlmwrite('IZT.txt', InitialY, '-append', 'delimiter', '\t', 'precision', 10);
- % 列印「MouseDraw down!」讯息
- % % fprintf('MouseDraw down!\n');
- % 设定滑鼠移动时的反应指令为「MouseDraw move」
- set(gcf, 'WindowButtonMotionFcn', 'MouseDraw move');
- set(gcf, 'WindowButtonUpFcn', 'MouseDraw up');
- elseif strcmp(get(FigHandle, 'SelectionType'), 'alt') % 如果是右键
- set(FigHandle, 'Pointer', 'arrow');
- set( FigHandle, 'WindowButtonMotionFcn', '')
- set(FigHandle, 'WindowButtonUpFcn', '')
- fprintf('MouseDraw right button down!\n');
- ImageX = importdata('IXT.txt');
- ImageY = importdata('IZT.txt');
- InputImage = ones(imSize);
- roundX = round(ImageX);
- roundY = round(ImageY);
- for k = 1:size(ImageX,1)
- if 0<roundX(k) && roundX(k)<imSize && 0<roundY(k) && roundY(k)<imSize
- InputImage(roundX(k)-1:roundX(k)+2, roundY(k)-1:roundY(k)+2) = 0;
- end
- end
- InputImage = imrotate(InputImage,180); % 图像旋转
- figure(2);
- imshow(InputImage);
- end
- %%滑鼠移动时的反应指令
- case 'move'
- CurPiont = get(gca, 'CurrentPoint');
- X = CurPiont(1,1);
- Y = CurPiont(1,2);
-
% 当鼠标移动较快时,不会出现离散点。
-
% 利用y=kx+b直线方程实现。
- x_gap = 1; % 定义x方向增量
- y_gap = 1; % 定义y方向增量
- if X > InitialX
- step_x = x_gap;
- else
- step_x = -x_gap;
- end
- if Y > InitialY
- step_y = y_gap;
- else
- step_y = -y_gap;
- end
- % 定义x,y的变化范围和步长
- if abs(X-InitialX) < 0.1 % 线平行于y轴,即斜率不存在时
- iy = InitialY:step_y:Y;
- ix = X.*ones(1,size(iy,2));
- else
- ix = InitialX:step_x:X ; % 定义x的变化范围和步长
- % 当斜率存在,即k = (Y-InitialY)/(X-InitialX) ~= 0
- iy = (Y-InitialY)/(X-InitialX).*(ix-InitialX)+InitialY;
- end
- ImageX = [ix, X];
- ImageY = cat(2, iy, Y);
- line(ImageX,ImageY, 'marker', '.', 'markerSize',1, ...
- 'LineStyle', '-', 'LineWidth', 2, 'Color', 'Blue');
- dlmwrite('IXT.txt', ImageX, '-append', 'delimiter', '\t', 'precision', 10);
- dlmwrite('IZT.txt', ImageY, '-append', 'delimiter', '\t', 'precision',10);
- InitialX = X; %记住当前点坐标
- InitialY = Y; %记住当前点坐标
- % 列印「MouseDraw is moving!」及滑鼠现在位置
- % fprintf('MouseDraw is moving! Current location = (%g, %g)\n', ...
- % CurPiont(1,1), CurPiont(1,2));
- % % fprintf('MouseDraw move!\n');
- % 设定滑鼠按钮被释放时的反应指令为「MouseDraw up」
- % set(gcf, 'WindowButtonUpFcn', 'MouseDraw up');
- %%滑鼠按钮被释放时的反应指令
- case 'up'
- % 清除滑鼠移动时的反应指令
- set(gcf, 'WindowButtonMotionFcn', '');
- % 清除滑鼠按钮被释放时的反应指令
- set(gcf, 'WindowButtonUpFcn', '');
- % 列印「MouseDraw up!」
- % % fprintf('MouseDraw up!\n');
- end
- end
5、也是拖动直线的,不过一个拖动时,另一个不动。
点击show position...在命令行返回数值(因为有GUI所以打包放在我的资源里了,需要的自己下)点击打开链接。
6、后记
以上为网上资源的收集,主要是供自己学习。
如果碰巧你需要,甚幸~.~ //
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)