function cutter_demo % egr106 project 2006 clc clear %% 2 demos for demo = 1:2 close all x1 = [ 1 2 3 2 ]; y1 = [ 1 1 2 3 ]; patch(x1,y1,'r') x2 = [ 4 5 5 3 ]; y2 = [ 1 2 4 3 ]; patch(x2,y2,'g') axis([0 6 0 5]) grid on switch demo case 1 pathx = [ x1, x1(1), x2(4), x2, 0 ]; pathy = [ y1, x1(1), y2(4), y2, 0 ]; type = [ 0 1 1 1 1 0 1 1 1 1 0 ]; case 2 pathx = [ x1(1:3), x2(4), x2, x1(3:end), x1(1), 0 ]; pathy = [ y1(1:3), y2(4), y2, y1(3:end), y1(1), 0 ]; type = [ 0 1 1 0 1 1 1 1 0 1 1 0 ]; end egr106cutter(1,1,1,1) for k = 1:length(pathx) egr106cutter(pathx(k),pathy(k),type(k),0) end pause end %% cutting function function egr106cutter(xnew,ynew,type,flag) % function to simulate motion of the cutter % path is from current location to (xnew,ynew) % type specifies cutting (1) versus moving (0) % differences include speed and color % cutting and moving lengths since last reset are % shown in the title area % flag is normally 0; flag = 1 resets window persistent x y cutlen movlen blobhandle titlehandle if flag == 1 % reinitialize routine x = 0; y = 0; cutlen = 0; movlen = 0; hold on blobhandle = plot(0,0,'o','markersize',10,'markerfacecolor','k'); titlehandle = title(['Cut length = ',num2str(0,4),... '; Move length = ',num2str(0,4)]); else dist = sqrt((x-xnew)^2+(y-ynew)^2); if type == 1 % cutting n = ceil(20*dist); color = 'k'; else % moving n = ceil(5*dist); color = 'r'; end hold on xv = linspace(x,xnew,n); yv = linspace(y,ynew,n); linehandle = plot(xv(1),yv(1),color,'linewidth',3); for k2 = 1:length(xv) set(blobhandle,'Xdata',xv(k2),'Ydata',yv(k2)) set(linehandle,'Xdata',xv([1,k2]),'Ydata',yv([1,k2])) pause(.05) if type == 1 cutlen = cutlen + dist/n; else movlen = movlen + dist/n; end set(titlehandle,'String',['Cut length = ',sprintf('%6.3f',cutlen),... '; Move length = ',sprintf('%6.3f',movlen)]) drawnow end x = xnew; y = ynew; end