来源:https://blog.csdn.net/weixin_39090239/article/details/80586930
前记:
人机交互的方式--键盘开关、鼠标、触摸屏、体感传感器(Kinect、leap motion)等的使用大大促进人与机器的交流过程。
这里记录鼠标事件与MATLAB的结合,为后期机器人仿真控制打下基础---如鼠标直接拖动机器人运动(像很多机器人仿真软件里的作用一样,如Robotstudio、soldworks、proe\core、Adams等)。
1、鼠标画矩形
代码功能:运行代码在出现的figure窗口,点击鼠标画出矩形
-
-
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
-
-
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)];
-
-
-
2、鼠标拖动直线
-
-
-
aH=axes('Xlim',[0 1],'Ylim',[0 1]);
-
h=line([0.5 0.5],[0 1],...
-
-
-
'ButtonDownFcn',@startDragFcn);
-
set(f,'WindowButtonUpFcn',@startDragFcn);
-
function startDragFcn(varargin)
-
set(f,'WindowButtonMotionFcn',@draggingFcn);
-
-
function draggingFcn(varargin)
-
pt=get(aH,'CurrentPoint');
-
set(h,'Xdata',pt(1)*[1 1]);
-
-
function stopDragFcn(varargin)
-
set(f,'WindowButtonMotionFcn','');
-
-
3、显示坐标
-
-
-
-
-
-
-
-
set(gcf, 'WindowButtonMotionFcn', @callback);
-
-
function callback(hObject, event)
-
loc = get(gca, 'CurrentPoint');
-
-
set(tb, 'string', num2str(loc), 'position', loc);
-
-
4、画轨迹并保存点坐标
-
function MouseDraw(action)
-
% MouseDraw 本例展示如何以Handle Graphics来设定滑鼠事件
-
% (MouseDraw Events)的反应指令(Callbacks)
-
-
-
-
global InitialX InitialY FigHandle
-
if nargin == 0, action = 'start';
-
-
-
-
-
-
FigHandle = figure('WindowButtonDownFcn','MouseDraw down');
-
axis([-600 600 -600 600]); % 设定图轴范围
-
-
-
-
-
-
%%设定滑鼠按钮被按下时的反应指令为「MouseDraw down」
-
% set(gcf, 'WindowButtonDownFcn', 'MouseDraw down');
-
dlmwrite('IXT.txt', 10, 'delimiter', '\t', 'precision', 10);
-
dlmwrite('IZT.txt', 10, 'delimiter', '\t', 'precision', 10);
-
-
-
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);
-
-
% % 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);
-
-
-
-
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;
-
-
-
InputImage = imrotate(InputImage,180); % 图像旋转
-
-
-
-
-
-
CurPiont = get(gca, 'CurrentPoint');
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
if abs(X-InitialX) < 0.1 % 线平行于y轴,即斜率不存在时
-
-
ix = X.*ones(1,size(iy,2));
-
-
ix = InitialX:step_x:X ; % 定义x的变化范围和步长
-
% 当斜率存在,即k = (Y-InitialY)/(X-InitialX) ~= 0
-
iy = (Y-InitialY)/(X-InitialX).*(ix-InitialX)+InitialY;
-
-
-
-
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);
-
-
-
% 列印「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');
-
-
-
-
set(gcf, 'WindowButtonMotionFcn', '');
-
-
set(gcf, 'WindowButtonUpFcn', '');
-
-
% % fprintf('MouseDraw up!\n');
-
-
-
-
5、也是拖动直线的,不过一个拖动时,另一个不动。点击show position...在命令行返回数值(因为有GUI所以打包放在我的资源里了,需要的自己下)点击打开链接。
6、后记
以上为网上资源的收集,主要是供自己学习。如果碰巧你需要,甚幸~.~ //
|
请发表评论