Code covered by the BSD License  

Highlights from
throw body with mouse

image thumbnail
from throw body with mouse by Maxim Vedenyov
It throws body in gravitation. Initial velocity defined by pointer velocity.

move(hfl,xp,yp,xd,yd,kkx,kky,xys1,xys2,xl,yl)
function move(hfl,xp,yp,xd,yd,kkx,kky,xys1,xys2,xl,yl)
% makes move
% hfl - fill handle (circle)
% xp,yp - initial pointer position
% xd,yd - initial coodinaties in fill
% kk - scale coefficient between pixels and units of axes

%global xys sp iscnt mth res hc

global hf ha xys t dt xyh th hsm hlg


xys_old=[xys1;xys2];

no=get(hfl,'Userdata');

dxl=xl(2)-xl(1);
dyl=yl(2)-yl(1);

pl=get(0,'PointerLocation');
xpt=pl(1);
ypt=pl(2);

dx=kkx*(xpt-xp);
dy=kky*(ypt-yp);

% % axis limits:
% wth=0;
% xlw=[xl(1)+wth xl(2)-wth];
% ylw=[yl(1)+wth yl(2)-wth];
% if (xys_old(1,no)+dx)>xlw(2)
%     dx=xlw(2)-xys_old(1,no);
% end
% 
% if (xys_old(1,no)+dx)<xlw(1)
%     dx=xlw(1)-xys_old(1,no);
% end
% 
% if (xys_old(2,no)+dy)>ylw(2)
%     dy=ylw(2)-xys_old(2,no);
% end
% 
% if (xys_old(2,no)+dy)<ylw(1)
%     dy=ylw(1)-xys_old(2,no);
% end


nm=length(xys(1,:));

    
xdt=xd+dx;
ydt=yd+dy;

xys(1,no)=xys_old(1,no)+dx;
xys(2,no)=xys_old(2,no)+dy;

% hystory
if hlg<hsm
    xyh=[xyh, xys];
    th=[th, toc];
    hlg=hlg+1;
    
else
    xyh=[xyh(:,2:end), xys];
    th=[th(2:end), toc];
end

set(hfl,'XData',xdt);
set(hfl,'YData',ydt);

update_in_main;

% dt=toc-t;
% t=t+dt;

drawnow;


Contact us at files@mathworks.com