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:
  • x – Dimension x. An instance of <float>

  • y – Dimension y. An instance of <float>

  • z – Dimension z. An instance of <float>

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 a v3d.

Signature:

v-x v => (x)

Parameters:
  • v – An instance of <v3>

Values:
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'
v-y(<v3>) Method#

Returns the y dimension of a v3d.

Signature:

v-y v => (y)

Parameters:
  • v – An instance of <v3>

Values:
Example:

let u = make(<v3>, x: 1.0, y: 2.0, z: 3.0);
format-out("y = %=\n", u.v-y);
// prints 'y = 2.0'
v-z(<v3>) Method#

Returns the z dimension of a v3d.

Signature:

v-z v => (z)

Parameters:
  • v – An instance of <v3>

Values:
Example:

let u = make(<v3>, x: 1.0, y: 2.0, z: 3.0);
format-out("z = %=\n", u.v-z);
// prints 'z = 3.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:
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 (*)#

*(<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

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 (/)#

/(<v3>, <float>) Method#

Divide a vector by a number.

Signature:

  • a n => (division)

Parameters:
  • a – An instance of <v3>.

  • n – An instance of <float>.

Values:
  • division – An instance of <float>.

Example:

let v1 = v3(3.0, 3.0, 3.0);
let v2 = v1 / 3.0;
format-out("%=\n", v2);
// (1.0, 1.0, 1.0)

Other operations#

squared#

squared Function#

x ^ 2 + y ^ 2 + z ^ 2.

Signature:

squared v => (n)

Parameters:
  • v – An instance of <v3>.

Values:
Example:

let v = v3(2.0, 2.0, 2.0);
let s = v.squared;
// 12.0

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:
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?#

unit? Function#

Is the magnitude of the vector 1.0?

Signature:

unit? u => (is-unit?)

Parameters:
  • u – An instance of <v3>.

Values:
let v = v3(0.0, 3.0, 4.0);
assert-false(v.unit?)

zero?#

zero?(<v3>) Method#

Are all the components of the vector 0?

Signature:

zero? u => (zero?)

Parameters:
  • u – An instance of <v3>.

Values:
let v = make(<v3>);
assert-true(v.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>