loops - Replace previous plot with each mouse click in matlab -
hi im trying have first part of program plot line origin length 10 in direction of mouse clicks in plot area. have this:
[gx,gy,b] = ginput(1); theta = atand(gy/gx); theta = theta*(pi/180); polar(theta,8,'-k')
within loop of while b~=3. ie trying whilst clicking left mouse button, want plot line in direction of click, , when right button pressed...move onto rest of program line staying in position of last left click made.
also, when each left click made, previous line plotted should replaced new one. ie 1 line on screen @ time.
thanks in advance help.
the following script works me, if interpreted question correctly.
figure r=10; % desired line length polar(0,r) % empty polar plot b=0; while b~=3 [gx,gy,b]=ginput(1); if b~=3 % not right mouse click theta=atan(gy/gx); if gx<0 theta=theta+pi; % needed, because atan(gy/gx) range -pi/2 pi/2 end polar([0 theta],[0 r],'-k') end end
what does
a figure made polar plot r=10
. next, loop initiated. in loop, ginput()
function called. outputs stored in gx
, gy
. next, checked whether or not right mouse button pressed. if not, gx
, gy
used did arctangent. if gx<0
, arctangent still calculate angle theta
between -pi/2 , pi/2, on right half of polar plot. therefore, pi added theta if case. black line plotted origin [0,0]
radius of 10 , calculated angle theta.
if right mouse clicked anywhere in figure, while loop left , matlab continues. note gx
, gy
contain position of right mouse click, not previous (not right) mouse click.
was intended behaviour? if not, let me know, can see if code can improved.
Comments
Post a Comment