This example demonstrates 3D Vector mathematics using the built-in TVector type.
// This example demonstrates 3D Vector mathematics using the built-in TVector type.
// Vectors are 4-component (X, Y, Z, W) but typically used for 3D graphics (X, Y, Z).
// Create vectors
var v1 := Vector(1, 0, 0); // X-axis unit vector
var v2 := Vector(0, 1, 0); // Y-axis unit vector
var vUp := Vector(0, 0, 1); // Z-axis unit vector
PrintLn('v1: ' + VectorToStr(v1));
PrintLn('v2: ' + VectorToStr(v2));
// Vector Addition
var v3 := v1 + v2;
PrintLn('v1 + v2: ' + VectorToStr(v3));
// Scalar Multiplication
var v4 := v3 * 2.5;
PrintLn('v3 * 2.5: ' + VectorToStr(v4));
// Dot Product (Scalar product)
// For unit vectors, it is the cosine of the angle between them.
// v1 and v2 are perpendicular, so dot product should be 0.
var dot := v1 * v2;
PrintLn('Dot Product (v1 * v2): ' + dot.ToString);
// Cross Product (Vector product)
// Result is perpendicular to both input vectors.
// X cross Y should be Z.
var cross := v1 ^ v2;
PrintLn('Cross Product (v1 ^ v2): ' + VectorToStr(cross));
// Normalization (Making unit length)
var vArbitrary := Vector(10, 10, 10);
PrintLn('Arbitrary: ' + VectorToStr(vArbitrary));
var vNorm := VectorNormalize(vArbitrary);
PrintLn('Normalized: ' + VectorToStr(vNorm));
// Length check (Dot product with self is length squared)
var lengthSq := vNorm * vNorm;
PrintLn('Length Squared of Normalized: ' + lengthSq.ToString);
v1: [1.00 0.00 0.00 0.00] v2: [0.00 1.00 0.00 0.00] v1 + v2: [1.00 1.00 0.00 0.00] v3 * 2.5: [2.50 2.50 0.00 0.00] Dot Product (v1 * v2): 0 Cross Product (v1 ^ v2): [0.00 0.00 1.00 0.00] Arbitrary: [10.00 10.00 10.00 0.00] Normalized: [0.58 0.58 0.58 0.00] Length Squared of Normalized: 1