Advertisement
Guest User

Untitled

a guest
Jan 1st, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Octave 1.79 KB | None | 0 0
  1. clear all
  2. graphics_toolkit qt
  3. set (0, "defaultlinelinewidth", 2);
  4.  
  5. h.points = rand (2, 3);         # 3 random points
  6. h.line = [];
  7. h.marker = [];
  8. set (gcf, "userdata", h)
  9.  
  10. function down_fig (hsrc, evt)
  11.  
  12.   h = get (hsrc, "userdata");
  13.   if (isempty (h.marker))
  14.     hold on
  15.     h.marker = plot (NA, NA, "o", "markersize", 15, "color", "green");
  16.     hold off
  17.   endif
  18.  
  19.   set (hsrc, "userdata", h);
  20.   drag_fig (hsrc, evt);
  21. endfunction
  22.  
  23. function drag_fig (hsrc, evt)
  24.  
  25.   # evt 1:left button, 2:middle button, 3:right button
  26.   h = get (hsrc, "userdata");
  27.  
  28.   if (! isempty (h.marker))
  29.     c = get (gca, "currentpoint")([1;3]);
  30.     set (h.marker, "xdata", c(1));
  31.     set (h.marker, "ydata", c(2));
  32.  
  33.     # find nearest point
  34.     d = h.points - c;
  35.     [~, idx] = min (hypot (d(1, :), d(2, :)));
  36.     h.points(:, idx) = c;
  37.    
  38.   endif
  39.  
  40.   # draw / update the line
  41.   tmp = [h.points h.points(:,1)]; # duplicate first point to close triangle
  42.   if (isempty (h.line))
  43.     h.line = plot (tmp(1, :), tmp(2, :), "-o");
  44.     h.text = text (NA, NA, "", "horizontalalignment", "center");
  45.     ## testing
  46.     axis ([0 1 0 1])
  47.   else
  48.     set (h.line, "xdata", tmp(1, :));
  49.     set (h.line, "ydata", tmp(2, :));
  50.   endif
  51.  
  52.   # calculate the area
  53.   A = polyarea (h.points(1, :),
  54.                 h.points(2, :));
  55.   P = mean (h.points, 2);
  56.   set (h.text, "position", mean (h.points, 2).');
  57.   set (h.text, "string", sprintf ("A = %.3f", A));
  58.  
  59.   set (hsrc, "userdata", h);
  60.  
  61. endfunction
  62.  
  63. function up_fig (hsrc, evt)
  64.  
  65.   h = get (gcbf, "userdata");
  66.   delete (h.marker);
  67.   h.marker = [];
  68.   set (gcbf, "userdata", h);
  69.  
  70. endfunction
  71.  
  72. set (gcf, "windowbuttondownfcn", @down_fig);
  73. set (gcf, "windowbuttonmotionfcn", @drag_fig)
  74. set (gcf, "windowbuttonupfcn", @up_fig)
  75.  
  76. # first update
  77. drag_fig (gcf, [])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement