sciapp / gr

GR framework: a graphics library for visualisation applications
Other
327 stars 54 forks source link

Polyline not plotting on simple example #176

Closed general-rishkin closed 1 year ago

general-rishkin commented 1 year ago

This example plot is empty.

int main(void) {
double x[] = {1.0, 1.2, 1.4, 1.6, 1.8, 2.0};
double y[] = {0.3, 0.5, 0.4, 0.2, 0.6, 0.7};
gr_polyline(6, x, y);
gr_axes(gr_tick(0, 1), gr_tick(0, 1), 0, 0, 1, 1, -0.01);
// Press any key to exit
getc(stdin);
return 0;
jheinen commented 1 year ago

You will have set the window limits and call gr_update() to flush all internal buffers:

/*
 cc ex.c -I/usr/local/gr/include -L/usr/local/gr/lib -lGR -Wl,-rpath,/usr/local/gr/lib
 */
#include <stdio.h>
#include "gr.h"

int main(void)
{
  double x[] = {1.0, 1.2, 1.4, 1.6, 1.8, 2.0};
  double y[] = {0.3, 0.5, 0.4, 0.2, 0.6, 0.7};

  gr_setwindow(1, 2, 0, 1);
  gr_polyline(6, x, y);
  gr_axes(gr_tick(1, 2), gr_tick(0, 1), 1, 0, 1, 1, -0.01);
  gr_updatews();

// Press any key to exit
  getc(stdin);
  return 0;
}
danielkaiser commented 1 year ago

Hi @general-rishkin,

Polylines are specified in world coordinates with the default coordinate window spanning $[0, 1] \times [0, 1]$, therefore all x coordinates of your example are outside of the visible window and clipped. To specify a different region (e.g. $[1, 2] \times [0, 1]$), you can use gr_setwindow. Also depending on the output workstation it is best to explicitly draw by calling gr_updatews();.

With these changes your example could look like this:

#include <stdio.h>
#include "gr.h"

int main(void) {
    double x[] = {1.0, 1.2, 1.4, 1.6, 1.8, 2.0};
    double y[] = {0.3, 0.5, 0.4, 0.2, 0.6, 0.7};
    gr_setwindow(1, 2, 0, 1);
    gr_polyline(6, x, y);
    gr_axes(gr_tick(1, 2), gr_tick(0, 1), 1, 0, 1, 1, -0.01);
    gr_updatews();
    // Press any key to exit
    getc(stdin);
    return 0;
}
general-rishkin commented 1 year ago

Thanks @jheinen and @danielkaiser. That works.