Interactive plots

From Octave
Revision as of 02:06, 21 August 2019 by Siko1056 (talk | contribs) (Add Category:Examples.)
Jump to navigation Jump to search

This page shows some possibilities to create interactive OpenGL plots (qt or fltk toolkit) not using uicontrols.

See also uicontrols where you can create buttons and slider to create plots

Demo 1

Interactive select2.gif

Code: imageViewer example
clear all
graphics_toolkit qt
set (0, "defaultlinelinewidth", 2);

h.points = rand (2, 3);         # 3 random points
h.line = [];
h.marker = [];
set (gcf, "userdata", h)

function down_fig (hsrc, evt)

  h = get (hsrc, "userdata");
  if (isempty (h.marker))
    hold on
    h.marker = plot (NA, NA, "o", "markersize", 15, "color", "green");
    hold off

  set (hsrc, "userdata", h);
  drag_fig (hsrc, evt);

function drag_fig (hsrc, evt)

  # evt 1:left button, 2:middle button, 3:right button
  h = get (hsrc, "userdata");

  if (! isempty (h.marker))
    c = get (gca, "currentpoint")([1;3]);
    set (h.marker, "xdata", c(1));
    set (h.marker, "ydata", c(2));

    # find nearest point
    d = h.points - c;
    [~, idx] = min (hypot (d(1, :), d(2, :)));
    h.points(:, idx) = c;


  # draw / update the line
  tmp = [h.points h.points(:,1)]; # duplicate first point to close triangle
  if (isempty (h.line))
    h.line = plot (tmp(1, :), tmp(2, :), "-o");
    h.text = text (NA, NA, "", "horizontalalignment", "center");
    ## testing
    axis ([0 1 0 1])
    set (h.line, "xdata", tmp(1, :));
    set (h.line, "ydata", tmp(2, :));

  # calculate the area
  A = polyarea (h.points(1, :),
                h.points(2, :));
  P = mean (h.points, 2);
  set (h.text, "position", mean (h.points, 2).');
  set (h.text, "string", sprintf ("A = %.3f", A));

  set (hsrc, "userdata", h);


function up_fig (hsrc, evt)

  h = get (gcbf, "userdata");
  delete (h.marker);
  h.marker = [];
  set (gcbf, "userdata", h);


set (gcf, "windowbuttondownfcn", @down_fig);
set (gcf, "windowbuttonmotionfcn", @drag_fig)
set (gcf, "windowbuttonupfcn", @up_fig)

# first update
drag_fig (gcf, [])