DISLIN Examples / C


Demonstration of CURVE / C


#include <stdio.h>
#include <math.h>
#include "dislin.h"

float xray[100], yray[100], y1ray[100], y2ray[100];

main()
{ int n = 100, i;
  double fpi = 3.1415926/180., step, x;

  step = 360. / (n-1);

  for (i = 0; i < n; i++)
  { xray[i] = i * step;
    x = xray[i] * fpi;
    y1ray[i] = sin(x);
    y2ray[i] = cos(x);
  }

  disini();
  pagera();
  complx();
  axspos(450,1800);
  axslen(2200,1200);

  name("X-axis","x");
  name("Y-axis","y");

  labdig(-1,"x");
  ticks(10,"xy");

  titlin("Demonstration of CURVE",1);
  titlin("SIN(X), COS(X)",3);

  graf(0.,360.,0.,90.,-1.,1.,-1.,0.5);
  title();

  color("red");
  curve(xray,y1ray,n);
  color("green");
  curve(xray,y2ray,n);

  color("fore");
  dash();
  xaxgit();

  disfin();
  return 0;
}


Symbols / C

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

main()
{ int nl, ny, i, nxp;
  static char ctit[] = "Symbols", cstr[3];

  setpag("da4p");
  disini();
  pagera();
  complx();

  height(60);
  nl = nlmess(ctit);
  messag(ctit,(2100-nl)/2,200);

  height(50);
  hsymbl(120);

  ny = 150;

  for (i = 0; i < 22; i++)
  { if ((i % 4) == 0) 
    { ny  += 400;
      nxp  = 550;
    }
    else
    { nxp += 350;
    }

    sprintf(cstr,"%d",i); 
    nl = nlmess(cstr)/2;
    messag(cstr,nxp-nl,ny+150);
    symbol(i,nxp,ny);
  }

  disfin();
}


Interpolation Methods / C

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

main()
{ int nya = 2700, i, nx, ny;
  static float
        x[] = {0.,1.,3.,4.5,6.,8.,9.,11.,12.,12.5,13.,15.,16.,17.,19.,20.},
        y[] = {2.,4.,4.5,3.,1.,7.,2.,3.,5.,2.,2.5,2.,4.,6.,5.5,4.};
  static char 
       *cpol[6] = {"SPLINE", "STEM", "BARS", "STAIRS", "STEP", "LINEAR"},
       *ctit    = "Interpolation Methods";

  setpag("da4p");
  disini();
  complx();
  pagera();
  incmrk(1);
  hsymbl(25);
  titlin(ctit,1);
  axslen(1500,350);
  setgrf("line","line","line","line");

  for (i=0; i<6; i++)
  { axspos(350,nya-i*350);
    polcrv(cpol[i]);
    marker(0);

    graf(0.,20.,0.,5.,0.,10.,0.,5.);
    nx=nxposn(1.);
    ny=nyposn(8.);
    messag(cpol[i],nx,ny);
    curve(x,y,16);

    if (i == 5)
    { height(50);
      title();
    }
    endgrf();
  }

  disfin();
}


Bar Graphs / C


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

main()
{ int nya = 2700, i;
  static char   *ctit = "Bar Graphs (BARS)", cbuf[25];

  static float x[9]  = {1.,2.,3.,4.,5.,6.,7.,8.,9.},
               y[9]  = {0.,0.,0.,0.,0.,0.,0.,0.,0.},
               y1[9] = {1.,1.5,2.5,1.3,2.0,1.2,0.7,1.4,1.1},
               y2[9] = {2.,2.7,3.5,2.1,3.2,1.9,2.0,2.3,1.8},
               y3[9] = {4.,3.5,4.5,3.7,4.,2.9,3.0,3.2,2.6};

  setpag("da4p");
  disini();
  pagera();
  complx();
  ticks(1,"x");
  intax();;
  axslen(1600,700);
  titlin(ctit,3);

  legini(cbuf,3,8);
  leglin(cbuf,"FIRST",1);
  leglin(cbuf,"SECOND",2);
  leglin(cbuf,"THIRD",3);
  legtit(" ");
  shdpat(5L);
  for (i = 1; i <= 3; i++)
  { if (i >  1) labels("none","x");
    axspos(300,nya-(i-1)*800);

    graf(0.,10.,0.,1.,0.,5.,0.,1.);

    if (i == 1)
    { bargrp(3,0.15);
      color("red");
      bars(x,y,y1,9);
      color("green");
      bars(x,y,y2,9);
      color("blue");
      bars(x,y,y3,9);
      color("fore");
      reset("bargrp");
    }
    else if (i == 2)
    { height(30);
      labels("delta","bars");
      labpos("center","bars");
      color("red");
      bars(x,y,y1,9);
      color("green");
      bars(x,y1,y2,9);
      color("blue");
      bars(x,y2,y3,9);
      color("fore");
      reset("height"); 
    }
    else if (i == 3)
    { labels("second","bars");
      labpos("outside","bars");
      color("red");
      bars(x,y,y1,9);
      color("fore");
    }

    if (i != 3) legend(cbuf,7);

    if (i == 3)
    { height(50);
      title();
    }

    endgrf();
  }

  disfin();
}


Pie Charts / C

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

main()
{ int nya = 2800, i;
  static char    *ctit = "Pie Charts (PIEGRF)", cbuf[41];
  static float xray[5] = {1.,2.5,2.,2.7,1.8};

  setpag("da4p");
  disini();
  pagera();
  complx();
  axslen(1600,1000);
  titlin(ctit,2);
  chnpie("both");

  legini(cbuf,5,8);
  leglin(cbuf,"FIRST",1);
  leglin(cbuf,"SECOND",2);
  leglin(cbuf,"THIRD",3);
  leglin(cbuf,"FOURTH",4);
  leglin(cbuf,"FIFTH",5);

  patcyc(1,7L);
  patcyc(2,4L);
  patcyc(3,13L);
  patcyc(4,3L);
  patcyc(5,5L);

  for (i = 0; i < 2; i++)
  { axspos(250,nya-i*1200);
    if (i == 1)
    { labels("data","pie");
      labpos("external","pie");
    }

    piegrf(cbuf,1,xray,5);

    if (i == 1)
    { height(50);
      title();
    }
    endgrf();
  }
  disfin();
}


3-D Bar Graph / 3-D Pie Chart / C

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

main()
{ char cbuf[80];
  float xray[5]  = {2.,4.,6.,8.,10.},
        y1ray[5] = {0.,0.,0.,0.,0.},
        y2ray[5] = {3.2,1.5,2.0,1.0,3.0};

  int ic1ray[5]  = {50,150,100,200,175},
      ic2ray[5]  = {50,150,100,200,175};

  setpag("da4p");
  disini();
  pagera();
  hwfont();

  titlin("3-D Bar Graph / 3-D Pie Chart", 2);
  htitle(40);

  shdpat(16);
  axslen(1500,1000);
  axspos(300,1400);

  barwth(0.5);
  bartyp("3dvert");
  labels("second","bars");
  labpos("outside","bars");
  labclr(255,"bars");
  graf(0.,12.,0.,2.,0.,5.,0.,1.);
  title();
  color("red");
  bars(xray,y1ray,y2ray,5);
  endgrf();

  shdpat(16);
  labels("data","pie");
  labclr(255,"pie");
  chnpie("none");
  pieclr(ic1ray,ic2ray,5);
  pietyp("3d");
  axspos(300,2700);
  piegrf(cbuf,0,y2ray,5);       
  disfin();
}


Shading Patterns / C

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

main()
{ int   ixp[4], iyp[4], nl, nx, nx0 = 335, ny0 = 350, ny, i, j, ii, k, iclr;
  static int  ix[4] = {0, 300, 300, 0},
              iy[4] = {0, 0, 400, 400};
  static char *ctit = "Shading Patterns (AREAF)", cstr[3];

  disini();
  pagera();
  complx();
  setvlt("small");

  height(50);
  nl = nlmess(ctit);
  nx = (2970-nl)/2;
  messag(ctit,nx,200);

  iclr = 0;
  for (i = 0; i < 3; i++)
  { ny = ny0+i*600;
    for (j = 0; j < 6; j++)
    { nx = nx0+j*400;
      ii = i*6+j;
      shdpat((long) ii);
      sprintf(cstr,"%d",ii);

      iclr = iclr % 16;
      iclr++;
      setclr(iclr);

      for (k=0; k<4; k++)
      { ixp[k] = ix[k] + nx;
        iyp[k] = iy[k] + ny;
      }
      areaf(ixp,iyp,4);

      nl  = nlmess(cstr);
      nx += (300-nl)/2;
      messag(cstr,nx,ny+460);
    }
  }

  disfin();
}


3-D Colour Plot / C

#include <stdio.h>
#include <math.h>
#include "dislin.h"

float zmat[100][100];

main()
{ int n = 100, i, j;
  double   fpi = 3.1415927/180., step, x, y;

  step = 360./(n-1);
  for (i = 0; i < n; i++)
  { x = i*step;
    for (j = 0; j < n; j++)
    { y = j*step;
      zmat[i][j] = 2*sin(x*fpi)*sin(y*fpi);
    }
  }

  disini();
  pagera();
  hwfont();

  titlin("3-D Colour Plot of the Function",2);
  titlin("F(X,Y) = 2 * SIN(X) * SIN(Y)",4);

  name("X-axis","x");
  name("Y-axis","y");
  name("Z-axis","z");

  intax();
  autres(n,n);
  axspos(300,1850);
  ax3len(2200,1400,1400);

  graf3(0.,360.,0.,90.,0.,360.,0.,90.,-2.,2.,-2.,1.);
  crvmat((float *) zmat,n,n,1,1);
  
  height(50);
  title();
  mpaepl(3);
  disfin();
}


Surface Plot / C

#include <stdio.h>
#include <math.h>
#include "dislin.h"

float zmat[50][50];

main()
{ int n = 50 ,i, j;
  double fpi=3.1415927/180., step, x, y;
  static char *ctit1 = "Surface Plot (SURMAT)",
              *ctit2 = "F(X,Y) = 2*SIN(X)*SIN(Y)";

  step = 360./(n-1);
  for (i = 0; i < n; i++)
  { x = i*step;
    for (j = 0; j < n; j++)
    { y = j*step;
      zmat[i][j] = 2*sin(x*fpi)*sin(y*fpi);
    }
  }

  setpag("da4p");
  disini();
  pagera();
  complx();
  axspos(200,2600);
  axslen(1800,1800);

  name("X-axis","x");
  name("Y-axis","y");
  name("Z-axis","z");

  titlin(ctit1,2);
  titlin(ctit2,4);

  view3d(-5.,-5.,4.,"abs");
  graf3d(0.,360.,0.,90.,0.,360.,0.,90.,-3.,3.,-3.,1.);
  height(50);
  title();

  color("green");
  surmat((float *) zmat,50,50,1,1);
  disfin();
}


Contour Plot / C

#include <stdio.h>
#include <math.h>
#include "dislin.h"

main()
{ int n = 50, i, j;
  double   fpi = 3.14159/180., step, x, y;
  float  zlev;

  step = 360./(n-1);

  for (i = 0; i < n; i++)
  { xray[i] = i*step;
    yray[i] = i*step;
  }

  for (i = 0; i < n; i++)
  { for (j = 0; j < n; j++)
    { x = xray[i]*fpi;
      y = yray[j]*fpi;    
      zmat[i][j] = 2*sin(x)*sin(y);
    }
  }

  setpag("da4p");
  disini();
  complx();
  pagera();

  titlin("Contour Plot",1);
  titlin("F(X,Y) = 2 * SIN(X) * SIN(Y)",3);

  name("X-axis","x");
  name("Y-axis","y");

  intax();
  axspos(450,2670);
  graf(0.,360.,0.,90.,0.,360.,0.,90.);

  height(30);
  for (i = 0; i < 9; i++)
  { zlev = -2.+i*0.5;
    setclr ((i+1) * 25);
    if (i == 4)
      labels("none","contur"); 
    else
      labels("float","contur");

    contur(xray,n,yray,n,(float *) zmat,zlev);
  }

  height(50);
  color("fore");
  title();

  disfin();
}


Shaded Contour Plot / C

#include <stdio.h>
#include <math.h>
#include "dislin.h"

float xray[50], yray[50], zmat[50][50];

main()
{ int n = 50, i, j;
  double step, x, y;
  float zlev[12];

  step = 1.6/(n-1);
  for (i = 0; i < n; i++)
  { x = 0.0+i*step;
    xray[i] = x;
    for (j = 0; j < n; j++)
    { y = 0.0+j*step;
      yray[j] = y;
      zmat[i][j] = (x*x-1.)*(x*x-1.) + (y*y-1.)*(y*y-1.);
    }
  }

  setpag("da4p");
  disini();
  pagera();
  complx();

  mixalf();
  titlin("Shaded Contour Plot",1);
  titlin("F(X,Y) = (X[2$ - 1)[2$ + (Y[2$ - 1)[2$",3);
  name("X-axis","x");
  name("Y-axis","y");

  shdmod("poly", "contur");
  axspos(450,2670);
  graf(0.0,1.6,0.0,0.2,0.0,1.6,0.0,0.2);

  for (i = 1; i <= 12; i++)
    zlev[12-i] = 0.1+(i-1)*0.1;

  conshd(xray, n, yray, n, (float *) zmat,zlev,12);

  height(50);
  title();
  disfin();
}


Map Plot / C

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

main()
{ 
  disini();
  pagera();
  complx();

  frame(3);
  axspos(400,1850);
  axslen(2400,1400);

  name("Longitude","x");
  name("Latitude","y");
  titlin("World Coastlines and Lakes",3);

  labels("map","xy");
  grafmp(-180.,180.,-180.,90.,-90.,90.,-90.,30.);

  gridmp(1,1);
  color("green");
  world();
  color("fore");

  height(50);
  title();
  disfin();
}


TeX Instructions for Mathematical Formulas / C

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

main()
{ char cstr[80];
  int nl;
  
  setpag ("da4p");
  disini ();
  pagera ();
  complx ();
  height (40);

  strcpy (cstr, "TeX Instructions for Mathematical Formulas");
  nl = nlmess (cstr);
  messag (cstr, (2100 - nl)/2, 100);
  
  texmod ("on");
  messag ("$\\frac{1}{x+y}$", 150, 400);
  messag ("$\\frac{a^2 - b^2}{a+b} = a - b$", 1200, 400);
  
  messag ("$r = \\sqrt{x^2 + y^2}", 150, 700);
  messag ("$\\cos \\phi = \\frac{x}{\\sqrt{x^2 + y^2}}$", 1200, 700);

  messag ("$\\Gamma(x) = \\int_0^\\infty e^{-t}t^{x-1}dt$", 150, 1000);
  messag ("$\\lim_{x \\to \\infty} (1 + \\frac{1}{x})^x = e$", 1200, 1000);

  messag ("$\\mu = \\sum_{i=1}^n x_i p_i$", 150, 1300);
  messag ("$\\mu = \\int_{-\\infty}^ \\infty x f(x) dx$", 1200, 1300);

  messag ("$\\overline{x} = \\frac{1}{n} \\sum_{i=1}^n x_i$", 150, 1600);
  messag ("$s^2 = \\frac{1}{n-1} \\sum_{i=1}^n (x_i - \\overline{x})^2$",
          1200, 1600);

  messag ("$\\sqrt[n]{\\frac{x^n - y^n}{1 + u^{2n}}}$", 150, 1900);  
  messag ("$\\sqrt[3]{-q + \\sqrt{q^2 + p^3}}$", 1200, 1900);

  messag ("$\\int \\frac{dx}{1+x^2} = \\arctan x + C$", 150, 2200);
  messag ("$\\int \\frac{dx}{\\sqrt{1+x^2}} = {\\rm arsinh} x + C$",
          1200, 2200);

  messag ("$\\overline{P_1P_2} = \\sqrt{(x_2-x_1)^2 + (y_2-y_1)^2}$",
           150,2500);
  messag ("$x = \\frac{x_1 + \\lambda x_2}{1 + \\lambda}$", 1200, 2500);
  disfin ();
}