VFIELD.C

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

    This test program illustrates the use of the vector() function to
    create a vector field. It models the magnetic field of two parallel
    conductors carrying equal currents in opposite directions.
****************************************************************************/

#include <graphic.h>                           /* Include all needed files */

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

/****************************************************************************
    Main program
****************************************************************************/
void GPC_MAIN(void)
{
    float r1s, r2s, x, x_comp, y, y_comp;
    float temp, temp2;
    float d = 2.f;                      /* HalfÄwidth of conductor spacing */
    float fact = 0.2f;                    /* Scaling factor for I=1 ampere */
                                         /* Gives H mag. in plot units */

    bgnplot(1, 'g', "vfield.tkf");               /* Graphic initialization */
    startplot(WHITE);
    metricunits(0);                                 /* Plots are in inches */
    font(2, "simplex.fnt", '\310', "triplex.fnt", '\311'); /* Select fonts */

    page(9.0f, 6.884f);                      /* Size of page and plot area */
    area2d(7.6f, 5.5f);
    color(RED);                                /* Red axis and plot titles */
    xname("\310x");
    yname("\310y");
    tfont(5);                         /* Set thickness for bold characters */
    heading("\311#H# Field - 2 Parallel Wires");          /* The H is bold */
    color(BLACK);                                         /* Axes in black */
    graf("%-1.1f", - 4.f, 1.f, 4.f, "%-1.1f", 0.f, 1.f, 6.f);
/* One of the conductors is at -d,3, the other is at +d,3. Points at
the base of the vectors are on a grid with half-integer spacing. */

    color(BLUE);                                        /* Vectors in blue */
    symbol(d, 3.f, 12);
    symbol( -d, 3.f, 12);
    for (y = 0; y <= 6.f; y += .5f) {                   /* Set up the grid */
        for (x = -4.f; x <= 4.f; x += 0.5f) {
            temp = x + d;        /* Calculate the distances from the conds */
            temp2 = y - 3.f;
            r1s = temp * temp + temp2 * temp2;
            if (r1s < .1f)                           /* Don't divide by 0! */
                continue;
            temp = x - d;
            r2s = temp * temp + temp2 * temp2;
            if (r2s < .1f)
                continue;
            x_comp = (y - 3.f) * (1 / r2s - 1 / r1s);    /* Get components */
            x_comp *= fact;           /* Factor for 1 ampere in conductors */
            y_comp = (x + d) / r1s - (x - d) / r2s;
            y_comp *= fact;
            uvector(x, y, x + x_comp, y + y_comp, 2.f, .06f, "01");/* Vector */
        }
    }

    endplot();                                 /* GraphiC closing routines */
    stopplot();
}