HowToC:DrawPoints

From S2PLOT

Revision as of 23:36, 10 December 2007; view current revision
←Older revision | Newer revision→
Jump to: navigation, search

Contents

Drawing Points

Perhaps the most common object you will need to draw as a point. There are two different ways to do this, using either the PGPLOT-style functions or the native S2PLOT point functions.

For PGPLOT programmers, the functional interface to s2pt1 (draw a single point), s2pt (draw multiple points) and s2pnts (draw multiple points with different symbols) will seem very familiar. In each case, x, y and z coordinates are passed to the functions along with an integer value for the "marker" style to use.

Points example

Copy and paste the following code, and save the file as s2points.c

#include <stdio.h>
#include <stdlib.h>
#include "s2plot.h"

int main(int argc, char *argv[])
{
   float xmin = -1, xmax = +1;          /* X data range */
   float ymin = -1, ymax = +1;          /* Y data range */
   float zmin = -1, zmax = +1;          /* Z data range */

   s2opend("/?",argc,argv);             /* Which S2PLOT device to open? */
   s2swin(xmin, xmax, ymin, ymax, zmin, zmax);  /* Set the display coordinates */

   s2pt1(0, 0, 0, 2);
  
   float x[10], y[10],z[10];            /* Coordinates of points */
   int sym[10];                         /* Symbol to draw for each point */
   int i;        

   for (i=0;i<10;i++) {                 /* Generate random point positions */
      x[i] = drand48()*(xmax-xmin) + xmin;
      y[i] = drand48()*(ymax-ymin) + ymin;
      z[i] = drand48()*(zmax-zmin) + zmin;
   }
   s2pt(10, x, y, z, 1);
       
   for (i=0;i<10;i++) {                 /* Generate more random point positions */
      x[i] = drand48()*(xmax-xmin) + xmin;
      y[i] = drand48()*(ymax-ymin) + ymin;
      z[i] = drand48()*(zmax-zmin) + zmin;
      sym[i] = random()%7;                      /* Choose a random symbol */
      if (sym[i] == 3) sym[i] = 1;              /* There is no symbol = 3 */
      else if (sym[i] == 5) sym[i] = 6;         /* There is no symbol = 5 */
   }
   s2pnts(10, x, y, z, sym, 10);        /* Draw points with different symbols */

   s2show(1);                           /* Hand over to the display */

   return 1;
}

Compile using: cbuild.csh s2points<tt> and then run by typing <tt>s2points at the command prompt.

The obvious limitation with this example is that all of the points are the same colour! Point colours can be changed using the function s2sci. Put:

s2sci(N);

where N is an integer between 0 and 15 inclusive, before each of the point plotting commands. For a list of the default colour codes, see Colours. You can choose one of these colours by using, for example:

s2sci(S2_PG_YELLOW);

For more information on colours and colour maps, see Colours and Colour Maps.

The next aspect you may need to control is the thickness or size of points. This is done using s2sch. Put:

s2sch(size);

before each of the point plotting functions, where size is a floating point number. The default value is 1.0.

The second way to plot points is to use the native S2PLOT routines. These provide full (r,g,b) colour control, and come both standard and vector formats (using the struct COLOUR and struct XYZ data structures).

Add the following lines of code before the s2show() command:

   COLOUR rgb;
   XYZ xyz;
   for (i=0;i<10;i++)  {
      xyz.x = drand48()*(xmax-xmin) + xmin;     /* Random point position */
      xyz.y = drand48()*(ymax-ymin) + ymin;
      xyz.z = drand48()*(zmax-zmin) + zmin;
      rgb.r = drand48();                        /* Random colour */
      rgb.g = drand48();
      rgb.b = drand48();
      ns2point(xyz.x, xyz.y, xyz.z, rgb.r, rgb.g, rgb.g);
   }

The (r,g,b) colours are each floating point numbers between 0 and 1. To use the vector form of this function, replace the ns2point function call with:

ns2vpoint(xyz, rgb);

In order to plot multiple points with the same colour, use ns2vnpoint. Add the following lines before s2show():

   XYZ xyz_a[10];
   for (i=0;i<10;i++)  {
      xyz_a[i].x = drand48()*(xmax-xmin) + xmin;
      xyz_a[i].y = drand48()*(ymax-ymin) + ymin;
      xyz_a[i].z = drand48()*(zmax-zmin) + zmin;
   }
   rgb.r = drand48();
   rgb.g = drand48();
   rgb.b = drand48();
   ns2vnpoint(xyz_a, rgb, 10);                  /* Draw points with same colour */

Symbol thickness for the PGLOT-style functions was controlled with s2sch(). To get thick points use ns2thpoint or ns2vthpoint. Put the following code before s2show(); to see how these are used:

   for (i=0;i<10;i++)  {
      xyz.x = drand48()*(xmax-xmin) + xmin;     /* Random point position */
      xyz.y = drand48()*(ymax-ymin) + ymin;
      xyz.z = drand48()*(zmax-zmin) + zmin;
      rgb.r = drand48();                        /* Random colour */
      rgb.g = drand48();
      rgb.b = drand48();
      ns2thpoint(xyz.x, xyz.y, xyz.z, rgb.r, rgb.g, rgb.g, 5);

      xyz.x = drand48()*(xmax-xmin) + xmin;     /* Random point position */
      xyz.y = drand48()*(ymax-ymin) + ymin;
      xyz.z = drand48()*(zmax-zmin) + zmin;
      rgb.r = drand48();                        /* Random colour */
      rgb.g = drand48();
      rgb.b = drand48();
      ns2vthpoint(xyz, rgb, 3);
   }

Finally, to draw an arbitrary marker use ns2m or ns2vm. Put the following code before s2show();

   int ssym;
   float size = 0.1;
   for (i=0;i<10;i++)  {
      xyz.x = drand48()*(xmax-xmin) + xmin;     /* Random point position */
      xyz.y = drand48()*(ymax-ymin) + ymin;
      xyz.z = drand48()*(zmax-zmin) + zmin;
      rgb.r = drand48();                        /* Random colour */
      rgb.g = drand48();
      rgb.b = drand48();
      ssym = random()%7;                        /* Choose a random symbol */
      if (ssym == 3) ssym = 1;                  /* There is no symbol = 3 */
      else if (ssym == 5) ssym = 6;             /* There is no symbol = 5 */

      ns2m(ssym, size, xyz.x, xyz.y, xyz.z, rgb.r, rgb.g, rgb.g);

      xyz.x = drand48()*(xmax-xmin) + xmin;     /* Random point position */
      xyz.y = drand48()*(ymax-ymin) + ymin;
      xyz.z = drand48()*(zmax-zmin) + zmin;
      rgb.r = drand48();                        /* Random colour */
      rgb.g = drand48();
      rgb.b = drand48();

      ns2vm(ssym, size, xyz, rgb);
   }

Experiment for yourself

Try changing the number of points that are plotted, generate point positions by reading in your favourite dataset, or experiment with colours and markers.

Function Overview

For more information on the functions we have introduced so far, see:

Common Problems

The marker size set using ns2m or ns2vm is not the same as the marker size set using s2sch - you may need to experiment to find a suitable value.

Test code: s2points.c

This is the full test code for the point-drawing example.

#include <stdio.h>
#include <stdlib.h>
#include "s2plot.h"

int main(int argc, char *argv[])
{
   float xmin = -1, xmax = +1;          /* X data range */
   float ymin = -1, ymax = +1;          /* Y data range */
   float zmin = -1, zmax = +1;          /* Z data range */

   s2opend("/?",argc,argv);             /* Which S2PLOT device to open? */
   s2swin(xmin, xmax, ymin, ymax, zmin, zmax);  /* Set the display coordinates */

   s2sci(8);
   s2sch(4);
   s2pt1(0, 0, 0, 2);

   float x[10], y[10],z[10];            /* Coordinates of points */
   int sym[10];                         /* Symbol to draw for each point */
   int i;
   for (i=0;i<10;i++) {                 /* Generate random point positions */
      x[i] = drand48()*(xmax-xmin) + xmin;
      y[i] = drand48()*(ymax-ymin) + ymin;
      z[i] = drand48()*(zmax-zmin) + zmin;
   }
   s2sci(3);
   s2sch(10);
   s2pt(10, x, y, z, 1);

   for (i=0;i<10;i++) {                 /* Generate more random point positions */
      x[i] = drand48()*(xmax-xmin) + xmin;
      y[i] = drand48()*(ymax-ymin) + ymin;
      z[i] = drand48()*(zmax-zmin) + zmin;
      sym[i] = random()%7;                      /* Choose a random symbol */
      if (sym[i] == 3) sym[i] = 1;              /* There is no symbol = 3 */
      else if (sym[i] == 5) sym[i] = 6;         /* There is no symbol = 5 */
   }
   s2sci(4);
   s2sch(2);
   s2pnts(10, x, y, z, sym, 10);        /* Draw points with different symbols */

   COLOUR rgb;
   XYZ xyz;
   for (i=0;i<10;i++)  {
      xyz.x = drand48()*(xmax-xmin) + xmin;     /* Random point position */
      xyz.y = drand48()*(ymax-ymin) + ymin;
      xyz.z = drand48()*(zmax-zmin) + zmin;
      rgb.r = drand48();                        /* Random colour */
      rgb.g = drand48();
      rgb.b = drand48();
      ns2point(xyz.x, xyz.y, xyz.z, rgb.r, rgb.g, rgb.g);
   }

   for (i=0;i<10;i++)  {
      xyz.x = drand48()*(xmax-xmin) + xmin;     /* Random point position */
      xyz.y = drand48()*(ymax-ymin) + ymin;
      xyz.z = drand48()*(zmax-zmin) + zmin;
      rgb.r = drand48();                        /* Random colour */
      rgb.g = drand48();
      rgb.b = drand48();
      ns2vpoint(xyz, rgb);                      /* Plot with vector routine */
   }

   XYZ xyz_a[10];
   for (i=0;i<10;i++)  {
      xyz_a[i].x = drand48()*(xmax-xmin) + xmin;
      xyz_a[i].y = drand48()*(ymax-ymin) + ymin;
      xyz_a[i].z = drand48()*(zmax-zmin) + zmin;
   }
   rgb.r = drand48();
   rgb.g = drand48();
   rgb.b = drand48();
   ns2vnpoint(xyz_a, rgb, 10);                  /* Draw points with same colour */

   for (i=0;i<10;i++)  {
      xyz.x = drand48()*(xmax-xmin) + xmin;     /* Random point position */
      xyz.y = drand48()*(ymax-ymin) + ymin;
      xyz.z = drand48()*(zmax-zmin) + zmin;
      rgb.r = drand48();                        /* Random colour */
      rgb.g = drand48();
      rgb.b = drand48();
      ns2thpoint(xyz.x, xyz.y, xyz.z, rgb.r, rgb.g, rgb.g, 5);

      xyz.x = drand48()*(xmax-xmin) + xmin;     /* Random point position */
      xyz.y = drand48()*(ymax-ymin) + ymin;
      xyz.z = drand48()*(zmax-zmin) + zmin;
      rgb.r = drand48();                        /* Random colour */
      rgb.g = drand48();
      rgb.b = drand48();
      ns2vthpoint(xyz, rgb, 3);
   }

   int ssym;
   float size = 0.1;
   for (i=0;i<10;i++)  {
      xyz.x = drand48()*(xmax-xmin) + xmin;     /* Random point position */
      xyz.y = drand48()*(ymax-ymin) + ymin;
      xyz.z = drand48()*(zmax-zmin) + zmin;
      rgb.r = drand48();                        /* Random colour */
      rgb.g = drand48();
      rgb.b = drand48();
      ssym = random()%7;                        /* Choose a random symbol */
      if (ssym == 3) ssym = 1;                  /* There is no symbol = 3 */
      else if (ssym == 5) ssym = 6;             /* There is no symbol = 5 */

      ns2m(ssym, size, xyz.x, xyz.y, xyz.z, rgb.r, rgb.g, rgb.g);

      xyz.x = drand48()*(xmax-xmin) + xmin;     /* Random point position */
      xyz.y = drand48()*(ymax-ymin) + ymin;
      xyz.z = drand48()*(zmax-zmin) + zmin;
      rgb.r = drand48();                        /* Random colour */
      rgb.g = drand48();
      rgb.b = drand48();

      ns2vm(ssym, size, xyz, rgb);
   }

   s2show(1);                                   /* Hand over to the display */

   return 1;
}

Where to Next?

Personal tools