math3d

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 14, 2026 License: MIT Imports: 2 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package math3d provides dependency-free geometric value types and operations.

Conventions

The package uses right-handed Cartesian cross products. View and world helpers treat local forward as the negative Z direction; the package does not prescribe a world-up axis.

Matrices use row-vector semantics. A point is transformed as [x y z 1] * M, while a direction uses [x y z 0] * M. Surface normals use the inverse transpose of M's linear transform so they remain perpendicular under non-uniform scale and shear. Translation occupies M41, M42, and M43. In a product A * B, A is applied first and B second.

Rotation APIs accept radians unless their documentation explicitly says otherwise. Positive rotations follow the right-hand rule. Coordinate record fields whose upstream declarations do not specify units document their units individually rather than inheriting this rule.

Primary floating-point values use float32, matching the upstream C# float representation. Types prefixed with D use float64. AxisAngle is also float64-based to match its upstream representation. Floating-point values retain normal Go IEEE 754 equality behavior, including its behavior for NaN. Approximate comparisons use an explicit absolute tolerance; conveniences may use the source default of 1e-7. No mutable global tolerance is used.

Values have public fields and may be constructed and changed directly by callers. Package operations use value semantics: they do not mutate value arguments or receiver values, and return changed values instead. A zero value is safe to store and pass, but it is not necessarily geometrically meaningful; operations document any stronger preconditions and report ordinary failures with an ok result.

Struct layout is an API representation, not a binary wire format. Any binary encoding supplied by this package defines component order and endianness explicitly instead of serializing raw struct memory.

Index

Examples

Constants

View Source
const (
	Pi        float32 = 3.1415927
	HalfPi    float32 = Pi / 2
	TwoPi     float32 = Pi * 2
	Tolerance float32 = 0.0000001
	Log10E    float32 = 0.4342945
	Log2E     float32 = 1.442695
	E         float32 = 2.7182817

	RadiansToDegrees  float64 = 57.295779513082320876798154814105
	DegreesToRadians  float64 = 0.017453292519943295769236907684886
	OneTenthOfADegree float64 = DegreesToRadians / 10

	MillimetersToFeet float64 = 0.00328084
	FeetToMillimeters float64 = 1 / MillimetersToFeet
)

Mathematical constants preserve the precision and values of the upstream declarations.

Variables

This section is empty.

Functions

func CatmullRom

func CatmullRom(value1, value2, value3, value4, amount float32) float32

CatmullRom interpolates between value2 and value3 using value1 and value4 as neighboring control values. Amount is not clamped.

func CombineHash

func CombineHash(first, second int32) int32

CombineHash combines two source-compatible signed 32-bit hash values.

func Coplanar

func Coplanar(a, b, c, d Vec3, tolerance float32) bool

Coplanar reports whether four finite points have an absolute scalar triple product strictly less than tolerance. As in upstream, tolerance is absolute and scales with the cube of the coordinates. A degenerate point set is coplanar; a non-positive or non-finite tolerance returns false.

func HashInts

func HashInts(values ...int32) int32

HashInts combines all values in order from a zero seed.

func Hermite

func Hermite(value1, tangent1, value2, tangent2, amount float32) float32

Hermite performs cubic Hermite interpolation. Amount is not clamped.

func IsNonZeroAndValid

func IsNonZeroAndValid(value, tolerance float32) bool

IsNonZeroAndValid reports whether value is finite and its absolute value is greater than tolerance.

func NearestPowerOfTwo

func NearestPowerOfTwo(value int32) (int32, bool)

NearestPowerOfTwo returns the power of two whose base-two logarithm is nearest to value's, matching the pinned upstream helper for positive inputs. Zero, negative values, and results that do not fit in int32 fail.

func Percentage

func Percentage(denominator, numerator float64) float64

Percentage returns numerator as a percentage of denominator.

func RandomFloat

func RandomFloat(minimum, maximum float32, index, seed int32) float32

RandomFloat returns a deterministic value in the inclusive range [minimum, maximum] when minimum <= maximum.

func RandomInt

func RandomInt(index, seed int32) int32

RandomInt returns a deterministic source-compatible value for index and seed.

func RandomUint

func RandomUint(index, seed int32) uint32

RandomUint returns the unsigned representation of RandomInt.

func RandomUnitFloat

func RandomUnitFloat(index, seed int32) float32

RandomUnitFloat returns a deterministic value in [0, 1].

func SmoothStep

func SmoothStep(value1, value2, amount float32) float32

SmoothStep smoothly interpolates from value1 to value2, clamping amount to [0, 1].

func Smoothstep

func Smoothstep(x float64) float64

Smoothstep evaluates x²(3-2x) without clamping x.

func StatsAverage

func StatsAverage[T statsVector](stats Stats[T]) (T, bool)

StatsAverage returns the component-wise average. It fails when Count is not positive.

func StatsExtents

func StatsExtents[T statsVector](stats Stats[T]) T

StatsExtents returns Max-Min component by component.

func StatsMiddle

func StatsMiddle[T statsVector](stats Stats[T]) T

StatsMiddle returns the midpoint between Min and Max.

func ToDegrees

func ToDegrees(radians float64) float64

ToDegrees converts radians to degrees.

func ToRadians

func ToRadians(degrees float64) float64

ToRadians converts degrees to radians.

func Version

func Version() string

Version returns the semantic version of the package.

func WrapAngle

func WrapAngle(angle float32) float32

WrapAngle wraps an angle in radians to (-Pi, Pi].

Types

type AxisAngle

type AxisAngle struct {
	Axis  DVec3
	Angle float64
}

AxisAngle is the upstream AxisAngle value. Its float64 precision matches the source despite the absence of a D prefix. Angle is in radians. A zero axis is safe to represent but does not define a rotation.

func NewAxisAngle

func NewAxisAngle(axis DVec3, angle float64) AxisAngle

NewAxisAngle constructs an AxisAngle without normalizing axis.

func (AxisAngle) AlmostEqual

func (a AxisAngle) AlmostEqual(other AxisAngle, tolerance float64) bool

AlmostEqual compares the stored axis and angle, not rotational equivalence.

type Box2

type Box2 struct {
	Min, Max Vec2
}

Box2 is the upstream AABox2D value. Construction preserves endpoint order; any Min component greater than its corresponding Max component represents an empty box. The zero value is a point box at the origin.

func Box2FromPoints

func Box2FromPoints(points []Vec2) (Box2, bool)

Box2FromPoints returns the bounds of points, or false for an empty or non-finite input.

func EmptyBox2

func EmptyBox2() Box2

EmptyBox2 returns the canonical empty 2D box.

func NewBox2

func NewBox2(minimum, maximum Vec2) Box2

NewBox2 constructs a Box2 without reordering endpoints.

func (Box2) AlmostEqual

func (b Box2) AlmostEqual(other Box2, tolerance float32) bool

AlmostEqual compares corresponding endpoints.

func (Box2) Center

func (b Box2) Center() Vec2

func (Box2) ContainsBox

func (b Box2) ContainsBox(other Box2) Containment

func (Box2) ContainsPoint

func (b Box2) ContainsPoint(point Vec2) bool

func (Box2) Corners

func (b Box2) Corners() [4]Vec2

func (Box2) Empty

func (b Box2) Empty() bool

func (Box2) Expanded

func (b Box2) Expanded(point Vec2) Box2

func (Box2) Extent

func (b Box2) Extent() Vec2

func (Box2) Intersection

func (b Box2) Intersection(other Box2) (Box2, bool)

func (Box2) Intersects

func (b Box2) Intersects(other Box2) bool

func (Box2) Merge

func (b Box2) Merge(other Box2) Box2

func (Box2) Translated

func (b Box2) Translated(offset Vec2) Box2

func (Box2) Valid

func (b Box2) Valid() bool

type Box3

type Box3 struct {
	Min, Max Vec3
}

Box3 is the upstream AABox value. Construction preserves endpoint order; any reversed component represents an empty box. The zero value is a point box at the origin.

func Box3FromPoints

func Box3FromPoints(points []Vec3) (Box3, bool)

Box3FromPoints returns the bounds of points, or false for an empty or non-finite input.

Example
package main

import (
	"fmt"

	"github.com/maloquacious/math3d"
)

func main() {
	box, ok := math3d.Box3FromPoints([]math3d.Vec3{
		math3d.V3(2, -1, 4),
		math3d.V3(-3, 5, 1),
		math3d.V3(0, 2, 8),
	})
	if !ok {
		return
	}

	fmt.Printf("min: %.0f %.0f %.0f\n", box.Min.X, box.Min.Y, box.Min.Z)
	fmt.Printf("max: %.0f %.0f %.0f\n", box.Max.X, box.Max.Y, box.Max.Z)
}
Output:
min: -3 -1 1
max: 2 5 8

func EmptyBox3

func EmptyBox3() Box3

EmptyBox3 returns the canonical empty 3D box.

func NewBox3

func NewBox3(minimum, maximum Vec3) Box3

NewBox3 constructs a Box3 without reordering endpoints.

func (Box3) AlmostEqual

func (b Box3) AlmostEqual(other Box3, tolerance float32) bool

AlmostEqual compares corresponding endpoints.

func (Box3) Center

func (b Box3) Center() Vec3

func (Box3) ContainsBox

func (b Box3) ContainsBox(other Box3) Containment

func (Box3) ContainsPoint

func (b Box3) ContainsPoint(point Vec3) bool

func (Box3) ContainsSphere

func (b Box3) ContainsSphere(sphere Sphere) Containment

func (Box3) Corners

func (b Box3) Corners() [8]Vec3

func (Box3) DBox3

func (b Box3) DBox3() DBox3

DBox3 converts b to float64 precision.

func (Box3) Empty

func (b Box3) Empty() bool

func (Box3) Expanded

func (b Box3) Expanded(point Vec3) Box3

func (Box3) Extent

func (b Box3) Extent() Vec3

func (Box3) Intersection

func (b Box3) Intersection(other Box3) (Box3, bool)

func (Box3) Intersects

func (b Box3) Intersects(other Box3) bool

func (Box3) IntersectsPlane

func (b Box3) IntersectsPlane(plane Plane) PlaneIntersection

IntersectsPlane classifies b against plane. Contact is intersecting.

func (Box3) IntersectsSphere

func (b Box3) IntersectsSphere(sphere Sphere) bool

func (Box3) Merge

func (b Box3) Merge(other Box3) Box3

func (Box3) Transformed

func (b Box3) Transformed(matrix Mat4) (Box3, bool)

Transformed returns the axis-aligned bounds of all transformed corners.

func (Box3) Translated

func (b Box3) Translated(offset Vec3) Box3

func (Box3) Valid

func (b Box3) Valid() bool

type Box4

type Box4 struct {
	Min, Max Vec4
}

Box4 is the upstream AABox4D value. Construction preserves endpoint order; any reversed component represents an empty box. The zero value is a point box at the origin.

func EmptyBox4

func EmptyBox4() Box4

EmptyBox4 returns the canonical empty 4D box.

func NewBox4

func NewBox4(minimum, maximum Vec4) Box4

NewBox4 constructs a Box4 without reordering endpoints.

func (Box4) AlmostEqual

func (b Box4) AlmostEqual(other Box4, tolerance float32) bool

AlmostEqual compares corresponding endpoints.

func (Box4) Center

func (b Box4) Center() Vec4

func (Box4) Empty

func (b Box4) Empty() bool

func (Box4) Expanded

func (b Box4) Expanded(point Vec4) Box4

func (Box4) Extent

func (b Box4) Extent() Vec4

func (Box4) Intersection

func (b Box4) Intersection(other Box4) (Box4, bool)

func (Box4) Merge

func (b Box4) Merge(other Box4) Box4

func (Box4) Valid

func (b Box4) Valid() bool

type Byte2

type Byte2 struct {
	X, Y byte
}

Byte2 is the upstream Byte2 value.

func NewByte2

func NewByte2(x, y byte) Byte2

NewByte2 constructs a Byte2.

type Byte3

type Byte3 struct {
	X, Y, Z byte
}

Byte3 is the upstream Byte3 value.

func NewByte3

func NewByte3(x, y, z byte) Byte3

NewByte3 constructs a Byte3.

type Byte4

type Byte4 struct {
	X, Y, Z, W byte
}

Byte4 is the upstream Byte4 value.

func NewByte4

func NewByte4(x, y, z, w byte) Byte4

NewByte4 constructs a Byte4.

type Complex

type Complex struct {
	Real, Imaginary float64
}

Complex is the upstream Complex value. It interoperates explicitly with Go's complex128 rather than shadowing the built-in conversion syntax.

func ComplexFrom128

func ComplexFrom128(value complex128) Complex

ComplexFrom128 converts a built-in complex value.

func NewComplex

func NewComplex(real, imaginary float64) Complex

NewComplex constructs a Complex value.

func (Complex) AlmostEqual

func (c Complex) AlmostEqual(other Complex, tolerance float64) bool

AlmostEqual reports whether corresponding components differ by less than tolerance. It follows normal IEEE behavior: NaN never compares equal.

func (Complex) Complex128

func (c Complex) Complex128() complex128

Complex128 converts c to a built-in complex value.

type Containment

type Containment uint8

Containment describes the spatial relationship between two shapes.

const (
	// ContainmentDisjoint is the zero value.
	ContainmentDisjoint Containment = iota
	ContainmentContains
	ContainmentIntersects
)

type Cylindrical

type Cylindrical struct {
	Radius, Azimuth, Height float64
}

Cylindrical is the upstream CylindricalCoordinate value.

func NewCylindrical

func NewCylindrical(radius, azimuth, height float64) Cylindrical

NewCylindrical constructs a Cylindrical value.

func (Cylindrical) AlmostEqual

func (v Cylindrical) AlmostEqual(other Cylindrical, tolerance float64) bool

AlmostEqual compares corresponding components using a strict absolute tolerance.

type DBox2

type DBox2 struct {
	Min, Max DVec2
}

DBox2 is the upstream DAABox2D value. Construction preserves endpoint order; any reversed component represents an empty box. The zero value is a point box at the origin.

func EmptyDBox2

func EmptyDBox2() DBox2

func NewDBox2

func NewDBox2(minimum, maximum DVec2) DBox2

NewDBox2 constructs a DBox2 without reordering endpoints.

func (DBox2) AlmostEqual

func (b DBox2) AlmostEqual(other DBox2, tolerance float64) bool

AlmostEqual compares corresponding endpoints.

func (DBox2) Center

func (b DBox2) Center() DVec2

func (DBox2) Empty

func (b DBox2) Empty() bool

func (DBox2) Expanded

func (b DBox2) Expanded(point DVec2) DBox2

func (DBox2) Extent

func (b DBox2) Extent() DVec2

func (DBox2) Intersection

func (b DBox2) Intersection(other DBox2) (DBox2, bool)

func (DBox2) Merge

func (b DBox2) Merge(other DBox2) DBox2

func (DBox2) Valid

func (b DBox2) Valid() bool

type DBox3

type DBox3 struct {
	Min, Max DVec3
}

DBox3 is the upstream DAABox value. Construction preserves endpoint order; any reversed component represents an empty box. The zero value is a point box at the origin.

func EmptyDBox3

func EmptyDBox3() DBox3

func NewDBox3

func NewDBox3(minimum, maximum DVec3) DBox3

NewDBox3 constructs a DBox3 without reordering endpoints.

func (DBox3) AlmostEqual

func (b DBox3) AlmostEqual(other DBox3, tolerance float64) bool

AlmostEqual compares corresponding endpoints.

func (DBox3) Box3

func (b DBox3) Box3() Box3

Box3 converts b to float32 precision.

func (DBox3) Center

func (b DBox3) Center() DVec3

func (DBox3) ContainsPoint

func (b DBox3) ContainsPoint(point DVec3) bool

func (DBox3) Empty

func (b DBox3) Empty() bool

func (DBox3) Expanded

func (b DBox3) Expanded(point DVec3) DBox3

func (DBox3) Extent

func (b DBox3) Extent() DVec3

func (DBox3) Intersection

func (b DBox3) Intersection(other DBox3) (DBox3, bool)

func (DBox3) Merge

func (b DBox3) Merge(other DBox3) DBox3

func (DBox3) Valid

func (b DBox3) Valid() bool

type DBox4

type DBox4 struct {
	Min, Max DVec4
}

DBox4 is the upstream DAABox4D value. Construction preserves endpoint order; any reversed component represents an empty box. The zero value is a point box at the origin.

func EmptyDBox4

func EmptyDBox4() DBox4

func NewDBox4

func NewDBox4(minimum, maximum DVec4) DBox4

NewDBox4 constructs a DBox4 without reordering endpoints.

func (DBox4) AlmostEqual

func (b DBox4) AlmostEqual(other DBox4, tolerance float64) bool

AlmostEqual compares corresponding endpoints.

func (DBox4) Center

func (b DBox4) Center() DVec4

func (DBox4) Empty

func (b DBox4) Empty() bool

func (DBox4) Expanded

func (b DBox4) Expanded(point DVec4) DBox4

func (DBox4) Extent

func (b DBox4) Extent() DVec4

func (DBox4) Intersection

func (b DBox4) Intersection(other DBox4) (DBox4, bool)

func (DBox4) Merge

func (b DBox4) Merge(other DBox4) DBox4

func (DBox4) Valid

func (b DBox4) Valid() bool

type DInterval

type DInterval struct {
	Min, Max float64
}

DInterval stores inclusive float64 scalar bounds. Construction preserves bound order; Min > Max is the empty representation. The zero value is [0, 0].

func EmptyDInterval

func EmptyDInterval() DInterval

EmptyDInterval returns the canonical empty float64 interval.

func NewDInterval

func NewDInterval(minimum, maximum float64) DInterval

NewDInterval constructs a DInterval without reordering its bounds.

func (DInterval) AlmostEqual

func (i DInterval) AlmostEqual(other DInterval, tolerance float64) bool

AlmostEqual compares corresponding bounds using a strict absolute tolerance.

func (DInterval) Center

func (i DInterval) Center() float64

func (DInterval) Empty

func (i DInterval) Empty() bool

func (DInterval) Expanded

func (i DInterval) Expanded(value float64) DInterval

func (DInterval) Extent

func (i DInterval) Extent() float64

func (DInterval) Intersection

func (i DInterval) Intersection(other DInterval) (DInterval, bool)

func (DInterval) Merge

func (i DInterval) Merge(other DInterval) DInterval

func (DInterval) Valid

func (i DInterval) Valid() bool

type DPlane

type DPlane struct {
	Normal DVec3
	D      float64
}

DPlane is the float64 plane representation using the equation dot(Normal, point) + D = 0. Its zero value does not define a plane.

func DPlaneFromVec4

func DPlaneFromVec4(value DVec4) DPlane

DPlaneFromVec4 constructs a DPlane from normal components followed by D.

func NewDPlane

func NewDPlane(normal DVec3, d float64) DPlane

NewDPlane constructs a DPlane without normalizing normal or changing D.

func (DPlane) AlmostEqual

func (p DPlane) AlmostEqual(other DPlane, tolerance float64) bool

AlmostEqual compares corresponding stored components.

func (DPlane) DVec4

func (p DPlane) DVec4() DVec4

DVec4 returns the plane's normal components followed by D.

type DQuat

type DQuat struct {
	X, Y, Z, W float64
}

DQuat is the upstream DQuaternion value. The zero value is not a valid rotation, and direct construction does not normalize the value.

func DQuatFromVector

func DQuatFromVector(vector DVec3, scalar float64) DQuat

DQuatFromVector constructs a DQuat from its vector and scalar parts.

func IdentityDQuat

func IdentityDQuat() DQuat

IdentityDQuat returns the float64 identity rotation.

func NewDQuat

func NewDQuat(x, y, z, w float64) DQuat

NewDQuat constructs a DQuat without normalizing it.

func (DQuat) AlmostEqual

func (q DQuat) AlmostEqual(other DQuat, tolerance float64) bool

AlmostEqual compares corresponding components using a strict absolute tolerance. It compares representations, so q and -q are not treated as equivalent rotations.

func (DQuat) Quat

func (q DQuat) Quat() Quat

Quat converts q to float32 precision.

func (DQuat) Vector

func (q DQuat) Vector() DVec3

Vector returns q's vector part.

type DRay

type DRay struct {
	Origin, Direction DVec3
}

DRay is the float64 ray representation. Direction is not implicitly normalized. A zero direction does not define a geometric ray.

func NewDRay

func NewDRay(origin, direction DVec3) DRay

NewDRay constructs a DRay without normalizing direction.

func (DRay) AlmostEqual

func (r DRay) AlmostEqual(other DRay, tolerance float64) bool

AlmostEqual compares corresponding stored components.

func (DRay) IntersectBox

func (r DRay) IntersectBox(box DBox3) (float64, bool)

func (DRay) IntersectSphere

func (r DRay) IntersectSphere(sphere DSphere) (float64, bool)

func (DRay) PointAt

func (r DRay) PointAt(t float64) DVec3

type DSphere

type DSphere struct {
	Center DVec3
	Radius float64
}

DSphere is the float64 sphere representation. NewDSphere preserves negative radii. The zero value is a radius-zero sphere at the origin.

func NewDSphere

func NewDSphere(center DVec3, radius float64) DSphere

NewDSphere constructs a DSphere without validating radius.

func (DSphere) AlmostEqual

func (s DSphere) AlmostEqual(other DSphere, tolerance float64) bool

AlmostEqual compares corresponding stored components.

func (DSphere) Bounds

func (s DSphere) Bounds() DBox3

func (DSphere) ContainsPoint

func (s DSphere) ContainsPoint(point DVec3) Containment

func (DSphere) Intersects

func (s DSphere) Intersects(other DSphere) bool

func (DSphere) Valid

func (s DSphere) Valid() bool

type DVec2

type DVec2 struct{ X, Y float64 }

DVec2 is the upstream DVector2 value.

func DV2

func DV2(x, y float64) DVec2

func SplatDV2

func SplatDV2(v float64) DVec2

func (DVec2) Add

func (v DVec2) Add(o DVec2) DVec2

func (DVec2) AlmostEqual

func (v DVec2) AlmostEqual(o DVec2, tolerance float64) bool

func (DVec2) Cross

func (v DVec2) Cross(o DVec2) float64

func (DVec2) Div

func (v DVec2) Div(o DVec2) DVec2

func (DVec2) Dot

func (v DVec2) Dot(o DVec2) float64

func (DVec2) IsFinite

func (v DVec2) IsFinite() bool

func (DVec2) IsInf

func (v DVec2) IsInf() bool

func (DVec2) IsNaN

func (v DVec2) IsNaN() bool

func (DVec2) Magnitude

func (v DVec2) Magnitude() float64

func (DVec2) MagnitudeSquared

func (v DVec2) MagnitudeSquared() float64

func (DVec2) Max

func (v DVec2) Max(o DVec2) DVec2

func (DVec2) MaxComponent

func (v DVec2) MaxComponent() float64

func (DVec2) Min

func (v DVec2) Min(o DVec2) DVec2

func (DVec2) MinComponent

func (v DVec2) MinComponent() float64

func (DVec2) Mul

func (v DVec2) Mul(o DVec2) DVec2

func (DVec2) Negated

func (v DVec2) Negated() DVec2

func (DVec2) Normalized

func (v DVec2) Normalized() (DVec2, bool)

func (DVec2) ProductComponents

func (v DVec2) ProductComponents() float64

func (DVec2) Scale

func (v DVec2) Scale(s float64) DVec2

func (DVec2) Sub

func (v DVec2) Sub(o DVec2) DVec2

func (DVec2) SumComponents

func (v DVec2) SumComponents() float64

func (DVec2) SumSquaredComponents

func (v DVec2) SumSquaredComponents() float64

func (DVec2) Vec2

func (v DVec2) Vec2() Vec2

type DVec3

type DVec3 struct{ X, Y, Z float64 }

DVec3 is the upstream DVector3 value.

func DV3

func DV3(x, y, z float64) DVec3

func SplatDV3

func SplatDV3(v float64) DVec3

func (DVec3) Add

func (v DVec3) Add(o DVec3) DVec3

func (DVec3) AlmostEqual

func (v DVec3) AlmostEqual(o DVec3, tolerance float64) bool

func (DVec3) Cross

func (v DVec3) Cross(o DVec3) DVec3

func (DVec3) Div

func (v DVec3) Div(o DVec3) DVec3

func (DVec3) Dot

func (v DVec3) Dot(o DVec3) float64

func (DVec3) IsFinite

func (v DVec3) IsFinite() bool

func (DVec3) IsInf

func (v DVec3) IsInf() bool

func (DVec3) IsNaN

func (v DVec3) IsNaN() bool

func (DVec3) Magnitude

func (v DVec3) Magnitude() float64

func (DVec3) MagnitudeSquared

func (v DVec3) MagnitudeSquared() float64

func (DVec3) Max

func (v DVec3) Max(o DVec3) DVec3

func (DVec3) MaxComponent

func (v DVec3) MaxComponent() float64

func (DVec3) Min

func (v DVec3) Min(o DVec3) DVec3

func (DVec3) MinComponent

func (v DVec3) MinComponent() float64

func (DVec3) Mul

func (v DVec3) Mul(o DVec3) DVec3

func (DVec3) Negated

func (v DVec3) Negated() DVec3

func (DVec3) Normalized

func (v DVec3) Normalized() (DVec3, bool)

func (DVec3) ProductComponents

func (v DVec3) ProductComponents() float64

func (DVec3) Scale

func (v DVec3) Scale(s float64) DVec3

func (DVec3) Sub

func (v DVec3) Sub(o DVec3) DVec3

func (DVec3) SumComponents

func (v DVec3) SumComponents() float64

func (DVec3) SumSquaredComponents

func (v DVec3) SumSquaredComponents() float64

func (DVec3) Vec3

func (v DVec3) Vec3() Vec3

type DVec4

type DVec4 struct{ X, Y, Z, W float64 }

DVec4 is the upstream DVector4 value.

func DV4

func DV4(x, y, z, w float64) DVec4

func SplatDV4

func SplatDV4(v float64) DVec4

func (DVec4) Add

func (v DVec4) Add(o DVec4) DVec4

func (DVec4) AlmostEqual

func (v DVec4) AlmostEqual(o DVec4, tolerance float64) bool

func (DVec4) Div

func (v DVec4) Div(o DVec4) DVec4

func (DVec4) Dot

func (v DVec4) Dot(o DVec4) float64

func (DVec4) IsFinite

func (v DVec4) IsFinite() bool

func (DVec4) IsInf

func (v DVec4) IsInf() bool

func (DVec4) IsNaN

func (v DVec4) IsNaN() bool

func (DVec4) Magnitude

func (v DVec4) Magnitude() float64

func (DVec4) MagnitudeSquared

func (v DVec4) MagnitudeSquared() float64

func (DVec4) Max

func (v DVec4) Max(o DVec4) DVec4

func (DVec4) MaxComponent

func (v DVec4) MaxComponent() float64

func (DVec4) Min

func (v DVec4) Min(o DVec4) DVec4

func (DVec4) MinComponent

func (v DVec4) MinComponent() float64

func (DVec4) Mul

func (v DVec4) Mul(o DVec4) DVec4

func (DVec4) Negated

func (v DVec4) Negated() DVec4

func (DVec4) Normalized

func (v DVec4) Normalized() (DVec4, bool)

func (DVec4) ProductComponents

func (v DVec4) ProductComponents() float64

func (DVec4) Scale

func (v DVec4) Scale(s float64) DVec4

func (DVec4) Sub

func (v DVec4) Sub(o DVec4) DVec4

func (DVec4) SumComponents

func (v DVec4) SumComponents() float64

func (DVec4) SumSquaredComponents

func (v DVec4) SumSquaredComponents() float64

func (DVec4) Vec4

func (v DVec4) Vec4() Vec4

type Domain

type Domain struct {
	Lower, Upper float64
}

Domain is the upstream ValueDomain range.

func NewDomain

func NewDomain(lower, upper float64) Domain

NewDomain constructs a Domain without reordering its bounds.

func (Domain) Normalize

func (d Domain) Normalize(value float64) float64

Normalize preserves the upstream ValueDomain behavior: it clamps value to the domain and divides by Upper. It is not conventional inverse lerp unless Lower is zero.

type Euler

type Euler struct {
	Yaw, Pitch, Roll float32
}

Euler is the upstream Euler value. Its yaw, pitch, and roll angles are radians. The zero value represents no rotation; conversion conventions are defined with the quaternion algorithms.

func NewEuler

func NewEuler(yaw, pitch, roll float32) Euler

NewEuler constructs an Euler value in yaw, pitch, roll order.

func (Euler) AlmostEqual

func (e Euler) AlmostEqual(other Euler, tolerance float32) bool

AlmostEqual compares corresponding angles using a strict absolute tolerance.

type Geo

type Geo struct {
	Latitude, Longitude float64
}

Geo is the upstream GeoCoordinate value. Upstream does not specify whether latitude and longitude are expressed in degrees or radians.

func NewGeo

func NewGeo(latitude, longitude float64) Geo

NewGeo constructs a Geo value.

func (Geo) AlmostEqual

func (v Geo) AlmostEqual(other Geo, tolerance float64) bool

AlmostEqual compares corresponding components using a strict absolute tolerance.

type HDRColor

type HDRColor struct {
	R, G, B, A float32
}

HDRColor is the upstream ColorHDR value.

func NewHDRColor

func NewHDRColor(r, g, b, a float32) HDRColor

NewHDRColor constructs an HDRColor.

func (HDRColor) AlmostEqual

func (c HDRColor) AlmostEqual(other HDRColor, tolerance float32) bool

AlmostEqual reports whether corresponding components differ by less than tolerance. It follows normal IEEE behavior: NaN never compares equal.

type Horizontal

type Horizontal struct {
	Azimuth, Inclination float64
}

Horizontal is the upstream HorizontalCoordinate value.

func HorizontalFromDVec2

func HorizontalFromDVec2(v DVec2) Horizontal

HorizontalFromDVec2 maps X to azimuth and Y to inclination.

func HorizontalFromVec2

func HorizontalFromVec2(v Vec2) Horizontal

HorizontalFromVec2 maps X to azimuth and Y to inclination.

func NewHorizontal

func NewHorizontal(azimuth, inclination float64) Horizontal

NewHorizontal constructs a Horizontal value.

func (Horizontal) AlmostEqual

func (v Horizontal) AlmostEqual(other Horizontal, tolerance float64) bool

AlmostEqual compares corresponding components using a strict absolute tolerance.

func (Horizontal) DVec2

func (v Horizontal) DVec2() DVec2

DVec2 converts azimuth and inclination to X and Y.

func (Horizontal) Vec2

func (v Horizontal) Vec2() Vec2

Vec2 converts azimuth and inclination to float32 X and Y.

type IVec2

type IVec2 struct {
	X, Y int32
}

IVec2 is the upstream Int2 value. Its components retain C#'s 32-bit width.

func IV2

func IV2(x, y int32) IVec2

IV2 constructs an IVec2.

func SplatIV2

func SplatIV2(value int32) IVec2

SplatIV2 constructs an IVec2 whose components are value.

func (IVec2) Vec2

func (v IVec2) Vec2() Vec2

Vec2 converts v to float32 precision.

type IVec3

type IVec3 struct {
	X, Y, Z int32
}

IVec3 is the upstream Int3 value. Its components retain C#'s 32-bit width.

func IV3

func IV3(x, y, z int32) IVec3

IV3 constructs an IVec3.

func SplatIV3

func SplatIV3(value int32) IVec3

SplatIV3 constructs an IVec3 whose components are value.

func (IVec3) Vec3

func (v IVec3) Vec3() Vec3

Vec3 converts v to float32 precision.

type IVec4

type IVec4 struct {
	X, Y, Z, W int32
}

IVec4 is the upstream Int4 value. Its components retain C#'s 32-bit width.

func IV4

func IV4(x, y, z, w int32) IVec4

IV4 constructs an IVec4.

func SplatIV4

func SplatIV4(value int32) IVec4

SplatIV4 constructs an IVec4 whose components are value.

type Interval

type Interval struct {
	Min, Max float32
}

Interval stores inclusive scalar bounds. Construction preserves bound order; Min > Max is the empty representation. The zero value is [0, 0].

func EmptyInterval

func EmptyInterval() Interval

EmptyInterval returns the canonical empty float32 interval.

func NewInterval

func NewInterval(minimum, maximum float32) Interval

NewInterval constructs an Interval without reordering its bounds.

func (Interval) AlmostEqual

func (i Interval) AlmostEqual(other Interval, tolerance float32) bool

AlmostEqual compares corresponding bounds using a strict absolute tolerance.

func (Interval) Center

func (i Interval) Center() float32

func (Interval) Empty

func (i Interval) Empty() bool

func (Interval) Expanded

func (i Interval) Expanded(value float32) Interval

func (Interval) Extent

func (i Interval) Extent() float32

func (Interval) Intersection

func (i Interval) Intersection(other Interval) (Interval, bool)

func (Interval) Merge

func (i Interval) Merge(other Interval) Interval

func (Interval) Valid

func (i Interval) Valid() bool

type LogPolar

type LogPolar struct {
	Rho, Azimuth float64
}

LogPolar is the upstream LogPolarCoordinate value.

func NewLogPolar

func NewLogPolar(rho, azimuth float64) LogPolar

NewLogPolar constructs a LogPolar value.

func (LogPolar) AlmostEqual

func (v LogPolar) AlmostEqual(other LogPolar, tolerance float64) bool

AlmostEqual compares corresponding components using a strict absolute tolerance.

type Mat4

type Mat4 struct {
	M11, M12, M13, M14 float32
	M21, M22, M23, M24 float32
	M31, M32, M33, M34 float32
	M41, M42, M43, M44 float32
}

Mat4 is the upstream Matrix4x4 value. Fields are in row-major order, but transformations use row vectors: translation occupies M41, M42, and M43. The zero value is a zero matrix, not an identity transform.

func BillboardMat4

func BillboardMat4(objectPosition, cameraPosition, cameraUp, cameraForward Vec3) (Mat4, bool)

BillboardMat4 creates a spherical billboard at objectPosition.

func ComposeMat4

func ComposeMat4(scale Vec3, rotation Quat, translation Vec3) Mat4

ComposeMat4 creates scale, then rotation, then translation for row vectors.

func ConstrainedBillboardMat4

func ConstrainedBillboardMat4(objectPosition, cameraPosition, rotateAxis, cameraForward, objectForward Vec3) (Mat4, bool)

ConstrainedBillboardMat4 creates a cylindrical billboard around a unit axis.

func IdentityMat4

func IdentityMat4() Mat4

IdentityMat4 returns the identity matrix.

func LookAtMat4

func LookAtMat4(position, target, up Vec3) (Mat4, bool)

LookAtMat4 creates a right-handed view matrix. It fails for degenerate directions.

func Mat4FromAxisAngle

func Mat4FromAxisAngle(axis Vec3, angle float32) Mat4

Mat4FromAxisAngle creates a rotation around a unit axis.

func Mat4FromQuat

func Mat4FromQuat(q Quat) Mat4

Mat4FromQuat creates the row-vector rotation matrix represented by q.

func Mat4FromRows

func Mat4FromRows(row0, row1, row2, row3 Vec4) Mat4

Mat4FromRows constructs a Mat4 from four rows.

func Mat4FromYawPitchRoll

func Mat4FromYawPitchRoll(yaw, pitch, roll float32) Mat4

Mat4FromYawPitchRoll creates roll (Z), then pitch (X), then yaw (Y).

func NewMat4

func NewMat4(
	m11, m12, m13, m14 float32,
	m21, m22, m23, m24 float32,
	m31, m32, m33, m34 float32,
	m41, m42, m43, m44 float32,
) Mat4

NewMat4 constructs a Mat4 from rows in row-major order.

func OrthographicMat4

func OrthographicMat4(width, height, near, far float32) Mat4

func OrthographicOffCenterMat4

func OrthographicOffCenterMat4(left, right, bottom, top, near, far float32) Mat4

func PerspectiveFOVMat4

func PerspectiveFOVMat4(fieldOfView, aspect, near, far float32) (Mat4, bool)

PerspectiveFOVMat4 creates a right-handed perspective projection with depth [0,1].

func PerspectiveMat4

func PerspectiveMat4(width, height, near, far float32) (Mat4, bool)

PerspectiveMat4 creates a perspective projection from near-plane dimensions.

func PerspectiveOffCenterMat4

func PerspectiveOffCenterMat4(left, right, bottom, top, near, far float32) (Mat4, bool)

PerspectiveOffCenterMat4 creates an asymmetric perspective projection.

func ReflectionMat4

func ReflectionMat4(plane Plane) (Mat4, bool)

ReflectionMat4 creates a reflection about dot(Normal, point)+D=0.

func RotationAroundMat4

func RotationAroundMat4(rotation Mat4, center Vec3) Mat4

func RotationXMat4

func RotationXMat4(angle float32) Mat4

func RotationYMat4

func RotationYMat4(angle float32) Mat4

func RotationZMat4

func RotationZMat4(angle float32) Mat4

func ScaleAroundMat4

func ScaleAroundMat4(v, center Vec3) Mat4

ScaleAroundMat4 creates a scale about center.

func ScaleMat4

func ScaleMat4(v Vec3) Mat4

func ShadowMat4

func ShadowMat4(lightDirection Vec3, plane Plane) (Mat4, bool)

ShadowMat4 projects onto plane along lightDirection.

func TranslationMat4

func TranslationMat4(v Vec3) Mat4

func WorldMat4

func WorldMat4(position, forward, up Vec3) (Mat4, bool)

WorldMat4 creates a world matrix whose local forward direction is negative Z.

func (Mat4) Add

func (m Mat4) Add(o Mat4) Mat4

func (Mat4) AlmostEqual

func (m Mat4) AlmostEqual(other Mat4, tolerance float32) bool

AlmostEqual compares corresponding elements using a strict absolute tolerance.

func (Mat4) At

func (m Mat4) At(row, column int) float32

At returns the element at the zero-based row and column. It panics if either index is outside [0, 4).

func (Mat4) Column

func (m Mat4) Column(column int) Vec4

Column returns the zero-based column. It panics if column is outside [0, 4).

func (Mat4) Decompose

func (m Mat4) Decompose() (scale Vec3, rotation Quat, translation Vec3, ok bool)

Decompose extracts scale, rotation, and translation from an affine SRT matrix.

func (Mat4) Determinant

func (m Mat4) Determinant() float32

func (Mat4) Inverse

func (m Mat4) Inverse() (Mat4, bool)

Inverse returns the inverse matrix, or false when m is singular or non-finite.

func (Mat4) IsIdentity

func (m Mat4) IsIdentity() bool

IsIdentity reports exact representation equality with the identity matrix.

func (Mat4) IsReflection

func (m Mat4) IsReflection() bool

func (Mat4) Lerp

func (m Mat4) Lerp(o Mat4, amount float32) Mat4

func (Mat4) Mul

func (m Mat4) Mul(o Mat4) Mat4

Mul multiplies two matrices. With row vectors, m is applied before o.

Example
package main

import (
	"fmt"

	"github.com/maloquacious/math3d"
)

func main() {
	// With row vectors, multiplication order is application order: scale,
	// then rotate, then translate.
	matrix := math3d.ScaleMat4(math3d.V3(2, 2, 2)).
		Mul(math3d.RotationZMat4(math3d.HalfPi)).
		Mul(math3d.TranslationMat4(math3d.V3(5, -1, 0)))
	point := matrix.TransformPoint(math3d.V3(1, 0, 0))

	fmt.Printf("%.0f %.0f %.0f\n", point.X, point.Y, point.Z)
}
Output:
5 1 0

func (Mat4) Quat

func (m Mat4) Quat() Quat

Quat returns the quaternion represented by the upper-left rotation matrix.

func (Mat4) RayFromNormalizedScreen

func (m Mat4) RayFromNormalizedScreen(screen Vec2) (Ray, bool)

RayFromNormalizedScreen constructs a ray by unprojecting screen through m. Screen uses normalized coordinates: (0, 0) is the top-left, (1, 1) is the bottom-right, and values outside that range extrapolate beyond the viewport. Clip-space depth is [0, 1].

The matrix is not an inverse: this method inverts it. A projection matrix produces a view-space ray; a row-vector view-projection matrix view.Mul(projection) produces a world-space ray. The ray starts on the near clip plane, not at the camera, and its direction is normalized. It fails for non-finite screen coordinates, a singular matrix, an invalid homogeneous divide, or a degenerate direction.

Example
package main

import (
	"fmt"

	"github.com/maloquacious/math3d"
)

func main() {
	projection, ok := math3d.PerspectiveFOVMat4(math3d.HalfPi, 1, 1, 10)
	if !ok {
		return
	}
	// Screen coordinates are normalized with a top-left origin. A projection
	// matrix alone produces a view-space ray starting on the near clip plane.
	ray, ok := projection.RayFromNormalizedScreen(math3d.V2(0.5, 0.5))
	if !ok {
		return
	}

	fmt.Printf("origin: %.0f %.0f %.0f\n", ray.Origin.X, ray.Origin.Y, ray.Origin.Z)
	fmt.Printf("direction: %.0f %.0f %.0f\n", ray.Direction.X, ray.Direction.Y, ray.Direction.Z)
}
Output:
origin: 0 0 -1
direction: 0 0 -1

func (Mat4) RotationDeterminant

func (m Mat4) RotationDeterminant() float32

func (Mat4) Row

func (m Mat4) Row(row int) Vec4

Row returns the zero-based row. It panics if row is outside [0, 4).

func (Mat4) Scale

func (m Mat4) Scale(s float32) Mat4

func (Mat4) Sub

func (m Mat4) Sub(o Mat4) Mat4

func (Mat4) TransformDirection

func (m Mat4) TransformDirection(v Vec3) Vec3

TransformDirection transforms a direction with homogeneous W=0.

func (Mat4) TransformNormal

func (m Mat4) TransformNormal(normal Vec3) (Vec3, bool)

TransformNormal transforms a surface normal using the inverse transpose of m, as required by the row-vector convention. Translation has no effect, and the result is not normalized. It fails for a non-finite normal, a singular or non-finite matrix, or a non-finite result.

func (Mat4) TransformPoint

func (m Mat4) TransformPoint(v Vec3) Vec3

TransformPoint transforms a position with homogeneous W=1.

func (Mat4) TransformVec4

func (m Mat4) TransformVec4(v Vec4) Vec4

TransformVec4 applies m to the row vector v.

func (Mat4) Translation

func (m Mat4) Translation() Vec3

Translation returns the row-vector translation stored in m.

func (Mat4) Transposed

func (m Mat4) Transposed() Mat4

func (Mat4) WithTranslation

func (m Mat4) WithTranslation(translation Vec3) Mat4

WithTranslation returns m with its row-vector translation replaced.

type Plane

type Plane struct {
	Normal Vec3
	D      float32
}

Plane represents dot(Normal, point) + D = 0. The normal is not implicitly normalized. The zero value does not define a geometric plane.

func NewPlane

func NewPlane(normal Vec3, d float32) Plane

NewPlane constructs a Plane without normalizing normal or changing D.

func PlaneFromNormalPoint

func PlaneFromNormalPoint(normal, point Vec3) (Plane, bool)

PlaneFromNormalPoint constructs a normalized plane containing point. It fails when normal cannot be normalized. Unlike the inconsistent upstream factory, D follows dot(Normal, point)+D=0.

func PlaneFromPoints

func PlaneFromPoints(a, b, c Vec3) (Plane, bool)

PlaneFromPoints constructs a normalized plane through a, b, and c. Vertex order determines the normal direction. It fails when the points do not define a finite, non-degenerate plane.

func PlaneFromVec4

func PlaneFromVec4(value Vec4) Plane

PlaneFromVec4 constructs a Plane from normal components followed by D.

func XYPlane

func XYPlane() Plane

XYPlane returns the plane z=0.

func XZPlane

func XZPlane() Plane

XZPlane returns the plane y=0.

func YZPlane

func YZPlane() Plane

YZPlane returns the plane x=0.

func (Plane) AlmostEqual

func (p Plane) AlmostEqual(other Plane, tolerance float32) bool

AlmostEqual compares corresponding stored components.

func (Plane) ClassifyPoint

func (p Plane) ClassifyPoint(point Vec3) float32

ClassifyPoint returns the signed plane equation value for point. Negative values are behind the normal, positive values are in front, and zero is on the plane. It is a signed distance only when Normal is unit length.

func (Plane) Dot

func (p Plane) Dot(value Vec4) float32

Dot returns the four-component dot product of p and value.

func (Plane) DotCoordinate

func (p Plane) DotCoordinate(point Vec3) float32

DotCoordinate evaluates dot(Normal, point)+D.

func (Plane) DotNormal

func (p Plane) DotNormal(direction Vec3) float32

DotNormal evaluates dot(Normal, direction), ignoring D.

func (Plane) Normalized

func (p Plane) Normalized() (Plane, bool)

Normalized returns an equivalent plane with a unit normal. It fails for a zero or non-finite normal.

func (Plane) Project

func (p Plane) Project(point Vec3) (Vec3, bool)

Project projects point orthogonally onto p. It supports non-unit normals and fails for a zero or non-finite normal or a non-finite result.

func (Plane) Rotated

func (p Plane) Rotated(rotation Quat) Plane

Rotated rotates p's normal and leaves D unchanged. Rotation must be a unit quaternion.

func (Plane) Transformed

func (p Plane) Transformed(matrix Mat4) (Plane, bool)

Transformed applies matrix's inverse transpose to p. It fails when matrix is singular or when the resulting plane has non-finite components.

func (Plane) Vec4

func (p Plane) Vec4() Vec4

Vec4 returns the plane's normal components followed by D.

type PlaneIntersection

type PlaneIntersection uint8

PlaneIntersection describes which side of a plane a shape occupies.

const (
	// PlaneIntersectionFront is the zero value.
	PlaneIntersectionFront PlaneIntersection = iota
	PlaneIntersectionBack
	PlaneIntersectionIntersecting
)

type Polar

type Polar struct {
	Radius, Azimuth float64
}

Polar is the upstream PolarCoordinate value.

func NewPolar

func NewPolar(radius, azimuth float64) Polar

NewPolar constructs a Polar value.

func (Polar) AlmostEqual

func (v Polar) AlmostEqual(other Polar, tolerance float64) bool

AlmostEqual compares corresponding components using a strict absolute tolerance.

type Quad2

type Quad2 struct{ A, B, C, D Vec2 }

Quad2 is the upstream Quad2D value. Vertices retain caller-supplied order. The zero value is degenerate.

func NewQuad2

func NewQuad2(a, b, c, d Vec2) Quad2

NewQuad2 constructs a Quad2 in A, B, C, D order.

func (Quad2) AlmostEqual

func (q Quad2) AlmostEqual(other Quad2, tolerance float32) bool

AlmostEqual compares corresponding vertices without disregarding winding.

type Quad3

type Quad3 struct{ A, B, C, D Vec3 }

Quad3 is the upstream Quad value. Vertices retain caller-supplied order. The zero value is degenerate.

func NewQuad3

func NewQuad3(a, b, c, d Vec3) Quad3

NewQuad3 constructs a Quad3 in A, B, C, D order.

func (Quad3) AlmostEqual

func (q Quad3) AlmostEqual(other Quad3, tolerance float32) bool

AlmostEqual compares corresponding vertices without disregarding winding.

func (Quad3) Map

func (q Quad3) Map(mapper func(Vec3) Vec3) Quad3

Map applies mapper to all vertices while preserving their order.

func (Quad3) Transformed

func (q Quad3) Transformed(matrix Mat4) Quad3

Transformed applies matrix to every vertex as a point.

type Quat

type Quat struct {
	X, Y, Z, W float32
}

Quat is the upstream Quaternion value. X, Y, and Z are its vector part and W is its scalar part. The zero value is not a valid rotation.

Quat values need not be normalized. Operations that require a unit quaternion are introduced with the quaternion algorithms.

func Concatenate

func Concatenate(first, second Quat) Quat

Concatenate returns the rotation first followed by second.

func IdentityQuat

func IdentityQuat() Quat

IdentityQuat returns the identity rotation.

func LookAtQuat

func LookAtQuat(position, target, up, localForward Vec3) (Quat, bool)

LookAtQuat returns a rotation that turns the caller-supplied localForward direction toward target-position while using up to define the horizontal plane. Inputs need not be normalized, but localForward must be perpendicular to up. It fails for non-finite or zero inputs, coincident position and target, direction/up parallelism, or localForward/up non-perpendicularity.

func NewQuat

func NewQuat(x, y, z, w float32) Quat

NewQuat constructs a Quat without normalizing it.

func QuatFromAxisAngle

func QuatFromAxisAngle(axis Vec3, angle float32) Quat

QuatFromAxisAngle creates a quaternion for an angle in radians. Axis must be unit length.

func QuatFromEulerAngles

func QuatFromEulerAngles(angles Vec3) Quat

QuatFromEulerAngles creates an intrinsic X, then Y, then Z rotation.

func QuatFromVector

func QuatFromVector(vector Vec3, scalar float32) Quat

QuatFromVector constructs a Quat from its vector and scalar parts.

func QuatFromYawPitchRoll

func QuatFromYawPitchRoll(yaw, pitch, roll float32) Quat

QuatFromYawPitchRoll creates roll (Z), then pitch (X), then yaw (Y).

func QuatRotationX

func QuatRotationX(angle float32) Quat

func QuatRotationY

func QuatRotationY(angle float32) Quat

func QuatRotationZ

func QuatRotationZ(angle float32) Quat

func RotationBetween

func RotationBetween(from, to, oppositeAxis Vec3) (Quat, bool)

RotationBetween returns a rotation from one unit vector to another. For opposite vectors, oppositeAxis must be unit and perpendicular to from.

func (Quat) Add

func (q Quat) Add(other Quat) Quat

func (Quat) AlmostEqual

func (q Quat) AlmostEqual(other Quat, tolerance float32) bool

AlmostEqual compares corresponding components using a strict absolute tolerance. It compares representations, so q and -q are not treated as equivalent rotations.

func (Quat) Conjugated

func (q Quat) Conjugated() Quat

func (Quat) DQuat

func (q Quat) DQuat() DQuat

DQuat converts q to float64 precision.

func (Quat) Div

func (q Quat) Div(other Quat) (Quat, bool)

Div multiplies q by the inverse of other.

func (Quat) Dot

func (q Quat) Dot(other Quat) float32

Dot returns the four-component dot product.

func (Quat) EulerAngles

func (q Quat) EulerAngles() Vec3

EulerAngles returns intrinsic X, Y, Z angles in radians.

func (Quat) Inverse

func (q Quat) Inverse() (Quat, bool)

Inverse returns the multiplicative inverse. It fails for zero and non-finite norms.

func (Quat) Length

func (q Quat) Length() float64

func (Quat) LengthSquared

func (q Quat) LengthSquared() float32

func (Quat) Lerp

func (q Quat) Lerp(other Quat, amount float32) (Quat, bool)

Lerp linearly interpolates along the shortest rotational arc and normalizes the result.

func (Quat) Mul

func (q Quat) Mul(other Quat) Quat

Mul returns the Hamilton product q·other. Quaternion multiplication applies other first and q second; use Concatenate for explicitly ordered rotations.

func (Quat) Negated

func (q Quat) Negated() Quat

func (Quat) Normalized

func (q Quat) Normalized() (Quat, bool)

Normalized returns a unit quaternion. It fails for zero and non-finite lengths.

func (Quat) Rotate

func (q Quat) Rotate(v Vec3) Vec3

Rotate rotates v. q must be a unit quaternion.

func (Quat) RotateToVec4

func (q Quat) RotateToVec4(v Vec3) Vec4

RotateToVec4 rotates v and returns homogeneous coordinates with W set to 1. q must be unit.

func (Quat) RotateVec2

func (q Quat) RotateVec2(v Vec2) Vec2

RotateVec2 rotates v embedded as (X, Y, 0) and returns the resulting X and Y components. Rotation out of the XY plane is discarded. q must be unit.

func (Quat) RotateVec2ToVec4

func (q Quat) RotateVec2ToVec4(v Vec2) Vec4

RotateVec2ToVec4 rotates v embedded as (X, Y, 0) and returns homogeneous coordinates with W set to 1. q must be unit.

func (Quat) RotateVec4

func (q Quat) RotateVec4(v Vec4) Vec4

RotateVec4 rotates the X, Y, and Z components of v and preserves W exactly. q must be unit.

func (Quat) Scale

func (q Quat) Scale(scalar float32) Quat

func (Quat) Slerp

func (q Quat) Slerp(other Quat, amount float32) Quat

Slerp spherically interpolates along the shortest rotational arc.

func (Quat) Sub

func (q Quat) Sub(other Quat) Quat

func (Quat) Vector

func (q Quat) Vector() Vec3

Vector returns q's vector part.

type RGB

type RGB struct {
	R, G, B byte
}

RGB is the upstream ColorRGB value.

func NewRGB

func NewRGB(r, g, b byte) RGB

NewRGB constructs an RGB color.

type RGBA

type RGBA struct {
	R, G, B, A byte
}

RGBA is the upstream ColorRGBA value.

func DarkBlue

func DarkBlue() RGBA

DarkBlue returns the upstream dark-blue color.

func DarkGreen

func DarkGreen() RGBA

DarkGreen returns the upstream dark-green color.

func DarkRed

func DarkRed() RGBA

DarkRed returns the upstream dark-red color.

func LightBlue

func LightBlue() RGBA

LightBlue returns the upstream light-blue color.

func LightGreen

func LightGreen() RGBA

LightGreen returns the upstream light-green color.

func LightRed

func LightRed() RGBA

LightRed returns the upstream light-red color.

func NewRGBA

func NewRGBA(r, g, b, a byte) RGBA

NewRGBA constructs an RGBA color.

type Ray

type Ray struct {
	Origin, Direction Vec3
}

Ray stores an origin and direction. Direction is not implicitly normalized, and its length scales the ray parameter. A zero direction is representable but does not define a geometric ray.

func NewRay

func NewRay(origin, direction Vec3) Ray

NewRay constructs a Ray without normalizing direction.

func (Ray) AlmostEqual

func (r Ray) AlmostEqual(other Ray, tolerance float32) bool

AlmostEqual compares corresponding stored components.

func (Ray) IntersectBox

func (r Ray) IntersectBox(box Box3) (float32, bool)

IntersectBox returns the first non-negative parameter at which r meets box.

func (Ray) IntersectPlane

func (r Ray) IntersectPlane(plane Plane, tolerance float32) (float32, bool)

IntersectPlane returns the non-negative parameter at which r meets plane. Parallel rays, including rays contained in the plane, do not have one distinguished intersection and return false.

func (Ray) IntersectSphere

func (r Ray) IntersectSphere(sphere Sphere) (float32, bool)

IntersectSphere returns the first non-negative ray parameter at which r meets sphere. It supports directions of any non-zero finite length.

Example
package main

import (
	"fmt"

	"github.com/maloquacious/math3d"
)

func main() {
	// Direction is not unit length, so t is a ray parameter rather than a
	// Euclidean distance.
	ray := math3d.NewRay(math3d.V3(-5, 0, 0), math3d.V3(2, 0, 0))
	t, ok := ray.IntersectSphere(math3d.NewSphere(math3d.V3(0, 0, 0), 1))
	if !ok {
		return
	}
	point := ray.PointAt(t)

	fmt.Printf("t: %.0f\n", t)
	fmt.Printf("point: %.0f %.0f %.0f\n", point.X, point.Y, point.Z)
}
Output:
t: 2
point: -1 0 0

func (Ray) IntersectTriangle

func (r Ray) IntersectTriangle(triangle Triangle3, tolerance float32) (float32, bool)

IntersectTriangle returns the positive ray parameter at which r meets triangle. Both windings are accepted, and edges and vertices are included. Hits at the ray origin or within tolerance of it are excluded.

func (Ray) PointAt

func (r Ray) PointAt(t float32) Vec3

PointAt evaluates the ray at parameter t. The parameter is a Euclidean distance only when Direction has unit length.

func (Ray) Transformed

func (r Ray) Transformed(matrix Mat4) (Ray, bool)

Transformed applies matrix to the origin and its linear part to Direction.

type Segment2

type Segment2 struct{ A, B Vec2 }

Segment2 is the upstream Line2D value. Its zero value is degenerate.

func NewSegment2

func NewSegment2(a, b Vec2) Segment2

NewSegment2 constructs a Segment2 from its endpoints.

func (Segment2) AlmostEqual

func (s Segment2) AlmostEqual(other Segment2, tolerance float32) bool

AlmostEqual compares corresponding endpoints.

func (Segment2) Bounds

func (s Segment2) Bounds() Box2

Bounds returns the componentwise bounds of the endpoints.

func (Segment2) ContainsOnLine

func (s Segment2) ContainsOnLine(point Vec2, tolerance float32) bool

ContainsOnLine reports whether point is within tolerance of the infinite supporting line. It does not restrict the point to the segment's bounds.

func (Segment2) Intersects

func (s Segment2) Intersects(other Segment2, tolerance float32) bool

Intersects reports whether two closed 2D segments touch or cross. Collinear overlap and shared endpoints count as intersections. Tolerance is an absolute cross-product tolerance, matching the upstream operation.

func (Segment2) Vector

func (s Segment2) Vector() Vec2

Vector returns the directed displacement from A to B.

type Segment3

type Segment3 struct{ A, B Vec3 }

Segment3 is the upstream Line value, represented by endpoints A and B. Its zero value is a degenerate segment at the origin.

func NewSegment3

func NewSegment3(a, b Vec3) Segment3

NewSegment3 constructs a Segment3 from its endpoints.

func (Segment3) AlmostEqual

func (s Segment3) AlmostEqual(other Segment3, tolerance float32) bool

AlmostEqual compares corresponding endpoints.

func (Segment3) Length

func (s Segment3) Length() float64

Length returns the Euclidean distance between the endpoints.

func (Segment3) LengthSquared

func (s Segment3) LengthSquared() float32

LengthSquared returns the squared distance between the endpoints.

func (Segment3) Map

func (s Segment3) Map(mapper func(Vec3) Vec3) Segment3

Map applies mapper to both endpoints while preserving their order.

func (Segment3) Midpoint

func (s Segment3) Midpoint() Vec3

Midpoint returns the point halfway between the endpoints.

func (Segment3) PointAt

func (s Segment3) PointAt(t float32) Vec3

PointAt linearly maps parameter t onto the segment's supporting line. Values outside [0, 1] extrapolate beyond the endpoints.

func (Segment3) Ray

func (s Segment3) Ray() Ray

Ray returns a ray starting at A with the unnormalized displacement to B.

func (Segment3) Reversed

func (s Segment3) Reversed() Segment3

Reversed returns the same segment with opposite endpoint order.

func (Segment3) Transformed

func (s Segment3) Transformed(matrix Mat4) Segment3

Transformed applies matrix to both endpoints as points.

func (Segment3) Vector

func (s Segment3) Vector() Vec3

Vector returns the directed displacement from A to B.

func (Segment3) WithLength

func (s Segment3) WithLength(length float32) (Segment3, bool)

WithLength returns a segment starting at A in the same direction with the requested length. It fails when the direction cannot be normalized or length is negative or non-finite.

type Sphere

type Sphere struct {
	Center Vec3
	Radius float32
}

Sphere stores a center and radius. NewSphere preserves any radius, including a negative one; algorithms that require a non-negative finite radius state that precondition. The zero value is a radius-zero sphere at the origin.

func NewSphere

func NewSphere(center Vec3, radius float32) Sphere

NewSphere constructs a Sphere without validating radius.

func (Sphere) AlmostEqual

func (s Sphere) AlmostEqual(other Sphere, tolerance float32) bool

AlmostEqual compares corresponding stored components.

func (Sphere) Bounds

func (s Sphere) Bounds() Box3

func (Sphere) ContainsBox

func (s Sphere) ContainsBox(box Box3) Containment

func (Sphere) ContainsPoint

func (s Sphere) ContainsPoint(point Vec3) Containment

func (Sphere) ContainsSphere

func (s Sphere) ContainsSphere(other Sphere) Containment

func (Sphere) DistanceToPoint

func (s Sphere) DistanceToPoint(point Vec3) float32

func (Sphere) DistanceToSphere

func (s Sphere) DistanceToSphere(other Sphere) float32

func (Sphere) Intersects

func (s Sphere) Intersects(other Sphere) bool

func (Sphere) IntersectsBox

func (s Sphere) IntersectsBox(box Box3) bool

func (Sphere) IntersectsPlane

func (s Sphere) IntersectsPlane(plane Plane) PlaneIntersection

func (Sphere) Merge

func (s Sphere) Merge(other Sphere) (Sphere, bool)

Merge returns the smallest sphere on the line between the centers that encloses both spheres. Both inputs must be valid.

func (Sphere) Transformed

func (s Sphere) Transformed(matrix Mat4) (Sphere, bool)

Transformed returns a sphere enclosing s after an affine transform. Its radius is scaled by the largest singular value of the linear matrix, which is exact for rotation and non-uniform scale and conservative for shear.

func (Sphere) Translated

func (s Sphere) Translated(offset Vec3) Sphere

func (Sphere) Valid

func (s Sphere) Valid() bool

Valid reports whether s has a finite center and non-negative finite radius.

type Spherical

type Spherical struct {
	Radius, Azimuth, Inclination float64
}

Spherical is the upstream SphericalCoordinate value.

func NewSpherical

func NewSpherical(radius, azimuth, inclination float64) Spherical

NewSpherical constructs a Spherical value.

func (Spherical) AlmostEqual

func (v Spherical) AlmostEqual(other Spherical, tolerance float64) bool

AlmostEqual compares corresponding components using a strict absolute tolerance.

type Stats

type Stats[T any] struct {
	Count         int
	Min, Max, Sum T
}

Stats contains one-pass summary statistics for a sequence of values. Count is zero for an empty sequence, in which case Min, Max, and Sum are their zero values.

func NewStats

func NewStats[T any](count int, minimum, maximum, sum T) Stats[T]

NewStats constructs summary statistics from their stored representation.

func StatsOf

func StatsOf[T statsVector](values []T) Stats[T]

StatsOf summarizes vectors component by component. Unlike upstream, the first value initializes Min and Max, so an all-positive or all-negative sequence is not biased toward zero.

type Transform

type Transform struct {
	Position    Vec3
	Orientation Quat
}

Transform is a rigid transform represented by a position and orientation. It has no scale. Its zero value has a zero quaternion and is therefore not a valid rigid transform; use IdentityTransform when identity is required.

Example
package main

import (
	"fmt"

	"github.com/maloquacious/math3d"
)

func main() {
	transform := math3d.NewTransform(
		math3d.V3(10, 0, 0),
		math3d.QuatRotationZ(math3d.HalfPi),
	)
	point := transform.TransformPoint(math3d.V3(1, 0, 0))

	fmt.Printf("%.0f %.0f %.0f\n", point.X, point.Y, point.Z)
}
Output:
10 1 0

func IdentityTransform

func IdentityTransform() Transform

IdentityTransform returns the identity rigid transform.

func NewTransform

func NewTransform(position Vec3, orientation Quat) Transform

NewTransform constructs a Transform without normalizing orientation.

func (Transform) AlmostEqual

func (t Transform) AlmostEqual(other Transform, tolerance float32) bool

AlmostEqual compares the stored representation, not geometric equivalence.

func (Transform) Mat4

func (t Transform) Mat4() Mat4

Mat4 returns the row-vector matrix that rotates by Orientation and then translates by Position. Orientation must be a unit quaternion for the result to be a rigid transform.

func (Transform) TransformDirection

func (t Transform) TransformDirection(direction Vec3) Vec3

TransformDirection rotates direction by Orientation without translating it. Orientation must be a unit quaternion.

func (Transform) TransformPoint

func (t Transform) TransformPoint(point Vec3) Vec3

TransformPoint rotates point by Orientation and then adds Position. Orientation must be a unit quaternion.

type Triangle2

type Triangle2 struct{ A, B, C Vec2 }

Triangle2 is the upstream Triangle2D value. Vertices retain caller-supplied order. The zero value is degenerate.

func NewTriangle2

func NewTriangle2(a, b, c Vec2) Triangle2

NewTriangle2 constructs a Triangle2 in A, B, C order.

func (Triangle2) AlmostEqual

func (t Triangle2) AlmostEqual(other Triangle2, tolerance float32) bool

AlmostEqual compares corresponding vertices without disregarding winding.

func (Triangle2) Area

func (t Triangle2) Area() float32

Area returns the non-negative geometric area.

func (Triangle2) Contains

func (t Triangle2) Contains(point Vec2) bool

Contains reports whether point lies in the strict interior. Points on an edge are excluded, matching the upstream operation.

func (Triangle2) SignedArea

func (t Triangle2) SignedArea() float32

SignedArea returns the oriented area. Positive values indicate counter-clockwise winding.

type Triangle3

type Triangle3 struct{ A, B, C Vec3 }

Triangle3 is the upstream Triangle value. Vertices retain caller-supplied order. The zero value is degenerate.

func NewTriangle3

func NewTriangle3(a, b, c Vec3) Triangle3

NewTriangle3 constructs a Triangle3 in A, B, C order.

func (Triangle3) AlmostEqual

func (t Triangle3) AlmostEqual(other Triangle3, tolerance float32) bool

AlmostEqual compares corresponding vertices without disregarding winding.

func (Triangle3) Area

func (t Triangle3) Area() float32

Area returns the non-negative geometric area.

func (Triangle3) Bounds

func (t Triangle3) Bounds() Box3

Bounds returns the componentwise bounds of the vertices.

func (Triangle3) Centroid

func (t Triangle3) Centroid() Vec3

Centroid returns the arithmetic mean of the vertices.

func (Triangle3) Degenerate

func (t Triangle3) Degenerate() bool

Degenerate reports whether the vertices are non-finite or have exactly zero geometric area. Use IsSliver when a non-zero minimum edge length is needed.

func (Triangle3) Edges

func (t Triangle3) Edges() [3]Segment3

Edges returns the directed AB, BC, and CA segments.

func (Triangle3) IsSliver

func (t Triangle3) IsSliver(tolerance float32) bool

IsSliver reports whether any edge length is at most tolerance. A negative or non-finite tolerance, or non-finite vertices, is treated as invalid.

func (Triangle3) Map

func (t Triangle3) Map(mapper func(Vec3) Vec3) Triangle3

Map applies mapper to all vertices while preserving their order.

func (Triangle3) Normal

func (t Triangle3) Normal() (Vec3, bool)

Normal returns the unit surface normal. It fails for a non-finite or zero-area triangle.

func (Triangle3) NormalDirection

func (t Triangle3) NormalDirection() Vec3

NormalDirection returns the winding-dependent, unnormalized surface normal.

func (Triangle3) Perimeter

func (t Triangle3) Perimeter() float64

Perimeter returns the sum of the three edge lengths.

func (Triangle3) Transformed

func (t Triangle3) Transformed(matrix Mat4) Triangle3

Transformed applies matrix to every vertex as a point.

type Vec2

type Vec2 struct {
	X, Y float32
}

Vec2 is the upstream Vector2 value. Its components use source-compatible float32 precision.

func RandomVec2

func RandomVec2(index, seed int32) Vec2

RandomVec2 returns a vector whose components use consecutive derived indices.

func SplatV2

func SplatV2(value float32) Vec2

SplatV2 constructs a Vec2 whose components are value.

func V2

func V2(x, y float32) Vec2

V2 constructs a Vec2.

func (Vec2) Add

func (v Vec2) Add(other Vec2) Vec2

func (Vec2) AlmostEqual

func (v Vec2) AlmostEqual(other Vec2, tolerance float32) bool

func (Vec2) Cross

func (v Vec2) Cross(other Vec2) float32

Cross returns the signed scalar cross product of v and other.

func (Vec2) DVec2

func (v Vec2) DVec2() DVec2

DVec2 converts v to float64 precision.

func (Vec2) Div

func (v Vec2) Div(other Vec2) Vec2

Div divides corresponding components using normal IEEE 754 semantics.

func (Vec2) Dot

func (v Vec2) Dot(other Vec2) float32

func (Vec2) IsFinite

func (v Vec2) IsFinite() bool

func (Vec2) IsInf

func (v Vec2) IsInf() bool

func (Vec2) IsNaN

func (v Vec2) IsNaN() bool

func (Vec2) Magnitude

func (v Vec2) Magnitude() float64

func (Vec2) MagnitudeSquared

func (v Vec2) MagnitudeSquared() float64

func (Vec2) Max

func (v Vec2) Max(other Vec2) Vec2

func (Vec2) MaxComponent

func (v Vec2) MaxComponent() float32

func (Vec2) Min

func (v Vec2) Min(other Vec2) Vec2

func (Vec2) MinComponent

func (v Vec2) MinComponent() float32

func (Vec2) Mul

func (v Vec2) Mul(other Vec2) Vec2

Mul multiplies corresponding components.

func (Vec2) Negated

func (v Vec2) Negated() Vec2

func (Vec2) Normalized

func (v Vec2) Normalized() (Vec2, bool)

Normalized returns a unit vector. It fails when the magnitude is zero or non-finite, including when its float32 calculation overflows.

func (Vec2) ProductComponents

func (v Vec2) ProductComponents() float32

func (Vec2) Reflect

func (v Vec2) Reflect(normal Vec2) (Vec2, bool)

Reflect returns v reflected around normal. Normal need not be unit length. It fails when v or normal is non-finite, normal has zero or overflowed squared magnitude, or the result is non-finite.

func (Vec2) Scale

func (v Vec2) Scale(scalar float32) Vec2

func (Vec2) Sub

func (v Vec2) Sub(other Vec2) Vec2

func (Vec2) SumComponents

func (v Vec2) SumComponents() float32

func (Vec2) SumSquaredComponents

func (v Vec2) SumSquaredComponents() float32

func (Vec2) Vec3

func (v Vec2) Vec3() Vec3

Vec3 promotes v with a zero Z component.

func (Vec2) Vec4

func (v Vec2) Vec4() Vec4

Vec4 promotes v with zero Z and W components.

type Vec3

type Vec3 struct {
	X, Y, Z float32
}

Vec3 is the upstream Vector3 value. Its components use source-compatible float32 precision.

Example
package main

import (
	"fmt"

	"github.com/maloquacious/math3d"
)

func main() {
	direction, ok := math3d.V3(3, 0, 4).Normalized()
	if !ok {
		return
	}

	fmt.Printf("unit: %.1f %.1f %.1f\n", direction.X, direction.Y, direction.Z)
	fmt.Printf("dot: %.1f\n", direction.Dot(math3d.V3(1, 2, 3)))
}
Output:
unit: 0.6 0.0 0.8
dot: 3.0

func RandomVec3

func RandomVec3(index, seed int32) Vec3

RandomVec3 returns a vector whose components use consecutive derived indices.

func SplatV3

func SplatV3(value float32) Vec3

SplatV3 constructs a Vec3 whose components are value.

func V3

func V3(x, y, z float32) Vec3

V3 constructs a Vec3.

func (Vec3) Add

func (v Vec3) Add(other Vec3) Vec3

func (Vec3) AlmostEqual

func (v Vec3) AlmostEqual(other Vec3, tolerance float32) bool

func (Vec3) Angle

func (v Vec3) Angle(other Vec3) (float32, bool)

Angle returns the smaller angle between v and other in radians. It fails when either vector is zero or non-finite, an intermediate overflows, or no finite angle can be calculated.

func (Vec3) CatmullRom

func (v Vec3) CatmullRom(value2, value3, value4 Vec3, amount float32) Vec3

CatmullRom interpolates between value2 and value3 using v and value4 as the neighboring control values. Amount is not clamped.

func (Vec3) Cross

func (v Vec3) Cross(other Vec3) Vec3

func (Vec3) DVec3

func (v Vec3) DVec3() DVec3

func (Vec3) Div

func (v Vec3) Div(other Vec3) Vec3

func (Vec3) Dot

func (v Vec3) Dot(other Vec3) float32

func (Vec3) Hermite

func (v Vec3) Hermite(tangent1, value2, tangent2 Vec3, amount float32) Vec3

Hermite interpolates from v with tangent1 to value2 with tangent2. Amount is not clamped.

func (Vec3) IsBackFace

func (v Vec3) IsBackFace(lineOfSight Vec3) bool

IsBackFace reports whether normal faces away from lineOfSight, represented by a strict negative dot product. The vectors need not be normalized; zero and non-finite inputs return false.

func (Vec3) IsCollinear

func (v Vec3) IsCollinear(other Vec3, tolerance float32) bool

IsCollinear reports whether the smaller angle between the vectors, ignoring direction, is at most tolerance radians. Unlike upstream's defective signed-angle test, it works in three dimensions and treats opposite vectors as collinear. Zero, non-finite, and overflowed vectors return false.

func (Vec3) IsFinite

func (v Vec3) IsFinite() bool

func (Vec3) IsInf

func (v Vec3) IsInf() bool

func (Vec3) IsNaN

func (v Vec3) IsNaN() bool

func (Vec3) IsPerpendicular

func (v Vec3) IsPerpendicular(other Vec3, tolerance float32) bool

IsPerpendicular reports whether both vectors are finite and nonzero and the absolute value of their dot product is strictly less than tolerance. Like upstream, tolerance is absolute and therefore depends on vector scale.

func (Vec3) Magnitude

func (v Vec3) Magnitude() float64

func (Vec3) MagnitudeSquared

func (v Vec3) MagnitudeSquared() float64

func (Vec3) Max

func (v Vec3) Max(other Vec3) Vec3

func (Vec3) MaxComponent

func (v Vec3) MaxComponent() float32

func (Vec3) Min

func (v Vec3) Min(other Vec3) Vec3

func (Vec3) MinComponent

func (v Vec3) MinComponent() float32

func (Vec3) Mul

func (v Vec3) Mul(other Vec3) Vec3

func (Vec3) Negated

func (v Vec3) Negated() Vec3

func (Vec3) Normalized

func (v Vec3) Normalized() (Vec3, bool)

func (Vec3) ProductComponents

func (v Vec3) ProductComponents() float32

func (Vec3) Projection

func (v Vec3) Projection(other Vec3) (Vec3, bool)

Projection returns the projection of v onto other. It fails when either vector is non-finite, other has zero or overflowed squared magnitude, or the result is non-finite.

func (Vec3) Reflect

func (v Vec3) Reflect(normal Vec3) (Vec3, bool)

Reflect returns v reflected around normal. Normal need not be unit length. It has the same failure conditions as Projection.

func (Vec3) Rejection

func (v Vec3) Rejection(other Vec3) (Vec3, bool)

Rejection returns the component of v perpendicular to other. It has the same failure conditions as Projection.

func (Vec3) Scale

func (v Vec3) Scale(scalar float32) Vec3

func (Vec3) SignedAngle

func (v Vec3) SignedAngle(other, axis Vec3) (float32, bool)

SignedAngle returns the smaller angle from v to other in radians, with its sign determined by axis·(v×other). It fails when Angle fails, axis is zero or non-finite, or a nonzero angle has no orientation around axis.

func (Vec3) SmoothStep

func (v Vec3) SmoothStep(value2 Vec3, amount float32) Vec3

SmoothStep smoothly interpolates from v to value2, clamping amount to [0, 1].

func (Vec3) Sub

func (v Vec3) Sub(other Vec3) Vec3

func (Vec3) SumComponents

func (v Vec3) SumComponents() float32

func (Vec3) SumSquaredComponents

func (v Vec3) SumSquaredComponents() float32

func (Vec3) Vec4

func (v Vec3) Vec4() Vec4

Vec4 promotes v with a zero W component.

func (Vec3) XY

func (v Vec3) XY() Vec2

func (Vec3) XZ

func (v Vec3) XZ() Vec2

func (Vec3) XZY

func (v Vec3) XZY() Vec3

func (Vec3) YXZ

func (v Vec3) YXZ() Vec3

func (Vec3) YZ

func (v Vec3) YZ() Vec2

func (Vec3) YZX

func (v Vec3) YZX() Vec3

func (Vec3) ZXY

func (v Vec3) ZXY() Vec3

func (Vec3) ZYX

func (v Vec3) ZYX() Vec3

type Vec4

type Vec4 struct {
	X, Y, Z, W float32
}

Vec4 is the upstream Vector4 value. Its components use source-compatible float32 precision.

func RandomVec4

func RandomVec4(index, seed int32) Vec4

RandomVec4 returns a vector whose components use consecutive derived indices.

func SplatV4

func SplatV4(value float32) Vec4

SplatV4 constructs a Vec4 whose components are value.

func V4

func V4(x, y, z, w float32) Vec4

V4 constructs a Vec4.

func (Vec4) Add

func (v Vec4) Add(o Vec4) Vec4

func (Vec4) AlmostEqual

func (v Vec4) AlmostEqual(o Vec4, tolerance float32) bool

func (Vec4) DVec4

func (v Vec4) DVec4() DVec4

func (Vec4) Div

func (v Vec4) Div(o Vec4) Vec4

func (Vec4) Dot

func (v Vec4) Dot(o Vec4) float32

func (Vec4) IsFinite

func (v Vec4) IsFinite() bool

func (Vec4) IsInf

func (v Vec4) IsInf() bool

func (Vec4) IsNaN

func (v Vec4) IsNaN() bool

func (Vec4) Magnitude

func (v Vec4) Magnitude() float64

func (Vec4) MagnitudeSquared

func (v Vec4) MagnitudeSquared() float64

func (Vec4) Max

func (v Vec4) Max(o Vec4) Vec4

func (Vec4) MaxComponent

func (v Vec4) MaxComponent() float32

func (Vec4) Min

func (v Vec4) Min(o Vec4) Vec4

func (Vec4) MinComponent

func (v Vec4) MinComponent() float32

func (Vec4) Mul

func (v Vec4) Mul(o Vec4) Vec4

func (Vec4) Negated

func (v Vec4) Negated() Vec4

func (Vec4) Normalized

func (v Vec4) Normalized() (Vec4, bool)

func (Vec4) ProductComponents

func (v Vec4) ProductComponents() float32

func (Vec4) Scale

func (v Vec4) Scale(s float32) Vec4

func (Vec4) Sub

func (v Vec4) Sub(o Vec4) Vec4

func (Vec4) SumComponents

func (v Vec4) SumComponents() float32

func (Vec4) SumSquaredComponents

func (v Vec4) SumSquaredComponents() float32

func (Vec4) Vec2

func (v Vec4) Vec2() Vec2

Vec2 returns the X and Y components.

func (Vec4) Vec3

func (v Vec4) Vec3() Vec3

Vec3 returns the X, Y, and Z components.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL