
--should these be local and declared per script?
sin, cos, abs, mod, floor, pow, sqrt = math.sin, math.cos, math.abs, math.mod, math.floor, math.pow, math.sqrt
min, max = math.min, math.max
insert = table.insert

math.randomseed( os.time() )
math.random();  math.random();  math.random();	--first few numbers aren't really random :O

random = math.random

PI = 3.141529
PIhalf = .5 * PI
PI2 = 2. * PI

function HSLA(h, s, v, a)
 
  local r, g, b
 
  local i = math.floor(h * 6)
  local f = h * 6 - i
  local p = v * (1 - s)
  local q = v * (1 - f * s)
  local t = v * (1 - (1 - f) * s)
 
  local switch = i % 6
  if switch == 0 then
    r = v g = t b = p
  elseif switch == 1 then
    r = q g = v b = p
  elseif switch == 2 then
    r = p g = v b = t
  elseif switch == 3 then
    r = p g = q b = v
  elseif switch == 4 then
    r = t g = p b = v
  elseif switch == 5 then
    r = v g = p b = q
  end
 Color( r, g, b, a )
 -- return math.floor(r), math.floor(g), math.floor(b)
--	return r, g, b
end

function distance2( v1, v2 )
	
	dis0 = v1[ 1 ] - v2[ 1 ]
	dis1 = v1[ 2 ] - v2[ 2 ]
	return sqrt( dis0 * dis0 + dis1 * dis1 )
	
end

function randint( low, high )
	return low + math.floor( math.random() * ( high + .999 - low ) )
end

function rand( low, high )
	return low + ( math.random() * ( high - low ) )
end

function clamp( val, a, b )
	if ( val < a ) then return a end
	if ( val > b ) then return b end
	return val
end

function normalize2( val )
	low = 0.00001
	local dist = max( low, sqrt( val[ 2 ] * val[ 2 ] + val[ 1 ] * val[ 1 ] ) )
	if ( dist <= low ) then
		val[ 1 ] = low
	end
	nVal = { val[ 1 ] / dist, val[ 2 ] / dist }
	
	return nVal, dist
end

function sign(x)
	if x<0 then
		return -1
	else
		return 1
	end
end
