<texit info> author=Roman Putanowicz title=Solution to exercise 6.1.1 </texit> back Solution to exercise 6.1.1 <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: variable stp defined in line 4 is used to indicate that the user requested termination of the program, in line 5 and 6 we calculate vector of codes for the keys (we also handle the case of the pressed caps lock). The letter “q” is special because its code is -1, in line 9, 21, 26 we check if the even key 'b' is equal to any of the defined key values, the variable cp defined in line 15 is used to hold coordinates of the mouse cursor as a column vector, in line 20 we append the current point coordinates the the matrix that stores coordinates of already clicked points. en/teaching/subjects/it/labs/sol_6_1_1.txt Last modified: 2017/10/02 15:54(external edit)