REM ***********************************************
REM Title: Vector Library
REM Author: Phaelax
REM Downloaded from: http://dbcc.zimnox.com/
REM ***********************************************
 
type Vector2D
    x as float
    y as float
endtype
 
type Vector3D
    x as float
    y as float
    z as float
endtype
 
 
 
 
// **********************************************************************************
// **********************************************************************************
//
//                                    2D VECTORS
//
// **********************************************************************************
// **********************************************************************************
 
 
// ==============================================================
// Returns a 2D vector set with [x,y]
// ==============================================================
function makeVector2D(x as float, y as float)
    v as Vector2D
    v.x = x
    v.y = ya 2D vector set with x,y
endfunction v
 
 
// ==============================================================
// Returns a 2D vector of the sum of v1 and v2
// ==============================================================
function addVector2(v1 as Vector2D, v2 as Vector2D)
    v as Vector2D
    v.x = v1.x + v2.x
    v.y = v1.y + v2.y
endfunction v
 
 
// ==============================================================
// Returns a 2D vector of the difference of v2 from v1
// ==============================================================
function subtractVector2(v1 as Vector2D, v2 as Vector2D)
    v as Vector2D
    v.x = v1.x - v2.x
    v.y = v1.y - v2.y
endfunction v
 
 
// ==============================================================
// Returns a 2D vector of the product of v1 and v2
// ==============================================================
function multiplyVector2(v1 as Vector2D, v2 as Vector2D)
    v as Vector2D
    v.x = v1.x * v2.x
    v.y = v1.y * v2.y
endfunction v
 
 
// ==============================================================
// Returns a 2D vector of the quotient of v1 and v2
// ==============================================================
function divideVector2(v1 as Vector2D, v2 as Vector2D)
    v as Vector2D
    v.x = v1.x / v2.x
    v.y = v1.y / v2.y
endfunction v
 
 
// ==============================================================
// Returns the dot product of two vectors
// ==============================================================
function dotProductVector2(v1 as Vector2D, v2 as Vector2D)
    d# = v1.x*v2.x + v1.y*v2.y
endfunction d#
 
 
// ==============================================================
// Returns a 2D vector with an interpolate value from v1
// to v2 using the 'sValue' as step value [0.0-1.0]
// ==============================================================
function linearInterpolateVector2(v1 as Vector2D, v2 as Vector2D, sValue as float)
    v as Vector2D
    v.x = v1.x + (v2.x*v1.x)*sValue
    v.y = v1.y + (v2.y*v1.y)*sValue
endfunction v
 
 
// ==============================================================
// Returns the length (or magnitude) of a 2D vector
// ==============================================================
function getVector2Length(v1 as Vector2D)
    l# = v1.x*v1.x + v1.y*v1.y
    l# = sqrt(l#)
endfunction l#
 
 
// ==============================================================
// Returns a normalized 2D vector (or unit vector) of v1
// ==============================================================
function normalizeVector2(v1 as Vector2D)
    v as Vector2D
    d# = getVector2Length(v1)
    v.x = v1.x / d#
    v.y = v1.y / d#
endfunction v
 
 
// ==============================================================
// Returns the reflected vector of 'I' off of a surface having
// the normal 'N'
// ==============================================================
function reflectVector2(I as Vector2D, N as vector2D)
    r as Vector2D
    d# = dotProductVector2(N, I)
    r.x = 2*N.x*d# - I.x
    r.y = 2*N.y*d# - I.y
endfunction r
 
 
 
// **********************************************************************************
// **********************************************************************************
//
//                                    3D VECTORS
//
// **********************************************************************************
// **********************************************************************************
 
 
// ==============================================================
// Returns a 3D vector set with [x,y,z]
// ==============================================================
function makeVector3(x as float, y as float, z as float)
    v as Vector3D
    v.x = x
    v.y = y
    v.z = z
endfunction v
 
 
// ==============================================================
// Returns a 3D vector of the sum of v1 and v2
// ==============================================================
function addVector3(v1 as Vector3D, v2 as Vector3D)
    v as Vector3D
    v.x = v1.x + v2.x
    v.y = v1.y + v2.y
    v.z = v1.z + v2.z
endfunction v
 
 
// ==============================================================
// Returns a 3D vector of the difference of v2 from v1
// ==============================================================
function subtractVector3(v1 as Vector3D, v2 as Vector3D)
    v as Vector3D
    v.x = v1.x - v2.x
    v.y = v1.y - v2.y
    v.z = v1.z - v2.z
endfunction v
 
 
// ==============================================================
// Returns a 3D vector of the product of v1 and v2
// ==============================================================
function multiplyVector3(v1 as Vector3D, v2 as Vector3D)
    v as Vector3D
    v.x = v1.x * v2.x
    v.y = v1.y * v2.y
    v.z = v1.z * v2.z
endfunction v
 
 
// ==============================================================
// Returns a 3D vector of the quotient of v1 and v2
/ ==============================================================
function divideVector3(v1 as Vector3D, v2 as Vector3D)
    v as Vector3D
    v.x = v1.x / v2.x
    v.y = v1.y / v2.y
    v.z = v1.z / v2.z
endfunction v
 
 
// ==============================================================
// Returns a 3D vector containing the cross product of v1 and v2
// ==============================================================
function crossProductVector3(v1 as Vector3D, v2 as Vector3D)
    v as Vector3D
    v.x = (v1.y * v2.z) - (v1.z * v2.y)
    v.y = (v1.z * v2.x) - (v1.x * v2.z)
    v.z = (v1.x * v2.y) - (v1.y * v2.x)
endfunction v
 
 
// ==============================================================
// Returns the dot product of two vectors
// ==============================================================
function dotProductVector3(v1 as Vector3D, v2 as Vector3D)
    d# = v1.x*v2.x + v1.y*v2.y + v1.z*v2.z
endfunction d#
 
 
// ==============================================================
// Returns a 3D vector with an interpolate value from v1
// to v2 using the 'sValue' as step value [0.0-1.0]
// ==============================================================
function linearInterpolateVector3(v1 as Vector3D, v2 as Vector3D, sValue as float)
    v as Vector3D
    v.x = v1.x + (v2.x*v1.x)*sValue
    v.y = v1.y + (v2.y*v1.y)*sValue
    v.z = v1.z + (v2.z*v1.z)*sValue
endfunction v
 
 
// ==============================================================
// Returns the length (or magnitude) of a 3D vector
// ==============================================================
function getVector3Length(v1 as Vector3D)
    l# = v1.x*v1.x + v1.y*v1.y + v1.z*v1.z
    l# = sqrt(l#)
endfunction l#
 
 
// ==============================================================
// Returns a normalized 3D vector (or unit vector) of v1
// ==============================================================
function normalizeVector3(v1 as Vector3D)
    v as Vector3D
    d# = getVector3Length(v1)
    v.x = v1.x / d#
    v.y = v1.y / d#
    v.z = v1.z / d#
endfunction v
 
 
// ==============================================================
// Returns the reflected vector of 'I' off of a surface having
// the normal 'N'
// ==============================================================
function reflectVector3(I as Vector3D, N as vector3D)
    r as Vector3D
    d# = dotProductVector3(N, I)
    r.x = 2*N.x*d# - I.x
    r.y = 2*N.y*d# - I.y
    r.z = 2*N.z*d# - I.z
endfunction r