Vector Structure (KerboScript) - griderd/Jebnix GitHub Wiki
A Vector structure consists of an (X, Y, Z) 3-tuple, defining a vector in 3D space. Like all vectors, it has direction and magnitude, the direction being the position (X, Y, Z) from the origin, and the magnitude being the square root of the sum of the squares of each element.
Constructors
V()
An empty constructor creates a zero-vector -- that is, a vector with zero magnitude and an undefined direction. All elements are set to 0.
set vector to V().
print vector.
print vector:mag.
// OUTPUT:
// V(0,0,0)
// 0
V(x, y, z)
This constructor sets X, Y, and Z to the values given.
set vector to V(1, 2, 3).
print vector.
print vector:mag.
// OUTPUT:
// V(1,2,3)
// 3.872983346
Properties
X
This value represents the X component of the vector. It is a floating-point.
Y
This value represents the Y component of the vector. It is a floating-point.
Z
This value represents the Z component of the vector. It is a floating-point.
MAG
This value represents the magnitude of the vector. It is a floating-point.
NORMALIZED
This value represents the vector with a magnitude of 1. The direction remains the same.
Instance Methods
DOTPRODUCT(vector)
Performs a dot product operation on the instance and the provided vector. This is significantly faster than performing dot product the long way.
set v to V(5,6,12).
set u to V(3,4,1).
print v:dotproduct(u).
// OUTPUT:
// 51
ANGLEBETWEEN(vector)
Calculates the angle in degrees between the instance and the provided vector. This is significantly faster than performing the calculation the long way.
set v to V(5,6,12).
set u to V(3,4,1).
print v:anglebetween(u).
// OUTPUT:
// 45.68804549
Operators
Vector * Vector
Performs element-wise multiplication of vectors. To perform dot-product, use the DOTPRODUCT() method.
set v to V(5,6,12).
set u to V(3,4,1).
print v * u.
// OUTPUT:
// V(15,24,12).
Float * Vector
Performs scalar multiplication of the vector.
set v * V(5,6,12).
print v * 2.
// OUTPUT:
// V(10,12,24).
Vector + Vector
Performs element-wise vector addition.
set v to V(5,6,12).
set u to V(3,4,1).
print v + u.
// OUTPUT:
// V(8,10,13).
Vector - Vector
Performs element-wise vector subtraction. Remember that subtracting vectors V - U creates a vector pointing from U to V.
set v to V(5,6,12).
set u to V(3,4,1).
print v - u.
// OUTPUT:
// V(2,2,11).
Behavior Notes
When the value is printed, it is converted to a string. In this case, it is converted into "V(X,Y,Z)" format.