Here's my Vector2 library:
type tVect2
x as float
y as float
endtype
////////////////////////
// CREATE VECTOR
function CreateVector2( mag as float, angle as float )
local v as tVect2
v.x = sin(angle) * mag
v.y = -cos(angle) * mag
endfunction v
function CreateVectorFromHeading(h as float)
v as tVect2
v.x = sin(h)
v.y = -cos(h)
endfunction v
function CreateUnitVectorFromHeading(h as float)
v as tVect2
v.x = sin(h)
v.y = -cos(h)
NormalizeVector2(v)
endfunction v
////////////////////////
// GET ANGLE FROM VECTOR
function GetHeadingVector2( v as tVect2 )
r# = atan2( v.y, v.x )
endfunction r#
/////////////////////////
// DISTANCE
function GetDistance2v( pos1 as tVect2, pos2 as tVect2)
x# = pos2.x - pos1.x
y# = pos2.y - pos1.y
d# = sqrt( (x#*x#) + (y#*y#) )
endfunction d#
function GetDistance2i( x1, y1, x2, y2 )
x = x2 - x1
y = y2 - y1
d# = sqrt( (x*x) + (y*y) )
endfunction d#
function GetDistance2f( x1#, y1#, x2#, y2# )
x# = x2# - x1#
y# = y2# - y1#
d# = sqrt( (x#*x#) + (y#*y#) )
endfunction d#
function GetSquareDistance2v( pos1 as tVect2, pos2 as tVect2)
x# = pos2.x - pos1.x
y# = pos2.y - pos1.y
d# = (x#*x#) + (y#*y#)
endfunction d#
function GetSquareDistance2i( x1, y1, x2, y2 )
x = x2 - x1
y = y2 - y1
d# = (x*x) + (y*y)
endfunction d#
function GetSquareDistance2f( x1#, y1#, x2#, y2# )
x# = x2# - x1#
y# = y2# - y1#
d# = (x#*x#) + (y#*y#)
endfunction d#
/////////////////////////
// VECTOR LENGTH
function GetVectorLength2( v as tVect2 )
l# = sqrt( (v.x*v.x) + (v.y*v.y) )
endfunction l#
function GetSquareVectorLength2( v as tVect2 )
endfunction (v.x*v.x) + (v.y*v.y)
////////////////////////
// ADD VECTORS
function AddVector2( v1 as tVect2, v2 as tVect2 )
r as tVect2
r.x = v1.x + v2.x
r.y = v1.y + v2.y
endfunction r
function AddPassedVector2( v1 REF as tVect2, v2 as tVect2 )
v1.x = v1.x + v2.x
v1.y = v1.y + v2.y
endfunction
////////////////////////
// SUBTRACT VECTORS
function SubtractVector2( v1 as tVect2, v2 as tVect2 )
r as tVect2
r.x = v1.x - v2.x
r.y = v1.y - v2.y
endfunction r
function SubtractPassedVector2( v1 REF as tVect2, v2 as tVect2 )
v1.x = v1.x-v2.x
v1.y = v1.y-v2.y
endfunction
////////////////////////
// MULTIPLY VECTORS
function MultiplyVector2( v1 as tVect2, n as float )
r as tVect2
r.x = v1.x * n
r.y = v1.y * n
endfunction r
function MultiplyPassedVector2( v REF as tVect2, n as float )
v.x = v.x * n
v.y = v.y * n
endfunction
////////////////////////
// DIVIDE VECTORS
function DivideVector2( v1 as tVect2, n as float )
r as tVect2
r.x = v1.x / n
r.y = v1.y / n
endfunction r
function DividePassedVector2( v REF as tVect2, n as float )
v.x = v.x / n
v.y = v.y / n
endfunction
////////////////////////
// ROTATE VECTORS
function RotateVector2( v as tVect2, a# )
r as tVect2
ca# = Cos(a#)
sa# = Sin(a#)
r.x = ca#*v.x - sa#*v.y
r.y = sa#*v.x + ca#*v.y
endfunction r
function RotatePassedVector2( v REF as tVect2, a# )
ca# = Cos(a#)
sa# = Sin(a#)
x# = ca#*v.x - sa#*v.y
y# = sa#*v.x + ca#*v.y
v.x = x#
v.y = y#
endfunction
////////////////////////
// DOT PRODUCT
function GetDotProduct( v1 as tVect2, v2 as tVect2 )
endfunction (v1.x * v2.x) + (v1.y * v2.y)
////////////////////////
// NORMALIZE VECTOR
function NormalizeVector2( v REF as tVect2 )
l# = GetVectorLength2( v )
v.x = v.x/l#
v.y = v.y/l#
endfunction
////////////////////////
// NEGATE VECTOR
function NegatePassedVector2(v REF as tVect2)
v.x = -v.x
v.y = -v.y
endfunction
function NegateVector2(v as tVect2)
r as tVect2
r.x = -v.x
r.y = -v.y
endfunction r
////////////////////////
// TRUNCATE VECTOR
function TruncatePassedVector2( v REF as tVect2, max# )
v.x = v.x/max#
v.y = v.y/max#
endfunction
function TruncateVector2( v as tVect2, max# )
r as tVect2
r.x = v.x/max#
r.y = v.y/max#
endfunction r
////////////////////////
// ANGLE BETWEEN POINTS
function GetAngleBetweenPoints2( v1 as tVect2, v2 as tVect2 )
SubtractPassedVector2(v2, v1)
r# = atan2(v1.y, v1.x)
endfunction r#
////////////////////////
// RANDOM UNIT VECTOR
function GetRandomUnitVector2()
v as tVect2
v.x = random2(-50,50)
v.y = random2(-50,50)
NormalizeVector2( v )
endfunction v
////////////////////////
// RANDOM VECTOR
function GetRandomVector2()
v as tVect2
v.x = random2(-50,50)
v.y = random2(-50,50)
endfunction v
////////////////////////
// RANDOM UNIT VECTOR IN CIRCLE
function GetRandomUnitVectorInCircle()
v as tVect2
repeat
v.x = random2(-50,50)
v.y = random2(-50,50)
until GetVectorLength2(v) <= 100
NormalizeVector2( v )
endfunction v
////////////////////////
// RANDOM UNIT VECTOR IN ARC
function GetRandomUnitVectorInArc( minA as float, maxA as float )
v as tVect2
v.x = 0
v.y = 1
RotateVector2(v, random2(minA, maxA))
NormalizeVector2( v )
endfunction v
////////////////////////
// VECTOR INTERPOLATION
function LerpVector2( a as tVect2, b as tVect2, t as float )
v as tVect2
v.x = (1-t)*a.x + t*b.x
v.y = (1-t)*a.y + t*b.y
endfunction v
////////////////////////
// VECTOR LIMIT
function SetVector2Limit(v as tVect2, l#)
if GetVectorLength2(v) < l# then exitfunction v
r as tVect2
r = v
NormalizeVector2(r)
MultiplyPassedVector2(r, l#)
endfunction r
function SetPassedVector2Limit(v REF as tVect2, l#)
if GetVectorLength2(v) < l# then exitfunction
NormalizeVector2(v)
MultiplyPassedVector2(v, l#)
endfunction
It might be of some use to you
AGK V2 user - Tier 1 (mostly)