<texit info> author=Roman Putanowicz title=Solution to exercise 6.1.1
</texit> back
<sxh c> h = figure(); axis([-5,5,-5,5]); xy = []; # array of vertex coordinates stp = 0; quitchar = [-1, toascii(“Q”)]; closechar = [toascii(“cC”)]; while stp == 0
[x,y,b] = ginput(1);
if sum(b == quitchar) # request for exit
break;
endif
xy= [x;y];
while 1
[x,y,b] = ginput(1);
cp = [x;y];
if b == 1 # draw new segment apped coords to xy
xs = [xy(1,end), x];
ys = [xy(2,end), y];
line(xs,ys);
xy = [xy, cp];
elseif sum(b == closechar) # close the polyline finish drawing
xs = [xy(1,1), xy(1,end)];
ys = [xy(2,1), xy(2,end)];
line(xs,ys);
break;
elseif sum(b == quitchar) # request for exit
stp = 1;
break;
endif
endwhile
endwhile </sxh>
<texit> \begin{lstlisting} h = figure(); axis([-5,5,-5,5]); xy = []; # array of vertex coordinates stp = 0; quitchar = [-1, toascii(“Q”)]; closechar = [toascii(“cC”)]; while stp == 0
[x,y,b] = ginput(1);
if sum(b == quitchar) # request for exit
break;
endif
xy= [x;y];
while 1
[x,y,b] = ginput(1);
cp = [x;y];
if b == 1 # draw new segment apped coords to xy
xs = [xy(1,end), x];
ys = [xy(2,end), y];
line(xs,ys);
xy = [xy, cp];
elseif sum(b == closechar) # close the polyline finish drawing
xs = [xy(1,1), xy(1,end)];
ys = [xy(2,1), xy(2,end)];
line(xs,ys);
break;
elseif sum(b == quitchar) # request for exit
stp = 1;
break;
endif
endwhile endwhile \end{lstlisting} </texit>
Description:
stp defined in line 4 is used to indicate that the user requested termination of the program,cp defined in line 15 is used to hold coordinates of the mouse cursor as a column vector,