WikiMath.h
From Odwiki
/****************************************************************************
File: wikiMath.h
Authors: The od[wiki] community at
http://www.odforce.net/wiki
Description: Basic math functions.
-----------------------------------------------------------------------------
Part of the small but growing od[wiki] library of VEX functions...
This software is placed in the public domain and is provided as is
without express or implied warranty.
*****************************************************************************/
#ifndef wikiMath_h__GUARD
#define wikiMath_h__GUARD
#include <wikiMathConst.h>
//----------------------------------------------------------------------------
// Bias (Perlin)
//----------------------------------------------------------------------------
// Float
float bias(float v,
{
return pow(v, log(
/log(0.5));
}
// Vector
vector biasV(vector v; float
{
return set(bias(v.x,
,bias(v.y,
,bias(v.z,
);
}
// Vector4
vector biasV4(vector4 v; float
{
return set(bias(v.x,
,bias(v.y,
,bias(v.z,
,bias(v.w,
);
}
//----------------------------------------------------------------------------
// Gain (Perlin)
//----------------------------------------------------------------------------
// Float
float gain(float v, g) {
float gg=1.0-g;
return (v<.5)? bias(2.0*v, gg) / 2.0 :
1.0 - bias(2.0 - (2.0*v), gg) / 2.0;
}
// Vector
vector gainV(vector v; float g) {
return set(gain(v.x,g),gain(v.y,g),gain(v.z,g));
}
// Vector4
vector gainV4(vector v; float g) {
return set(gain(v.x,g),gain(v.y,g),gain(v.z,g),gain(v.w,g));
}
//----------------------------------------------------------------------------
// Equality Comparison With Built-in Epsilon
//----------------------------------------------------------------------------
// Float
int equal(float a,
{
float d=abs(a-
;
if(d<=(EPSILON*abs(a)) || d<=(EPSILON*abs(
)) return 1;
return 0;
}
// Vector
int equalV(vector a,
{
return (equal(a.x,b.x) && equal(a.y,b.y) && equal(a.z,b.z));
}
// Vector4
int equalV4(vector4 a,
{
return (equal(a.x,b.x) &&
equal(a.y,b.y) &&
equal(a.z,b.z) &&
equal(a.w,b.w) );
}
//----------------------------------------------------------------------------
// Equality Comparison With User-Supplied Epsilon
//----------------------------------------------------------------------------
// Float
int equalEps(float a,b, e) {
float d=abs(a-
;
if(d<=(e*abs(a)) || d<=(e*abs(
)) return 1;
return 0;
}
// Vector
int equalEpsV(vector a,b; float e) {
return (equalEps(a.x,b.x,e) && equalEps(a.y,b.y,e) && equalEps(a.z,b.z,e));
}
// Vector4
int equalEpsV4(vector4 a,b; float e) {
return (equalEps(a.x,b.x,e) &&
equalEps(a.y,b.y,e) &&
equalEps(a.z,b.z,e) &&
equalEps(a.w,b.w,e) );
}
//----------------------------------------------------------------------------
// Modulo
//----------------------------------------------------------------------------
// Float
float modulo(float a,
{
// Alternatively: return a-(b*floor(a/b));
float rem=a%b;
return rem<0 ? rem+b : rem;
}
// Vector
vector moduloV(vector a; float
{
vector rem=a%b;
return set (
rem.x<0 ? rem.x+b : rem.x,
rem.y<0 ? rem.y+b : rem.y,
rem.z<0 ? rem.z+b : rem.z
);
}
// Vector4
vector4 moduloV4(vector4 a; float
{
vector4 rem=a%b;
return set (
rem.x<0 ? rem.x+b : rem.x,
rem.y<0 ? rem.y+b : rem.y,
rem.z<0 ? rem.z+b : rem.z,
rem.w<0 ? rem.w+b : rem.w
);
}
//----------------------------------------------------------------------------
// Sign
//----------------------------------------------------------------------------
#define SIGN(v) (float)( v < 0. ? -1. : 1. )
#define SIGNI(v) (int)( v < 0 ? -1 : 1 )
float sign(float v) { return v<0.?-1.0:1.0; }
int signI(int v) { return v<0?-1:1; }
//----------------------------------------------------------------------------
// Quadratic Function Solver
//----------------------------------------------------------------------------
void solveQuad(float a,b,c; export float r1,r2) {
float q = b*b - 4*a*c;
if(q<0.) { r1=r2=0.; return; }
q = -.5 * ( b + (b<0 ? -1. : 1.) * sqrt(q) );
r1 = q / a;
r2 = c / q;
}
#endif // End wikiMath_h__GUARD



