0% found this document useful (0 votes)
13 views11 pages

Vector Fields Visualization in C

These programs demonstrate how to generate and plot vector fields in 2D and 3D using the DISLIN library in C code. Program 1 generates a basic (x,y) vector field and plots it. Program 2 generates the vector field (-y,x) and plots it with streamlines. Later programs generate and plot vector fields for more complex functions, add texture mapping, and extend the methods to 3D vector fields.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views11 pages

Vector Fields Visualization in C

These programs demonstrate how to generate and plot vector fields in 2D and 3D using the DISLIN library in C code. Program 1 generates a basic (x,y) vector field and plots it. Program 2 generates the vector field (-y,x) and plots it with streamlines. Later programs generate and plot vector fields for more complex functions, add texture mapping, and extend the methods to 3D vector fields.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Vector Fields (C Code)

Vector Fields - Program 1


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

#define NX 25
#define NY 25

float xv[NX][NY], yv[NX][NY], xp[NX], yp[NY];

main ()
{
int i, j, iret;
double xstep, ystep;

xstep = 2.0 / (NX - 1);;


ystep = 2.0 / (NY - 1);;
for (i = 0; i < NX; i++)
{ xp[i] = (float) (-1. + i * xstep);
for (j = 0; j < NY; j++)
{ yp[j] = (float) (-1. + j *ystep);
xv[i][j] = xp[i];
yv[i][j] = yp[j];
}
}

winsiz (600, 600);


page (2600, 2600);
sclmod ("full");
scrmod ("revers");
metafl ("xwin");

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

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


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

axspos (350, 2300);


axslen (2000, 2000);
titlin ("Vector Field of (x, y)", 4);
graf (-1., 1., -1., 0.2, -1., 1., -1., 0.2);

height (50);
title ();

vecclr (-2);
vecmat ((float *) xv, (float *) yv, NX, NY, xp, yp, -1);
disfin ();
}

Vector Fields - Program 2


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

#define NX 25
#define NY 25

float xv[NX][NY], yv[NX][NY], xp[NX], yp[NY];

main ()
{
int i, j, iret, iclr;
double xstep, ystep;

xstep = 2.0 / (NX - 1);;


ystep = 2.0 / (NY - 1);;
for (i = 0; i < NX; i++)
{ xp[i] = (float) (-1. + i * xstep);
for (j = 0; j < NY; j++)
{ yp[j] = (float) (-1. + j *ystep);
xv[i][j] = -yp[j];
yv[i][j] = xp[i];
}
}

winsiz (600, 600);


page (2600, 2600);
sclmod ("full");
scrmod ("revers");
metafl ("xwin");

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

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


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

axspos (350, 2300);


axslen (2000, 2000);
titlin ("Vector Field of (-y, x)", 4);
graf (-1., 1., -1., 0.2, -1., 1., -1., 0.2);

height (50);
title ();

stmmod ("rk4", "integration");


stmmod ("on", "close");
stmmod ("on", "arrows");
stmval (0.04, "distance");

color ("red");
iclr = intrgb (0.0, 0.0, 0.0);
vecclr (iclr);
stream ((float *) xv, (float *) yv, NX, NY, xp, yp, NULL, NULL, 0);
disfin ();
}

Vector Fields - Program 3


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

#define NX 25
#define NY 25

float xv[NX][NY], yv[NX][NY], xp[NX], yp[NY];

main ()
{ int i, j, iret;
double xstep, ystep, fpi = 3.1415926/180.;

xstep = 360. / (NX - 1);;


ystep = 360. / (NY - 1);;
for (i = 0; i < NX; i++)
{ xp[i] = (float) (-1. + i * xstep);
for (j = 0; j < NY; j++)
{ yp[j] = (float) (-1. + j *ystep);
xv[i][j] = (float) sin (xp[i] * fpi);
yv[i][j] = (float) cos (yp[j] * fpi);
}
}

winsiz (600, 600);


page (2600, 2600);
sclmod ("full");
scrmod ("revers");
metafl ("xwin");

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

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


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

axspos (350, 2300);


axslen (2000, 2000);
titlin ("Vector Field of (sin x, cos y)", 4);
graf (0., 360., 0., 90., 0., 360., 0., 90.);

height (50);
title ();

vecclr (-2);
vecmat ((float *) xv, (float *) yv, NX, NY, xp, yp, -1);
disfin ();
}

Vector Fields - Program 4


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

#define NX 25
#define NY 25

float xv[NX][NY], yv[NX][NY], xp[NX], yp[NY];

main ()
{ int i, j, iret;
double xstep, ystep, fpi = 3.1415926/180.;

xstep = 360. / (NX - 1);;


ystep = 360. / (NY - 1);;
for (i = 0; i < NX; i++)
{ xp[i] = (float) (-1. + i * xstep);
for (j = 0; j < NY; j++)
{ yp[j] = (float) (-1. + j *ystep);
xv[i][j] = (float) sin (xp[i] * fpi);
yv[i][j] = (float) cos (yp[j] * fpi);
}
}

winsiz (600, 600);


page (2600, 2600);
sclmod ("full");
scrmod ("revers");
metafl ("xwin");

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

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


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

axspos (350, 2300);


axslen (2000, 2000);
titlin ("Vector Field of (sin x, cos y)", 4);
graf (0., 360., 0., 90., 0., 360., 0., 90.);

height (50);
title ();
vecclr (-2);
vecmat ((float *) xv, (float *) yv, NX, NY, xp, yp, -1);

stream ((float *) xv, (float *) yv, NX, NY, xp, yp, NULL, NULL, 0);
disfin ();
}

Vector Fields - Program 5


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

#define NX 256
#define NY 256

float xv[NX][NY], yv[NX][NY], wmat[NX][NY];


int itmat[NX][NY], iwmat[NX][NY];

main ()
{ int i, j, iret;
double xstep, ystep, xp, yp;

xstep = 2.0 / (NX - 1);;


ystep = 2.0 / (NY - 1);;
for (i = 0; i < NX; i++)
{ xp = -1. + i * xstep;
for (j = 0; j < NY; j++)
{ yp = -1. + j *ystep;
xv[i][j] = xp;
yv[i][j] = yp;
}
}

winsiz (600, 600);


page (2600, 2600);
sclmod ("full");
scrmod ("revers");
metafl ("xwin");

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

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


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

labdig (-1, "xyz");


axspos (250, 2250);
ax3len (2000, 2000, 2000);
titlin ("Vector Field of (x, y)", 4);

txture ((int *) itmat, NX, NY);


autres (NX, NY);
graf3 (0., 360., 0., 90., 0., 360., 0., 90., 0., 255., 0., 50);

licmod ("on", "scale");


licpts ((float *) xv, (float *) yv, NX, NY, (int *) itmat, (int *) iwmat,
(float *) wmat);
crvmat ((float *) wmat, NX, NY, 1, 1);

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

Vector Fields - Program 6


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

#define NX 256
#define NY 256

float xv[NX][NY], yv[NX][NY], wmat[NX][NY];


int itmat[NX][NY], iwmat[NX][NY];

main ()
{ int i, j, iret;
double xstep, ystep, xp, yp;

xstep = 2.0 / (NX - 1);;


ystep = 2.0 / (NY - 1);;
for (i = 0; i < NX; i++)
{ xp = -1. + i * xstep;
for (j = 0; j < NY; j++)
{ yp = -1. + j *ystep;
xv[i][j] = -yp;
yv[i][j] = xp;
}
}

winsiz (600, 600);


page (2600, 2600);
sclmod ("full");
scrmod ("revers");
metafl ("xwin");

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

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


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

labdig (-1, "xyz");


axspos (250, 2250);
ax3len (2000, 2000, 2000);
titlin ("Vector Field of (-y, x)", 4);

txture ((int *) itmat, NX, NY);


autres (NX, NY);
graf3 (0., 360., 0., 90., 0., 360., 0., 90., 0., 255., 0., 50);

licmod ("on", "scale");


licpts ((float *) xv, (float *) yv, NX, NY, (int *) itmat, (int *) iwmat,
(float *) wmat);
crvmat ((float *) wmat, NX, NY, 1, 1);

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

Vector Fields - Program 7


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

#define NX 256
#define NY 256

float xv[NX][NY], yv[NX][NY], wmat[NX][NY];


int itmat[NX][NY], iwmat[NX][NY];

main ()
{ int i, j, iret;
double xstep, ystep, fpi = 3.1415926/180., xp, yp;

xstep = 360. / (NX - 1);;


ystep = 360. / (NY - 1);;
for (i = 0; i < NX; i++)
{ xp = -1. + i * xstep;
for (j = 0; j < NY; j++)
{ yp = -1. + j *ystep;
xv[i][j] = (float) sin (xp * fpi);
yv[i][j] = (float) cos (yp * fpi);
}
}

winsiz (600, 600);


page (2600, 2600);
sclmod ("full");
scrmod ("revers");
metafl ("xwin");

disini ();
pagera ();
hwfont ();
setvlt ("rgrey");
name ("X-axis", "x");
name ("Y-axis", "y");

labdig (-1, "xyz");


axspos (250, 2250);
ax3len (2000, 2000, 2000);
titlin ("Vector Field of (sin x, cos y)", 4);

txture ((int *) itmat, NX, NY);


autres (NX, NY);
graf3 (0., 360., 0., 90., 0., 360., 0., 90., 0., 255., 0., 50);

licpts ((float *) xv, (float *) yv, NX, NY, (int *) itmat, (int *) iwmat,
(float *) wmat);
crvmat ((float *) wmat, NX, NY, 1, 1);
height (50);
title ();
disfin ();
}

Vector Fields - Program 8


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

#define NX 256
#define NY 256

float xv[NX][NY], yv[NX][NY], wmat[NX][NY];


int itmat[NX][NY], iwmat[NX][NY];

main ()
{ int i, j, iret;
double xstep, ystep, fpi = 3.1415926/180., xp, yp;

xstep = 360. / (NX - 1);;


ystep = 360. / (NY - 1);;
for (i = 0; i < NX; i++)
{ xp = -1. + i * xstep;
for (j = 0; j < NY; j++)
{ yp = -1. + j *ystep;
xv[i][j] = (float) sin (xp * fpi);
yv[i][j] = (float) cos (yp * fpi);
}
}

winsiz (600, 600);


page (2600, 2600);
sclmod ("full");
scrmod ("revers");
metafl ("xwin");
disini ();
pagera ();
hwfont ();

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


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

labdig (-1, "xyz");


axspos (250, 2250);
ax3len (2000, 2000, 2000);
titlin ("Vector Field of (sin x, cos y)", 4);

txture ((int *) itmat, NX, NY);


autres (NX, NY);
graf3 (0., 360., 0., 90., 0., 360., 0., 90., 0., 255., 0., 50);

licmod ("on", "scale");


licpts ((float *) xv, (float *) yv, NX, NY, (int *) itmat, (int *) iwmat,
(float *) wmat);
crvmat ((float *) wmat, NX, NY, 1, 1);
height (50);
title ();
disfin ();
}

Vector Fields - Program 9


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

#define NX 10
#define NY 10
#define NZ 10

float xv[NX][NY][NZ], yv[NX][NY][NZ], zv[NX][NY][NZ], xp[NX], yp[NY], zp[NZ];

main ()
{ int i, j, k;
double xstep, ystep, zstep;

xstep = 2.0 / (NX - 1);;


ystep = 2.0 / (NY - 1);;
zstep = 2.0 / (NZ - 1);;

for (i = 0; i < NX; i++)


{ xp[i] = (float) (-1. + i * xstep);
for (j = 0; j < NY; j++)
{ yp[j] = (float) (-1. + j * ystep);
for (k = 0; k < NZ; k++)
{ zp[k] = (float) (-1. + k * zstep);
xv[i][j][k] = xp[i];
yv[i][j][k] = yp[j];
zv[i][j][k] = zp[k];
}
}
}

winsiz (600, 600);


page (2600, 2600);
sclmod ("full");
scrmod ("revers");
metafl ("cons");
disini ();
pagera ();
hwfont ();

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


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

axspos (400, 2300);


axslen (2000, 2000);
titlin ("Vector Field of (x, y, z)", 4);

view3d (2., -5., 3., "ABS");


graf3d (-1., 1., -1., 0.2, -1., 1., -1., 0.2, -1., 1., -1., 0.2);
box3d ();

height (50);
title ();

vecclr (-2);
vecmat3d ((float *) xv, (float *) yv, (float *) zv, NX, NY, NZ,
xp, yp, zp, -1);
disfin ();
}

Vector Fields - Program 10


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

#define NX 10
#define NY 10
#define NZ 10

float xv[NX][NY][NZ], yv[NX][NY][NZ], zv[NX][NY][NZ], xp[NX], yp[NY], zp[NZ];

main ()
{ int i, j, k;
double xstep, ystep, zstep;

xstep = 2.0 / (NX - 1);;


ystep = 2.0 / (NY - 1);;
zstep = 2.0 / (NZ - 1);;

for (i = 0; i < NX; i++)


{ xp[i] = (float) (-1. + i * xstep);
for (j = 0; j < NY; j++)
{ yp[j] = (float) (-1. + j * ystep);
for (k = 0; k < NZ; k++)
{ zp[k] = (float) (-1. + k * zstep);
xv[i][j][k] = xp[i];
yv[i][j][k] = yp[j];
zv[i][j][k] = zp[k];
}
}
}

winsiz (600, 600);


page (2600, 2600);
sclmod ("full");
scrmod ("revers");
metafl ("cons");
disini ();
pagera ();
hwfont ();

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


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

axspos (400, 2300);


axslen (2000, 2000);
titlin ("Vector Field of (x, y, z)", 4);

view3d (2., -5., 3., "ABS");


graf3d (-1., 1., -1., 0.2, -1., 1., -1., 0.2, -1., 1., -1., 0.2);
box3d ();

height (50);
title ();

vecmat3d ((float *) xv, (float *) yv, (float *) zv, NX, NY, NZ,
xp, yp, zp, -1);
color ("red");

stmval (0.04, "distance");


stream3d ((float *) xv, (float *) yv, (float *) zv, NX, NY, NZ,
xp, yp, zp, NULL, NULL, NULL, 0);
disfin ();
}

Vector Fields - Program 11


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

#define NX 10
#define NY 10
#define NZ 10

float xv[NX][NY][NZ], yv[NX][NY][NZ], zv[NX][NY][NZ], xp[NX], yp[NY], zp[NZ];

main ()
{ int i, j, k;
double xstep, ystep, zstep;

xstep = 2.0 / (NX - 1);;


ystep = 2.0 / (NY - 1);;
zstep = 2.0 / (NZ - 1);;

for (i = 0; i < NX; i++)


{ xp[i] = (float) (-1. + i * xstep);
for (j = 0; j < NY; j++)
{ yp[j] = (float) (-1. + j * ystep);
for (k = 0; k < NZ; k++)
{ zp[k] = (float) (-1. + k * zstep);
xv[i][j][k] = -yp[j];
yv[i][j][k] = xp[i];
zv[i][j][k] = 0.;
}
}
}

winsiz (600, 600);


page (2600, 2600);
sclmod ("full");
scrmod ("revers");
metafl ("cons");
disini ();
pagera ();
hwfont ();

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


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

axspos (400, 2300);


axslen (2000, 2000);
titlin ("Vector Field of (-y, x, 0)", 4);

view3d (2., -5., 3., "ABS");


graf3d (-1., 1., -1., 0.2, -1., 1., -1., 0.2, -1., 1., -1., 0.2);
box3d ();

height (50);
title ();

stmmod ("on", "close");


stmmod ("rk4", "integration");
stmval (0.04, "distance");
stmmod ("on", "arrows");
vecclr (254);

stream3d ((float *) xv, (float *) yv, (float *) zv, NX, NY, NZ,
xp, yp, zp, NULL, NULL, NULL, 0);
disfin ();
}

Vector Fields - Program 12


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

#define NX 10
#define NY 10
#define NZ 10

float xv[NX][NY][NZ], yv[NX][NY][NZ], zv[NX][NY][NZ], xp[NX], yp[NY], zp[NZ];


float xs[10], ys[10], zs[10];

main ()
{ int i, j, k;
double xstep, ystep, zstep;

xstep = 2.0 / (NX - 1);;


ystep = 2.0 / (NY - 1);;
zstep = 2.0 / (NZ - 1);;

for (i = 0; i < NX; i++)


{ xp[i] = (float) (-1. + i * xstep);
for (j = 0; j < NY; j++)
{ yp[j] = (float) (-1. + j * ystep);
for (k = 0; k < NZ; k++)
{ zp[k] = (float) (-1. + k * zstep);
xv[i][j][k] = -yp[j];
yv[i][j][k] = xp[i];
zv[i][j][k] = 0.;
}
}
}

winsiz (600, 600);


page (2600, 2600);
sclmod ("full");
scrmod ("revers");
metafl ("cons");
disini ();
pagera ();
hwfont ();

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


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

axspos (400, 2300);


axslen (2000, 2000);
titlin ("Some Streamlines of (-y, x, 0.)", 4);

light ("on");
shdmod ("smooth", "surface");
litop3(1,0.5,0.5,0.5,"ambient");

view3d (2., -5., 3., "ABS");


graf3d (-1., 1., -1., 0.2, -1., 1., -1., 0.2, -1., 1., -1., 0.2);
box3d ();

height (50);
title ();

stmmod ("on", "close");


stmmod ("rk4", "integration");
stmmod ("on", "arrows");
stmopt (-2, "arrow");

for (i = 0; i < 10; i++)


{ xs[i] = -0.5;
ys[i] = -0.5;
zs[i] = -0.9 + i * 0.2;
}

matop3 (0.0, 1.0, 0.0, "diffuse");


stream3d ((float *) xv, (float *) yv, (float *) zv, NX, NY, NZ,
xp, yp, zp, xs, ys, zs, 10);
disfin ();
}

You might also like