CLRTEST.C

/****************************************************************************
        (c) 1984 - 2003 by Scientific Endeavors Corporation.
        All rights reserved.

        This program demonstrates a semi-log plot with a legend, a linear plot
        with a window, and a semi-log plot with left and right y axes and
        upright y labels.
****************************************************************************/
#include <graphic.h>                           /* Include all needed files */

#if TCQ                                 /* Set stack for Borland (Turbo) C */
extern unsigned _stklen = 0x3000;
#endif

/* Declare variables here so they don't go on the stack */
static char *xstring[] = {                    /* Strings for X-Axis labels */
        "1984", "1985", "1986", "1987", "1988", "1989"};
static float u1[62] = {                    /* u1 is the Company stock fund */
        6.74f, 7.3f, 8.61f, 9.29f, 9.74f, 10.f, 9.676f, 10.6038f, 11.7344f, 11.3485f,
        13.7248f, 12.7133f, 13.4976f, 15.498f, 16.3235f, 15.8140f, 15.7405f, 17.8188f,
        17.8277f, 18.0340f, 17.734f, 15.2812f, 15.2875f, 16.172f, 16.671f, 15.6087f,
        17.9595f, 20.7653f, 20.9448f, 22.5165f, 22.1078f, 20.95f, 22.9562f, 20.9474f,
        19.1381f, 21.3239f, 18.8562f, 21.7838f, 22.8548f, 24.4238f, 21.9162f, 21.9184f,
        22.967f, 24.9581f, 26.3963f, 27.2202f, 19.4238f, 18.9284f, 20.9032f, 23.4342f,
        23.3657f, 22.2842f, 21.2772f, 20.5066f, 22.5556f, 20.7719f, 19.7649f, 21.7906f,
        22.5653f, 21.3561f, 20.6527f, 21.562f
};
static float u2[62] = {                     /* u2 is the Fixed Income Fund */
        24.9266f, 25.1392f, 25.3568f, 25.5743f, 25.8139f, 26.0681f, 26.3109f, 26.5607f,
        26.8234f, 27.0789f, 27.3422f, 27.6022f, 27.8693f, 28.1191f, 28.3711f, 28.6253f,
        28.8833f, 29.1397f, 29.3996f, 29.6653f, 29.9315f, 30.2079f, 30.4776f, 30.7508f,
        31.047f, 31.3164f, 31.5869f, 31.8565f, 32.1266f, 32.3961f, 32.6705f, 32.95f,
        33.2245f, 33.5053f, 33.7878f, 34.0731f, 34.4021f, 34.6832f, 34.9363f, 35.2175f,
        35.4915f, 35.7733f, 36.0476f, 36.3593f, 36.627f, 36.9042f, 37.1871f, 37.4632f,
        37.7509f, 38.0319f, 38.2865f, 38.5704f, 38.8489f, 39.1397f, 39.4259f, 39.7232f,
        40.0245f, 40.3181f, 40.6301f, 40.932f, 41.2492f, 41.5818f
};
static float u3[62] = {                            /* u3 is the Equity Fund */
        18.26f, 18.2983f, 18.346f, 17.4876f, 17.7345f, 16.9745f, 17.33f, 17.087f,
        18.6332f, 18.7496f, 18.7721f, 18.7327f, 19.4395f, 20.4982f, 20.6580f, 21.0312f,
        21.3133f, 22.2778f, 22.8659f, 22.681f, 22.684f, 22.1318f, 23.2726f, 24.388f,
        25.2836f, 25.6543f, 27.4712f, 29.1251f, 28.4984f, 29.6629f, 30.1036f, 28.98f,
        31.5469f, 29.8584f, 30.4436f, 30.8878f, 30.6971f, 33.8124f, 34.8253f, 35.8124f,
        36.322f, 36.57f, 38.0806f, 38.6662f, 40.3914f, 38.4364f, 31.9274f, 29.2592f,
        31.0548f, 34.7917f, 35.4516f, 34.3014f, 34.6729f, 35.8435f, 38.9324f, 38.7803f,
        37.4419f, 39.6724f, 40.7028f, 40.1896f, 40.1600f, 43.6935f
};

void GPC_MAIN(void)
{
    float t[62];
    double x1a;
    int nxdiv, nydiv, npts;
    float shrs1[62], shrs2[62], shrs3[62], vl1[62], vl2[62], vl3[62];
    float taly1, taly2, taly3;
    int i, inum1, inum1m;
    float *x, *y, *z;

    x = (float *)gpcalloc(301, sizeof(float));
    y = (float *)gpcalloc(301, sizeof(float));
    z = (float *)gpcalloc(301, sizeof(float));
    if (x == (void  *)NULL || y == (void  *)NULL ||
        z == (void  *)NULL) {
        GPC_PUTS("CLRTEST: Could not allocate data arrays");
        goto FreeMem;
    }
    inum1 = 62;                                    /* Generate data points */
    inum1m = inum1-1;
    taly1 = 0.f;
    taly2 = 0.f;
    taly3 = 0.f;
    for(i = 0; i <= inum1m; i++){
        t[i] = i;
        taly1 = taly1 + 1.f / u1[i];
        shrs1[i] = taly1;
        vl1[i] = shrs1[i] * u1[i];

        taly2 = taly2 + 1.f / u2[i];
        shrs2[i] = taly2;
        vl2[i] = shrs2[i] * u2[i];

        taly3 = taly3 + 1.f / u3[i];
        shrs3[i] = taly3;
        vl3[i] = shrs3[i] * u3[i];
    }

    bgnplot(1, 'g', "clr.tkf");                  /* GraphiC initialization */
    startplot(BLACK);
    metricunits(0);                                 /* Plots are in inches */
    font(2, "complex.fnt", '\310', "microb.fnt", '\311');  /* Select fonts */
    PORTRAIT;                                          /* Plot is rotated */

    page(6.884f, 9.0f);                      /* Size of page and plot area */
    area2d(5.5f, 7.6f);

    color(WHITE);
    tcurve(4);                            /* Curves will be 4 pixels thick */
    legpos(3, 2.5f, 2.5f, 1);                               /* Draw legend */
    legend(1, "Company", 1, .14f, 9);
    legend(1, "Fixed Income", 4, .14f, 12);
    legend(1, "Equity Fund", 9, .14f, 13);
    yname("Price ($ per unit)");                   /* Axis and plot titles */
    heading("\311Savings Plan Values\310");
    frame(1,1);                               /* Semi-log axis with frame, */
    grid(2);                                           /* Dotted grid, and */
    fgrid(0,0);
    xlab(1,xstring);                                      /* String labels */
    ylog("", 0.f, 12.f, 59.f, 6.f, 60.f);
    xlab(0,xstring);
    curve((float *)t, (float *)u1, inum1, 0);
    curve((float *)t, (float *)u2, inum1, 0);
    curve((float *)t, (float *)u3, inum1, 0);
    tcurve(0);

    if (endplot())                                 /* Terminate first plot */
        goto EndOfApp;

    startplot(MAGENTA);               /* GraphiC initialization (2nd plot) */
    font(3, "simplex.fnt", '\310', "triplex.fnt", '\311',  /* Select fonts */
        "complex.fnt", '\312');
    LANDSCAPE;                                      /* Non rotated picture */
    page(9.0f, 6.884f);                      /* Size of page and plot area */
    area2d(7.6f, 5.5f);
    gwindow(4.0f, 1.0f, 8.8f, 4.25f);            /* Define area for window */
    color(YELLOW);                                 /* Axis and plot titles */
    xname("\312X axis");
    yname("\312Y axis");
    heading("\311WINDOW SAMPLE");
    npts=301;                                      /* Generate data points */
    for(i = 0; i < npts; i++){
        y[i] = .3f * i;
        x[i] = (y[i] * y[i]) / 2;
    }

    color(BLACK);
    nxdiv=5;                              /* Desired # of x-axis divisions */
    nydiv=6;                              /* Desired # of y-axis divisions */
    frame(0,0);                                          /* Turn off frame */
    fgrid(0,0);                               /* Turn off grid subdivision */
    grid(9);                                /* Fine dot grid at tick marks */
    box();
    scales(nxdiv, nydiv, (float *)x, (float *)y, npts);
    color(BRT_WHITE);
    dashf(6);
    curve((float *)x, (float *)y, npts, 0);             /* Draw curve only */
    endplot();
/*
    The following calls assume that the plot fills the entire screen.
    However, all plot elements will be scaled to fit within the area
    defined in the call to gwindow(). setscale(1) turns this mode on.
*/
    startplot(MAGENTA);/* Start the picture that goes in the windowed area */
    setscale(1);                                   /* Turn on font scaling */
    font(2, "swissbld.fnt", '\310', "triplex.fnt", '\311');/* Select fonts */
    fillfont(1);

    color(BLACK);                            /* Draw black box around plot */
    box();

    color(BLUE);                           /* Plot blue text in the window */
    tmargin(1.75f);
    lmargin(.25f);
    ltline("\310GraphiC \311allows you to leave an", .325f);
    ltline("\311area within a plot clear and", .325f);
    ltline("\311create a new plot in the clear", .325f);
    ltline("\311area.", .325f);

    setscale(0);                                  /* Turn off font scaling */

    if (endplot())                                   /* Terminate 2nd plot */
        goto EndOfApp;

    startplot(BLACK);                 /* GraphiC initialization (3rd plot) */
    font(4, "simplex.fnt", '\310', "triplex.fnt", '\311',  /* Select fonts */
        "complex.fnt", '\312', "compgrma.fnt", '\313');

    page(9.0f, 6.884f);                      /* Size of page and plot area */
    area2d(6.4f, 5.5f);

    color(MAGENTA);                                 /* Draw magenta legend */
    legpos(2, 5.3f, 5.1f, 1);
    legend(1, "Amplitude", 1, .125f, 14);
    legend(1, "Phase", 4, .125f, 12);

    pbox();                                  /* Draw a box around the plot */
    xname("\312Frequency");                                /* x axis label */
    yname("\312Amplitude");                                /* y axis label */
    heading("\311Filter Response");                          /* Plot title */

    color(WHITE);
    nxdiv=10;                             /* Desired # of x axis divisions */
    nydiv=6;                              /* Desired # of y axis divisions */
    grid(2);                              /* Grid lines on the x-axis only */
    fgrid(-1, 5);                   /* Subdivide axes with tick marks only */
    frame(1,0);                       /* Frame the plot with no tick marks */
    rightax(1);                      /* Second y-axis will be on the right */
    upright(1);                         /* y-axis labels are to be upright */
    xlog(0.05f, 10.0f, "%-1.2f", 0.0f, 0.4f, 2.4f);

    npts = 200;                                    /* Generate data points */
    for(i = 1; i <= 200; i++){
        x[i-1] = (float)i / 20.f;
        x1a = x[i-1] * x[i-1];
        y[i-1] = (float)(1. / sqrt(((1. - x1a) * (1. - x1a)) +
            ((.5 * x[i-1]) * (.5 * x[i-1]))));
        if(i == 20)
            z[i-1] = (float)(PI / 2.);
        else
            z[i-1] = (float)-atan(.5 * x[i-1] / (1. - x1a));
        if(z[i-1] > 0)
            z[i-1] -= (float)PI;
    }

    curve((float *)x, (float *)y, npts, 0);             /* Draw curve only */

    color(MAGENTA);
    yname("\312Phase in Radians");              /* Right-hand y axis label */

    color(WHITE);
    frame(0, 0);                                    /* Do not redraw frame */
    fgrid(0, 0);                                  /* Do not subdivide plot */
    xlog(0.05f, 10.0f, "%-1.1f", -3.6f, 0.6f, 0.0f);/* Draw right-hand y-axis */

    curve((float *)x, (float *)z, npts, 0);             /* Draw curve only */

    dateit((Uchar)'\312');                           /* Put date and time on plot */
    endplot();                                       /* Terminate 3rd plot */
EndOfApp:
    stopplot();                                    /* Quit and close files */

FreeMem:
    gpcfree((void **)&x);                           /* free allocated global memory */
    gpcfree((void **)&y);
    gpcfree((void **)&z);
}