home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Boldly Go Collection
/
version40.iso
/
TS
/
17A
/
DRWIN101.ZIP
/
LINUTIL.C
< prev
next >
Wrap
C/C++ Source or Header
|
1991-07-23
|
4KB
|
154 lines
#include "linutil.h"
vec3 zero3={0.0,0.0,0.0}; /*zero vector, duh?*/
bas3 eucl3={ /*euclidean basis*/
{1.0,0.0,0.0}, /*e0*/
{0.0,1.0,0.0}, /*e1*/
{0.0,0.0,1.0} /*e2*/
};
double *vset(vec3 u,double a,double b,double c) /*sets vector's components*/
{
u[0]=a;
u[1]=b;
u[2]=c;
return u;
} /*vset*/
double dot(vec3 u,vec3 v) /*returns scalar dot product*/
{
return u[0]*v[0]+u[1]*v[1]+u[2]*v[2]; /*u.v*/
} /*dot*/
double *vadd(vec3 u,vec3 v,vec3 w) /*add two vectors*/
{
w[0]=u[0]+v[0];
w[1]=u[1]+v[1];
w[2]=u[2]+v[2];
return w;
} /*vadd*/
double *vsub(vec3 u,vec3 v,vec3 w) /*subtract two vectors*/
{
w[0]=u[0]-v[0];
w[1]=u[1]-v[1];
w[2]=u[2]-v[2];
return w;
} /*vsub*/
double *vneg(vec3 u,vec3 v) /*negate vector*/
{
v[0]=-u[0];
v[1]=-u[1];
v[2]=-u[2];
return v;
} /*vneg*/
double *vmul(double a,vec3 u,vec3 v) /*multiply vector by scalar*/
{
v[0]=a*u[0];
v[1]=a*u[1];
v[2]=a*u[2];
return v;
} /*vmul*/
vec3 *rotb(double deg,int axis,bas3 b) /*rotates basis around axis*/
{
int a1,a2; /*other two axes*/
double t,s,c; /*temp, sine, cosine*/
deg/=180.0; /*turn into radians*/
deg*=PI;
s=sin(deg); /*get sine/cosine*/
c=cos(deg);
if (axis==1) { a1=2; a2=3; } /*set up other two axes*/
else if (axis==2) { a1=3; a2=1; }
else { axis=3; a1=1; a2=2; }
t=b[a1][0];
b[a1][0]=c*b[a1][0]+s*b[a2][0];
b[a2][0]=c*b[a2][0]-s*t;
t=b[a1][1];
b[a1][1]=c*b[a1][1]+s*b[a2][1];
b[a2][1]=c*b[a2][1]-s*t;
t=b[a1][2];
b[a1][2]=c*b[a1][2]+s*b[a2][2];
b[a2][2]=c*b[a2][2]-s*t;
return b;
} /*rotb*/
double *vdotb(vec3 v,bas3 b,vec3 d) /*dots v against each vector in b*/
{
d[0]=DOT(v,b[0]);
d[1]=DOT(v,b[1]);
d[2]=DOT(v,b[2]);
return d;
} /*vdotb*/
int vangles(vec3 r,double *a1,double *a2,bas3 b)
{
vec3 dotv; /*holds dots with basis vectors*/
vdotb(r,b,dotv); /*get dots with each basis vector*/
if (dotv[0]==0.0) { /*if in plane of forward looking..*/
*a1=PI/2.0; /*..set each to*/
*a2=PI/2.0; /*..90 degrees*/
}
else { /*otherwise..*/
*a1=atan(dotv[1]/dotv[0]); /*..get angle to y-axis*/
*a2=atan(dotv[2]/dotv[0]); /*..and z-axis*/
} /*else not in 0-plane*/
return (dotv[0]>0); /*return whether in front of you*/
} /*vangles*/
void vcross(vec3 u,vec3 v,vec3 w)
{
w[0]=u[1]*v[2]-u[2]*v[1];
w[1]=u[2]*v[0]-u[0]*v[2];
w[2]=u[0]*v[1]-u[1]*v[0];
} /*vcross*/
void vnormalize(vec3 v)
{
double d;
d=NORM(v);
if (d>0.0) VMUL(1.0/d,v,v);
} /*vnormalize*/
void vlookback(vec3 p,vec3 z,bas3 b)
{
double pnorm;
double psqr;
vec3 u;
psqr=DOT(p,p); /*length of p squared*/
pnorm=sqrt(psqr); /*norm (length) of p*/
if (pnorm<=0) /*if nul vector..*/
EQU(b,eucl3); /*..use euclidean*/
else { /*otherwise..*/
psqr=DOT(z,p)/psqr; /*..get dot product with zenith*/
VMUL(psqr,p,u); /*..put dot with zenith in u*/
VSUB(z,u,b[2]); /*..put projection onto z in basis[2]*/
VNEG(p,b[0]); /*..put rest of p into basis[0]*/
vcross(b[2],b[0],b[1]); /*..get third via cross product*/
vnormalize(b[0]); /*..normalize*/
vnormalize(b[1]); /*..normalize*/
vnormalize(b[2]); /*..normalize*/
} /*else need to calc*/
} /*vlookback*/