The v3d library¶
v3d¶
- make(<v3d>) Method¶
To create a vector using keywords. In case that a keyword is not used, the dimension is initialized to
0.0
.- Example:
let v = make(<v3>, x: 1.0, y: 2.0, z: 3.0); format-out("%=\n", v); // (1.0, 2.0, 3.0)
- 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
x
dimension 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'
Constants¶
Infix operations¶
- =(<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
- +(<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)
- -(<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)
- -(<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)
- *(<v3>, <v3>) Method¶
Product of two vectors.
- Signature:
a b => (product)
- Parameters:
a – An instance of
<v3>
.b – An instance of
<v3>
.
- Values:
product – An instance of
<float>
.
- Example:
let v1 = v3(2.0, 2.0, 2.0); let v2 = v3(2.0, 2.0, 2.0); let v3 = v1 * v2; format-out("%=\n", v3); // 12.0
- *(<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)
Other operations¶
- squared Function¶
x ^ 2 + y ^ 2 + z ^ 2.
- Signature:
squared v => (n)
- Parameters:
v – An instance of
<v3>
.
- Values:
n – An instance of
<float>
- Example:
let v = v3(2.0, 2.0, 2.0); let s = v.squared; // 12.0
- 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 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? Function¶
Is the magnitude of the vector 1.0?
- Signature:
unit? u => (is-unit?)
- Parameters:
u – An instance of
<v3>
.
- Values:
is-unit – An instance of
<boolean>
let v = v3(0.0, 3.0, 4.0); assert-false(v.unit?)
- zero?(<v3>) Method¶
Are all the components of the vector 0?
- Signature:
zero? u => (zero?)
- Parameters:
u – An instance of
<v3>
.
- Values:
zero? – An instance of
<boolean>
let v = make(<v3>); assert-true(v.zero?)
- 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 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>