two consequtive cursor's position in a while loop
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
MEHRDAD TAVASSOLI
le 4 Août 2022
Commenté : Bjorn Gustavsson
le 4 Août 2022
Hello,
I have the following proiblem:
I am getting the cursor's poition inside a plot. The code returns only the current position of the cursor. Although I can see the previouse positios but i cant have access to them. What I want is to return the two consequtive position of the cursor (the current poistion and the previous one) inside the while loop.
For example imagine the current position of the cursor is (x1,y1). The next iteration the cursor's position would be (x2,y2). In the current iteration it is (x3,y3). I would like to get (x2,y2) and (x3,y3).
here is what I got so far:
function test_simpleMouseEvents01
SS = get(0,'ScreenSize');
figure('position',[10,10,SS(3)-100,SS(4)-200]); axis off; hold on;
set(gcf,'WindowButtonMotionFcn',{@wbm});
axis([-1,1,-1,1]);
x = [0, 0];
x1 = [];
disp('Move mouse to draw. Press right mouse button to quit.');
while 1
mb = get(gcf,'SelectionType');
if strcmp(mb,'alt')==1
close all;
return;
end
if double(get(gcf,'CurrentCharacter'))==27
close all;
return;
end
cur_point = get(gca,'Currentpoint');
x(1:2) = cur_point(1,1:2)'
assignin('base','x',[x(1,1);x(1,2)]);
plot(x(1), x(2), 'k.','markersize',20);
% x1 = [x1, x']
drawnow;
end
end
%% Mouse move
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function wbm(h,evd)
end
0 commentaires
Réponse acceptée
Bjorn Gustavsson
le 4 Août 2022
Just after you have extracted and exported x in your while-loop you now have the next x-previous, so just stack that one away. Something like this:
function test_simpleMouseEvents01
SS = get(0,'ScreenSize');
figure('position',[10,10,SS(3)-100,SS(4)-200]); axis off; hold on;
set(gcf,'WindowButtonMotionFcn',{@wbm});
axis([-1,1,-1,1]);
x = [0, 0];
x1 = [];
x_prev = [];
disp('Move mouse to draw. Press right mouse button to quit.');
while 1
mb = get(gcf,'SelectionType');
if strcmp(mb,'alt')==1
close all;
return;
end
if double(get(gcf,'CurrentCharacter'))==27
close all;
return;
end
cur_point = get(gca,'Currentpoint');
x(1:2) = cur_point(1,1:2)'
assignin('base','x',[x(1,1);x(1,2)]);
assignin('base','x_prev',x_prev);
x_prev = x;
plot(x(1), x(2), 'k.','markersize',20);
% x1 = [x1, x']
drawnow;
end
end
%% Mouse move
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function wbm(h,evd)
end
First time through the loop x_prev will be empty but after that you're good to go. You might possibly want to initialize x_prev to something different that an empty array, like [0 0] or [-inf,-inf] if you need something of that size.
HTH
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Graphics Performance dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!