Reference

std/linear/vector3

std/linear/src/vector3.trb

Vector3, the three-component vector, layered by its scalar's bound exactly as Vector2 is.

The one member that has no two-dimensional twin is Vector3.cross, which answers a vector rather than a number: in the plane there is only one direction perpendicular to two vectors, and in space there is a whole vector of it.

type Vector3

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

A point, a direction or a size in space, over whatever scalar the program counts in.

The basis is right-handed: unitX.cross(unitY) is unitZ. Which way a program draws those three axes is the program's own business, and nothing here assumes that the second one points up.

Examples

const step = Vector3 1.0, 2.0, 2.0
print "{step.length()} {step.cross(Vector3(0.0, 1.0, 0.0))}"

Pitfalls

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

Related

  • Vector2 - the same layering with two components.
  • Quaternion - what rotates one of these without a matrix.

field x

x: Scalar

The first component.

field y

y: Scalar

The second component.

field z

z: Scalar

The third component.

fn filled

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

Every component set to the same value.

const zero

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

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

const one

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

Every component one.

const unitX

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

The first basis vector.

const unitY

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

The second basis vector.

const unitZ

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

The third basis vector.

fn add

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

The sum, component by component.

fn subtract

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

The difference, component by component.

fn multiply

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

Every component multiplied by the scalar.

fn divide

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

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

fn dot

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

The dot product. Zero exactly where the two are perpendicular.

fn lengthSquared

fn lengthSquared(): Scalar

The square of the length: the comparison that needs no root.

fn cross

fn cross(other: Vector3<Scalar>): Vector3<Scalar>

The vector perpendicular to both, with the length of the parallelogram they span, in the right-handed sense.

print Vector3(1.0, 0.0, 0.0).cross(Vector3(0.0, 1.0, 0.0))

fn scaled

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

Component by component.

fn divided

fn divided(by: Vector3<Scalar>): Vector3<Scalar>

Component by component. Panics where a component of by is zero.

fn min

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

The smaller of each pair of components.

fn max

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

The larger of each pair of components.

fn clamped

fn clamped(low: Vector3<Scalar>, high: Vector3<Scalar>): Vector3<Scalar>

Every component pulled into the box the two corners span.

fn toVector2

fn toVector2(): Vector2<Scalar>

The first two components.

fn withX

fn withX(value: Scalar): Vector3<Scalar>

The same vector with another first component.

fn withY

fn withY(value: Scalar): Vector3<Scalar>

The same vector with another second component.

fn withZ

fn withZ(value: Scalar): Vector3<Scalar>

The same vector with another third component.

fn isZero

fn isZero(): Bool

Whether every component is zero.

fn largestComponent

fn largestComponent(): Scalar

The largest of the three components.

fn smallestComponent

fn smallestComponent(): Scalar

The smallest of the three components.

fn sum

fn sum(): Scalar

Every component added together.

extend Vector3<Scalar> with Negate

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

A vector of a signed scalar can be turned around.

fn negate

fn negate(): Vector3<Scalar>

Every component with its sign flipped.

extend Vector3<Scalar>

extend<Scalar: Signed> Vector3<Scalar>

What a sign buys: a distance along the axes, without a root.

fn absolute

fn absolute(): Vector3<Scalar>

Every component without its sign.

fn manhattanLength

fn manhattanLength(): Scalar

The distance along the axes, |x| + |y| + |z|.

fn manhattanDistanceTo

fn manhattanDistanceTo(other: Vector3<Scalar>): Scalar

The Vector3.manhattanLength of the step from here to there.

extend Vector3<Scalar>

extend<Scalar: Real> Vector3<Scalar>

What a root and an angle buy: lengths, directions and interpolation.

fn length

fn length(): Scalar

The euclidean length.

fn distanceTo

fn distanceTo(other: Vector3<Scalar>): Scalar

The distance from here to there.

fn distanceSquaredTo

fn distanceSquaredTo(other: Vector3<Scalar>): Scalar

The square of the distance from here to there.

fn normalized

fn normalized(): Vector3<Scalar>

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

fn withLength

fn withLength(value: Scalar): Vector3<Scalar>

The same direction with the length given. A zero vector stays zero.

fn angleTo

fn angleTo(other: Vector3<Scalar>): Angle<Scalar>

The rotation between the two directions, in [0, pi]. Panics where either one is zero.

fn interpolated

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

The point factor of the way from here to there.

fn projectedOnto

fn projectedOnto(other: Vector3<Scalar>): Vector3<Scalar>

The part of this vector that lies along the other one. Panics where the other one is zero.

fn reflected

fn reflected(normal: Vector3<Scalar>): Vector3<Scalar>

Mirrored in the plane through the origin whose normal is given. The normal is expected to have length one.

fn isCloseTo

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

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

extend Vector3<Float>

extend Vector3<Float>

The float vector: the three ways down to a grid.

fn rounded

fn rounded(): Vector3<Int>

Every component rounded to the nearest whole number, halves away from zero.

fn floored

fn floored(): Vector3<Int>

Every component rounded towards negative infinity.

fn ceiling

fn ceiling(): Vector3<Int>

Every component rounded towards positive infinity.

extend Vector3<Int>

extend Vector3<Int>

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

fn toFloat

fn toFloat(): Vector3<Float>

The same vector over Float, exactly.

fn toFixed

fn toFixed(): Vector3<Fixed>

The same vector over Fixed, exactly.