User interactive animation in matlab -
i trying make user interactive animation in matlab. square translates , rotates across screen , user has click on it. if click on it, receive points , animation repeat. if click on whitespace (eg. anywhere except within square) animation exit , lose displayed. have animation finished using 2 functions. 1 create animation , registers mouse click. far can recognize mouse click , animation stop if user clicks on whitespace animation not repeat if user clicks within polygon. unsure how modify code animation repeat until user clicks white space. have pasted code below. appreciated.
animation function:
function movingpolygon global guserhitpolygon; global gcurrentxvertices; global gcurrentyvertices; guserhitpolygon = true; nsides =4; %polar points r=1; theta = pi/nsides * (1:2:2*nsides-1); %cartesisn points x0 = r * cos(theta); y0 = r * sin(theta); nframes = 100; xx = linspace(0,10, nframes); yy = xx; rr = linspace(0, 2*pi, nframes); h = figure; set(h,'windowbuttondownfcn', @mousedowncallback); = 1:nframes rx = [cos(rr(i)), -sin(rr(i))]; ry = [sin(rr(i)), cos(rr(i))]; x1 = rx * [x0; y0]; y1 = ry * [x0; y0]; x2= x1 + xx(i); y2= y1 + yy(i); gcurrentxvertices=x2; gcurrentyvertices=y2; y=fill(x2, y2, 'b'); xlim([0,10]); ylim([0,10]); hold on; pause(0.000000003); if ~guserhitpolygon clear global guserhitpolygon gcurrentxvertices gcurrentyvertices; break; end delete(y); end end
callback function:
function mousedowncallback(~,~) global userhitpolygon; global currentxvertices; global currentyvertices; xvertices = gcurrentxvertices; yvertices = gcurrentyvertices; % if have valid (so non-empty) sets of x- , y-vertices then... if ~isempty(xvertices) && ~isempty(yvertices) % coordinate on current axis coordinates = get(gca,'currentpoint'); coordinates = coordinates(1,1:2); % if coordinate not in polygon, change % flag if ~inpolygon(coordinates(1),coordinates(2),xvertices,yvertices) guserhitpolygon = false; end end end
edit: fixed bugs in callback function.
short answer
include animation in while loop
long answer
no matter user does, animation play once, because doesn't know repeat. answer use while loop, repeat animation until tell stop. main loop becomes
done = false; % stopping condition while ~done = 1:nframes rx = [cos(rr(i)), -sin(rr(i))]; ry = [sin(rr(i)), cos(rr(i))]; x1 = rx * [x0; y0]; y1 = ry * [x0; y0]; x2= x1 + xx(i); y2= y1 + yy(i); gcurrentxvertices=x2; gcurrentyvertices=y2; y=fill(x2, y2, 'b'); xlim([0,10]); ylim([0,10]); hold on; pause(0.000000003); if ~guserhitpolygon clear global guserhitpolygon gcurrentxvertices gcurrentyvertices; done = true; % set stopping condition break; end delete(y); end end
Comments
Post a Comment