_TITLE "Vector Math" ' B+ started 2019-10-20 ' Based on notes provided to QB64 forum by William F Barnes, on 2019-10-19
' https://www.qb64.org/forum/index.php?topic=1782.0
' A vector's dimension is the number of components it has.
' Here is code for processing 2 and 3 dimension vectors.
' notation 0 w/arrowHat (no way of telling if 2, 3 or more dimensions)
v2zero.x = 0: v2zero.y = 0
v3zero.x = 0: v3zero.y = 0: v3zero.z = 0
'Basis Vectors, isolate components e sub x Dot V w/arrowHat = V sub x
v2e(1).x = 1: v2e(1).y = 0
v2e(2).x = 0: v2e(2).y = 1
v3e(1).x = 1: v3e(1).y = 0: v3e(1).z = 0
v3e(2).x = 0: v3e(2).y = 1: v3e(2).z = 0
v3e(3).x = 0: v3e(3).y = 0: v3e(3).z = 1
'==================================================================================== test area
'define a test vector
A.x = 1: A.y = 2: A.z = 3
B.x = -10: B.y = -20: B.z = -30
PRINT "A w/arrowHat = " + v3$
(A
) PRINT "B w/arrowHat = " + v3$
(B
) v3Add A, B, Test
PRINT "A + B = " + v3$
(Test
) v3Subtr A, B, Test
PRINT "A - B = " + v3
(Test
) v3Scale 10, A, Test
PRINT "10A = " + v3$
(Test
) v3Inverse A, Inv
PRINT "A Inverse = " + v3$
(Inv
) v3Add A, Inv, Test
PRINT "Check Inverse: A v3Add Inv = " + v3$
(Test
) PRINT "A's magnitude is "; v3Magnitude
(A
) v3Unit A, Test
PRINT "A's unit vector is "; v3$
(Test
) 'isolate y component of test
Test.x = 10: Test.y = -101.11: Test.z = 22.37
PRINT "Test = " + v3$
(Test
) yComponent = v3DotProduct(v3e(2), Test)
PRINT "Test isolate component y = v3DotProduct(v3e(2), Test) = " + ts$
(yComponent
) 'yeah it worked!" setV3 1, 0, 0, A
setV3 0, 1, 0, B
v3CrossProduct A, B, Test
PRINT "A Cross B = " + v3$
(Test
) ' = [0, 0, 1] ?
'================================================= subs and fuctions
SUB setV3
(x
, y
, z
, setMe
AS xyzType
) setMe.x = x: setMe.y = y: setMe.z = z
v3$ = "[" + ts$(showMeInnards.x) + ", " + ts$(showMeInnards.y) + ", " + ts$(showMeInnards.z) + "]"
'notation UppercaseLetter w/arrowhat + uppercase Letter w/arrowHat
SUB v2Add
(A
AS xyType
, B
AS xyType
, Sum
AS xyType
) Sum.x = A.x + B.x
Sum.y = A.y + B.y
SUB v3Add
(A
AS xyzType
, B
AS xyzType
, Sum
AS xyzType
) Sum.x = A.x + B.x
Sum.y = A.y + B.y
Sum.z = A.z + B.z
'notation UppercaseLetter w/arrowHat - UppercaseLetter w/arrowHat
SUB v2Subtr
(A
AS xyType
, B
AS xyType
, Sum
AS xyType
) Sum.x = A.x - B.x
Sum.y = A.y - B.y
SUB v3Subtr
(A
AS xyzType
, B
AS xyzType
, Sum
AS xyzType
) Sum.x = A.x - B.x
Sum.y = A.y - B.y
Sum.z = A.z - B.z
'notation lowercaseletter (for a number next to (times)) UppercaseLetter w/arrowHat
Scale.x = mult * A.x
Scale.y = mult * A.y
Scale.x = mult * A.x
Scale.y = mult * A.y
Scale.z = mult * A.z
'notation the inverse of A w/arrowHat is -A w/arrowHat
SUB v2Inverse
(A
AS xyType
, Inverse
AS xyType
) ' A + InverseOfA = 0 Inverse.x = -A.x
Inverse.y = -A.y
SUB v3Inverse
(A
AS xyzType
, Inverse
AS xyzType
) ' A + InverseOfA = 0 Inverse.x = -A.x
Inverse.y = -A.y
Inverse.z = -A.z
'notation: A w/arrowHat Dot B w/arrowHat v2 Dot Product is a number, v3 Dot Product is a vector
FUNCTION v2DotProduct
(A
AS xyType
, B
AS xyType
) 'shadow or projection if A Dot B = 0 then A , B are perpendicular v2DotProduct = A.x * B.x + A.y * B.y
FUNCTION v3DotProduct
(A
AS xyzType
, B
AS xyzType
) 'shadow or projection if A Dot B = 0 then A , B are perpendicular v3DotProduct = A.x * B.x + A.y * B.y + A.z * B.z
'notation absolute value bars about A w/arrowHat OR just an UppercaseLetter (with no hat), its just a number
FUNCTION v2Magnitude
(A
AS xyType
) 'hypotenuse of right triangle v2Magnitude
= SQR(v2DotProduct
(A
, A
))FUNCTION v3Magnitude
(A
AS xyzType
) 'hypotenuse of cube v3Magnitude
= SQR(v3DotProduct
(A
, A
))
'notation: A w/arrowHat X B w/arrowHat, X is a Cross get it?
FUNCTION v2CrossProduct
(A
AS xyType
, B
AS xyType
) ' a vector perpendicular to both A and B, v2 is a magnitude v2CrossProduct = A.x * B.y - A.y * B.x
SUB v3CrossProduct
(A
AS xyzType
, B
AS xyzType
, Cross
AS xyzType
) ' v3 cross product is a 3d vector perpendicular to A and B 'notice x has no x components, y no y componets, z no z components
Cross.x = A.y * B.z - A.z * B.y
Cross.y = A.z * B.x - A.x * B.z
Cross.z = A.x * B.y - A.y * B.x
'notation: A w/caratHat = A w/arrowHat divided by A (UppercaseLetter) or scaled by 1/A magnitude (no hats)
SUB v2Unit
(A
AS xyType
, Unit
AS xyType
) m = v2Magnitude(A)
v2Scale 1 / m, A, Unit
SUB v3Unit
(A
AS xyzType
, Unit
AS xyzType
) m = v3Magnitude(A)
v3Scale 1 / m, A, Unit