<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: