Reference

std/linear/vector4

std/linear/src/vector4.trb

Vector4, the four-component vector: a homogeneous point or direction, and the column a Matrix4 is made of.

There is no cross product here. A fourth component turns a point in space into something a 4x4 matrix can translate, and that is what this type is for.

type Vector4

type Vector4<Scalar: Numeric = Float> with Add, Subtract, Multiply<Scalar>, Divide<Scalar>

Four components, over whatever scalar the program counts in: the homogeneous form of a point (w one) or of a direction (w zero), and a colour with an alpha where a program wants one.

Examples

const point = Vector4 1.0, 2.0, 3.0, 1.0
print "{point.toVector3()} {point.dot(point)}"

Pitfalls

  • The constants exist for every scalar and take it from the expected type. With nothing expected, the bare Vector4.zero is the Float one, the declared default.

Related

  • Matrix4 - what these are the columns of.
  • Vector4.projected - the way back from homogeneous coordinates.

field x

x: Scalar

The first component.

field y

y: Scalar

The second component.

field z

z: Scalar

The third component.

field w

w: Scalar

The fourth component: one for a point, zero for a direction.

fn filled

static fn filled(value: Scalar): Vector4<Scalar>

Every component set to the same value.

const zero

static zero: Vector4<Scalar> = Vector4 Scalar.zero, Scalar.zero, Scalar.zero, Scalar.zero

Every component zero, over whichever scalar is asked for: Vector4<Int>.zero, Vector4<Fixed>.zero.

const one

static one: Vector4<Scalar> = Vector4 Scalar.one, Scalar.one, Scalar.one, Scalar.one

Every component one.

const unitX

static unitX: Vector4<Scalar> = Vector4 Scalar.one, Scalar.zero, Scalar.zero, Scalar.zero

The first basis vector.

const unitY

static unitY: Vector4<Scalar> = Vector4 Scalar.zero, Scalar.one, Scalar.zero, Scalar.zero

The second basis vector.

const unitZ

static unitZ: Vector4<Scalar> = Vector4 Scalar.zero, Scalar.zero, Scalar.one, Scalar.zero

The third basis vector.

const unitW

static unitW: Vector4<Scalar> = Vector4 Scalar.zero, Scalar.zero, Scalar.zero, Scalar.one

The fourth basis vector, which is the origin as a homogeneous point.

fn of

static fn of(point: Vector3<Scalar>, w: Scalar): Vector4<Scalar>

A point in space as a homogeneous point, with w set to what is given.

fn add

fn add(other: Vector4<Scalar>): Vector4<Scalar>

The sum, component by component.

fn subtract

fn subtract(other: Vector4<Scalar>): Vector4<Scalar>

The difference, component by component.

fn multiply

fn multiply(other: Scalar): Vector4<Scalar>

Every component multiplied by the scalar.

fn divide

fn divide(other: Scalar): Vector4<Scalar>

Every component divided by the scalar. Panics on a division by zero.

fn dot

fn dot(other: Vector4<Scalar>): Scalar

The dot product.

fn lengthSquared

fn lengthSquared(): Scalar

The square of the length.

fn scaled

fn scaled(by: Vector4<Scalar>): Vector4<Scalar>

Component by component.

fn min

fn min(other: Vector4<Scalar>): Vector4<Scalar>

The smaller of each pair of components.

fn max

fn max(other: Vector4<Scalar>): Vector4<Scalar>

The larger of each pair of components.

fn toVector3

fn toVector3(): Vector3<Scalar>

The first three components, with the fourth dropped rather than divided out.

fn isZero

fn isZero(): Bool

Whether every component is zero.

fn sum

fn sum(): Scalar

Every component added together.

extend Vector4<Scalar> with Negate

extend<Scalar: Signed> Vector4<Scalar> with Negate

A vector of a signed scalar can be turned around.

fn negate

fn negate(): Vector4<Scalar>

Every component with its sign flipped.

extend Vector4<Scalar>

extend<Scalar: Signed> Vector4<Scalar>

Every component without its sign.

fn absolute

fn absolute(): Vector4<Scalar>

Every component without its sign.

extend Vector4<Scalar>

extend<Scalar: Real> Vector4<Scalar>

What a root buys: a length, a direction and the way out of homogeneous coordinates.

fn length

fn length(): Scalar

The euclidean length over all four components.

fn normalized

fn normalized(): Vector4<Scalar>

The same direction with length one. A zero vector answers itself.

fn projected

fn projected(): Vector3<Scalar>

The point this homogeneous vector stands for: the first three components divided by the fourth. Panics where the fourth is zero, which is the one case that stands for a direction and not for a point.

fn interpolated

fn interpolated(toward: Vector4<Scalar>, by: Scalar): Vector4<Scalar>

The point factor of the way from here to there.

fn isCloseTo

fn isCloseTo(other: Vector4<Scalar>, tolerance: Scalar): Bool

Whether every component is within tolerance of the other vector's.

extend Vector4<Int>

extend Vector4<Int>

The grid vector: the way up to a scalar that has fractions.

fn toFloat

fn toFloat(): Vector4<Float>

The same vector over Float, exactly.

fn toFixed

fn toFixed(): Vector4<Fixed>

The same vector over Fixed, exactly.