Combining matlab Scripts -


i trying create user interactive animation in matlab shape translates , rotates across screen , user has click on , click on white space, program exits. have animation , clicking script written unsure how combine them. have posted each script below. appreciated.

clear all; close all; clc 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);  = 1:nframes  rx = [cos(rr(i)), -sin(rr(i))]; ry = [sin(rr(i)), cos(rr(i))];  x1 = rx * [x0; y0]; y1 = ry * [x0; y0];  y=fill(x1 + xx(i), y1 + yy(i), 'b'); xlim([0,10]); ylim([0,10]); hold on; pause(0.000000003); delete(y);  end    xv = [ -3 3 3 -3]; %// x coords of polygon vertices. arbitrary number yv = [-5 -5 7 7]; %// y coords of polygon vertices. same number x fill(xv,yv,'b') %// draw polygon axis([-10 10 -10 10]) [xp, yp] = ginput(1); %// point coordinates inside = inpolygon(xp,yp,xv,yv); %// inside?  while inside fprintf('inside\n') [xp, yp] = ginput(1); inside = inpolygon(xp,yp,xv,yv); end fprintf('outside\n') 

errors:

error using inpolygon (line 66) polygon must defined vectors (xv, yv).  error in mousedowncallback (line 20)     if     inpolygon(coordinates(1),coordinates(2),xvertices,yvertices)  error using pause error while evaluating figure windowbuttondownfcn  error using inpolygon (line 66) polygon must defined vectors (xv, yv).  error in mousedowncallback (line 20)     if     inpolygon(coordinates(1),coordinates(2),xvertices,yvertices)  error using pause error while evaluating figure windowbuttondownfcn  error using delete invalid or deleted object.  error in movingpolygon (line 40) delete(y); 

an alternative combining scripts register callback figure displaying polygon callback fires every time user clicks within axes of figure. relatively straightforward , requires few lines of code, though make use of global variables may or may not issue. (the alternative create class manage figure , callbacks, , removes need global variables.)

to make things little simpler, change first script function, wraps code as

function movingpolygon     % code end 

the function name can same script name. can add callback function file. in main function, declare 3 global variables after clear commands

clear all; close all; clc  global guserhitpolygon;   % indicates whether user has hit polygon or not global gcurrentxvertices; % x-vertices of moving polygon global gcurrentyvertices; % y-vertices of moving polygon 

default guserhitpolygon true since want polygon move until user hits whitespace

guserhitpolygon = true; 

in for loop update polygon position, save x- , y-vertices (used in fill) remaining 2 global variables. on each iteration of i, these 2 global variables updated.

now continue moving polygon until user misses hit, last statement in for loop, after pause, should check see if user has hit polygon or missed it

if ~guserhitpolygon     clear global guserhitpolygon gcurrentxvertices gcurrentyvertices;     break; end 

in above, clear global variables (since no longer needed) , break out of for loop.

the thing left do, besides defining callback, register mouse button down events. prior entering for loop, create figure , register event

h = figure; set(h,'windowbuttondownfcn',   @mousedowncallback); 

that's - figure respond mouse button down events. callback, defined in same file, resembles second script

% callback mouse button down event. function mousedowncallback(~,~)      global guserhitpolygon;     global gcurrentxvertices;     global gcurrentyvertices;      % save local variables in case these globals cleared (this     % "danger" of using globals - not "thread-safe")     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         coord = get(gca,'currentpoint');         coord = coord(1,1:2);          % if coordinate not in polygon, change         % flag         if ~inpolygon(coord(1),coord(2),xvertices,yvertices)            guserhitpolygon = false;         end     end end 

and it. try above , see happens.


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -