The v3d library#
<v3> class#
The class <v3> has three dimensions x, y and z of
type <float>.
Creation with make#
To create a vector use keywords:
let v = make(<v3>, x: 1.0, y: 2.0, z: 3.0);
format-out("%=\n", v);
// (1.0, 2.0, 3.0)
In case that a keyword is not used, the dimension is initialized to
0.0.
Creation with v3#
- v3 Function#
Short form to create a
v3.- Signature:
v3 x y z => (v)
- Parameters:
- Values:
v – An instance of
v3d
- Discussion:
The code is shorter but less flexible, all parameters must be passed and the order is important.
- Example:
let v = v3(1.0, 2.0, 3.0); format-out("%=\n", v); // (1.0, 2.0, 3.0)
Dimension accessors (x, y and z)#
Dimensions x, y and z can be accessed by v-x, v-y
and v-z respectively.
- v-x(<v3>) Method#
Returns the
xdimension of av3d.- Signature:
v-x v => (x)
- Parameters:
v – An instance of
<v3>
- Values:
x – An instance of
<float>
- Example:
let u = make(<v3>, x: 1.0, y: 2.0, z: 3.0); format-out("x = %=\n", u.v-x); // prints 'x = 1.0'
Vector zero ($v3-zero)#
$v3-zero is a constant for a vector with 0.0 in coordinates
x, y and z.
Infix operations#
Equals (=)#
- =(<v3>, <v3>) Method#
Check if two vectors are equal.
- Signature:
= a b => (equal?)
- Parameters:
a – An instance of
<v3>.b – An instance of
<v3>.
- Values:
equal? – An instance of
<boolean>.
- Example:
let v1 = v3(1.0, 1.0, 1.0); let v2 = v3(2.0, 2.0, 2.0); let result = if (v1 = v2) "equals" else "different" end; format-out("%s\n", result); // different
Addition (+)#
- +(<v3>, <v3>) Method#
Adds two vectors.
- Signature:
+ a b => (sum)
- Parameters:
a – An instance of
<v3>.b – An instance of
<v3>.
- Values:
sum – An instance of
<v3>.
- Example:
let v1 = v3(1.0, 1.0, 1.0); let v2 = v3(2.0, 2.0, 2.0); let v3 = v1 + v2; format-out("%=\n", v3); // (3.0, 3.0, 3.0)
Substraction (-)#
- -(<v3>, <v3>) Method#
Substract two vectors.
- Signature:
- a b => (difference)
- Parameters:
a – An instance of
<v3>.b – An instance of
<v3>.
- Values:
difference – An instance of
<v3>.
- Example:
let v1 = v3(2.0, 2.0, 2.0); let v2 = v3(1.0, 1.0, 1.0); let v3 = v1 - v2; format-out("%=\n", v3); // (1.0, 1.0, 1.0)
Negative (-)#
- -(<v3>) Method#
Substract two vectors.
- Signature:
- a => (negated)
- Parameters:
a – An instance of
<v3>.
- Values:
negated – An instance of
<v3>.
- Example:
let v1 = v3(2.0, 2.0, 2.0); let v2 = -v1; format-out("%=\n", v2); // (-2.0, -2.0, -2.0)
Product (*)#
Scalar multiplication (*)#
- *(<v3>, <float>) Method#
Product scalar of a vector by a number.
Let v = (x1, y1, z1) and let k be scalar. The scalar multiplication of kv = (kx1, ky1, kz1).
- Signature:
a n => (product)
- Parameters:
a – An instance of
<v3>.n – An instance of
<float>.
- Values:
product – An instance of
<v3>.
- Example:
let v1 = v3(1.0, 1.0, 1.0); let v2 = v1 * 2.0; format-out("%=\n", v2); // (2.0, 2.0, 2.0)
- *(<float>, <v3>) Method#
Product scalar of a number by vector.
Let v = (x1, y1, z1) and let k be scalar. The scalar multiplication of kv = (kx1, ky1, kz1).
- Signature:
n a => (product)
- Parameters:
n – An instance of
<float>.a – An instance of
<v3>.
- Values:
product – An instance of
<v3>.
- Example:
let v1 = v3(1.0, 1.0, 1.0); let v2 = 2.0 * v1; format-out("%=\n", v2); // (2.0, 2.0, 2.0)
Division (/)#
Other operations#
squared#
magnitude#
- magnitude Function#
Scalar magnitude of a vector. Also called length. It’s calculated as the squared root of the squared vector.
- Signature:
magnitude v => (n)
- Parameters:
v – An instance of
<v3>.
- Values:
n – An instance of
<float>
let v = v3(0.0, 3.0, 4.0); assert-equal(v.magnitude, 5.0); let u = v3(2.0, 3.0, 4.0); assert-equal(u.magnitude, sqrt(29.0));
cross-product#
- cross-product Function#
Takes the cross product of vector u and v and returns a vector perpendicular to both u and v.
- Signature:
cross-product u v => (c)
- Parameters:
u – An instance of
<v3>.v – An instance of
<v3>.
- Values:
c – An instance of
<v3>.
let u = v3(3.0, -3.0, 1.0); let v = v3(4.0, 9.0, 2.0); let r = v3(-15.0, -2.0, 39.0); assert-equal(cross-product(u, v), r);
unit?#
zero?#
normalize#
- normalize Function#
- signature:
normalize u => (normalized)
- parameter u:
An instance of
<v3>.- value normalized:
An instance of
<v3>
let v1 = v3(3.0, 1.0, 2.0); assert-true(similar(v1.normalize.magnitude, 1.0));
distance#
- distance Function#
Magnitude of u - v
- Signature:
distance u v => (distance)
- Parameters:
u – An instance of
<v3>.v – An instance of
<v3>.
- Values:
distance – An instance of
<v3>