math64

package
v0.1.9 Latest Latest
Warning

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

Go to latest
Published: Jun 11, 2026 License: BSD-3-Clause Imports: 6 Imported by: 0

Documentation

Overview

StreamForce 2D

Created by Ilzr Kr.

Licensed under the BSD 3-Clause License. See LICENSE for details.

StreamForce 2D

Created by Ilzr Kr.

Licensed under the BSD 3-Clause License. See LICENSE for details.

StreamForce 2D

Created by Ilzr Kr.

Licensed under the BSD 3-Clause License. See LICENSE for details.

StreamForce 2D

Created by Ilzr Kr.

Licensed under the BSD 3-Clause License. See LICENSE for details.

StreamForce 2D

Created by Ilzr Kr.

Licensed under the BSD 3-Clause License. See LICENSE for details.

StreamForce 2D

Created by Ilzr Kr.

Licensed under the BSD 3-Clause License. See LICENSE for details.

StreamForce 2D

Created by Ilzr Kr.

Licensed under the BSD 3-Clause License. See LICENSE for details.

StreamForce 2D

Created by Ilzr Kr.

Licensed under the BSD 3-Clause License. See LICENSE for details.

StreamForce 2D

Created by Ilzr Kr.

Licensed under the BSD 3-Clause License. See LICENSE for details.

StreamForce 2D

Created by Ilzr Kr.

Licensed under the BSD 3-Clause License. See LICENSE for details.

StreamForce 2D

Created by Ilzr Kr.

Licensed under the BSD 3-Clause License. See LICENSE for details.

StreamForce 2D

Created by Ilzr Kr.

Licensed under the BSD 3-Clause License. See LICENSE for details.

Index

Constants

View Source
const (
	// PI is the value of pi.
	PI float64 = math.Pi

	// RIGHT_ANGLE is the right angle, in radians.
	RIGHT_ANGLE float64 = math.Pi / 2

	// CIRCUMFERENCE is the circumference of a circle, in radians.
	CIRCUMFERENCE float64 = 2 * math.Pi
)
View Source
const (
	// MAX_FLOAT32 is the maximum value of a float32.
	MAX_FLOAT32 float64 = math.MaxFloat32
)

Variables

This section is empty.

Functions

func Abs

func Abs(a float64) float64

Abs returns the absolute value of x.

Special cases are:

Abs(±Inf) = +Inf
Abs(NaN) = NaN

Parameters:

  • a: The value to get the absolute value of.

Returns:

  • float64: The absolute value of the float64.

func AbsoluteAngles

func AbsoluteAngles(v *Vec3) (float64, float64)

AbsoluteAngles returns the yaw and pitch angles of a vector, in radians. If the vector is `nil`, the angles are 0.0.

The angles are relative to the CanonicalAway vector.

Parameters:

  • v: The vector to calculate the angles of.

Returns:

  • float64: The yaw angle of the vector, in radians.
  • float64: The pitch angle of the vector, in radians.

func AngleBetween

func AngleBetween(v1, v2 *Vec3) float64

AngleBetween returns the angle between two vectors, in radians. If either one of them is `nil`, the angle is 0.0.

Parameters:

  • v1: The first vector to calculate the angle between.
  • v2: The second vector to calculate the angle between.

Returns:

  • float64: The angle between the two vectors, in radians.

func AnglesBetween

func AnglesBetween(v1, v2 *Vec3) (float64, float64)

AnglesBetween returns the yaw and pitch angles between two vectors, in radians. If either vector is `nil`, the angles are 0.0.

Parameters:

  • v1: The first vector to calculate the angles between.
  • v2: The other vector to calculate the angles between.

Returns:

  • float64: The yaw angle between the two vectors, in radians.
  • float64: The pitch angle between the two vectors, in radians.

func Atan2

func Atan2(y, x float64) float64

Atan2 returns the arc tangent of y/x, using the signs of the two to determine the quadrant of the return value.

Special cases are (in order):

Atan2(y, NaN) = NaN
Atan2(NaN, x) = NaN
Atan2(+0, x>=0) = +0
Atan2(-0, x>=0) = -0
Atan2(+0, x<=-0) = +PI
Atan2(-0, x<=-0) = -PI
Atan2(y>0, 0) = +PI/2
Atan2(y<0, 0) = -PI/2
Atan2(+Inf, +Inf) = +PI/4
Atan2(-Inf, +Inf) = -PI/4
Atan2(+Inf, -Inf) = 3PI/4
Atan2(-Inf, -Inf) = -3PI/4
Atan2(y, +Inf) = 0
Atan2(y>0, -Inf) = +PI
Atan2(y<0, -Inf) = -PI
Atan2(+Inf, x) = +PI/2
Atan2(-Inf, x) = -PI/2

Parameters:

  • y: The y component.
  • x: The x component.

Returns:

  • float64: The arc tangent of y/x.

func BinarySearch

func BinarySearch(s []float64, target float64) (uint32, bool)

BinarySearch searches for a target in a sorted slice and returns the lastest position where it is found, or the position where it would appear in the sort order.

The slice must be sorted in increasing order for BinarySearch to work. Also, if the slice is too big, then only the first slices.MAX_SLICE_SIZE elements are considered.

Parameters:

  • s: The slice to search in.
  • target: The target to search for.

Returns:

  • uint32: The position where the target is found, or the position where it would appear in the sort order. It is never greater than slices.MAX_SLICE_SIZE.
  • bool: `true` if the target is found in the slice, `false` otherwise.

func BisectAngle

func BisectAngle(a, b float64) float64

BisectAngle returns the angle between two angles, in radians.

Parameters:

  • a: The first angle, in radians.
  • b: The second angle, in radians.

Returns:

  • float64: The angle between the two angles, in radians.

func CrossPoints

func CrossPoints(ax, ay, az float64, bx, by, bz float64) (float64, float64, float64)

CrossPoints is an optimization function that returns the cross product between two points.

Parameters:

  • ax: The X coordinate of the first point.
  • ay: The Y coordinate of the first point.
  • az: The Z coordinate of the first point.
  • bx: The X coordinate of the second point.
  • by: The Y coordinate of the second point.
  • bz: The Z coordinate of the second point.

Returns:

  • float64: The resulting X coordinate.
  • float64: The resulting Y coordinate.
  • float64: The resulting Z coordinate.

func Degrees

func Degrees(radians float64) float64

Degrees converts radians to degrees.

Parameters:

  • radians: The radians to convert.

Returns:

  • float64: The converted degrees.

func DotPoints

func DotPoints(ax, ay, az float64, bx, by, bz float64) float64

DotPoints is an optimization function that returns the dot product between two points.

Parameters:

  • ax: The X coordinate of the first point.
  • ay: The Y coordinate of the first point.
  • az: The Z coordinate of the first point.
  • bx: The X coordinate of the second point.
  • by: The Y coordinate of the second point.
  • bz: The Z coordinate of the second point.

Returns:

  • float64: The dot product of the two points.

func Hypot added in v0.1.9

func Hypot(p, q float64) float64

Hypot returns Sqrt(p*p + q*q), taking care to avoid unnecessary overflow and underflow.

Special cases are:

Hypot(±Inf, q) = +Inf
Hypot(p, ±Inf) = +Inf
Hypot(NaN, q) = NaN
Hypot(p, NaN) = NaN

Parameters:

  • p: The first value.
  • q: The second value.

Returns:

  • float64: The hypotenuse.

func Max

func Max(a, b float64) float64

Max returns the larger of x or y.

Special cases are:

Max(x, +Inf) = Max(+Inf, x) = +Inf
Max(x, NaN) = Max(NaN, x) = NaN
Max(+0, ±0) = Max(±0, +0) = +0
Max(-0, -0) = -0

Parameters:

  • a: The first value.
  • b: The second value.

Returns:

  • float64: The maximum of the two values.

func Min

func Min(a, b float64) float64

Min returns the smaller of x or y.

Special cases are:

Min(x, -Inf) = Min(-Inf, x) = -Inf
Min(x, NaN) = Min(NaN, x) = NaN
Min(-0, ±0) = Min(±0, -0) = -0

Parameters:

  • a: The first value.
  • b: The second value.

Returns:

  • float64: The minimum of the two values.

func NearlyBetween

func NearlyBetween(min, value, max float64) bool

NearlyBetween reports whether the given value is within the given range.

The range is inclusive and, if min > max, min and max are swapped.

Parameters:

  • min: The minimum value.
  • value: The value to check.
  • max: The maximum value.

Returns:

  • bool: `true` if the value is within the range, `false` otherwise.

func NearlyCompare

func NearlyCompare(a, b float64) types.Result

NearlyCompare compares the two given values.

Parameters:

  • a: The first value.
  • b: The second value.

Returns:

  • types.Result: The result of the comparison.

func NearlyEqual

func NearlyEqual(a, b float64) bool

NearlyEqual reports whether the given values are nearly equal.

Parameters:

  • a: The first value.
  • b: The second value.

Returns:

  • bool: `true` if the values are nearly equal, `false` otherwise.

func NearlyGreater

func NearlyGreater(a, b float64) bool

NearlyGreater reports whether the first value is nearly greater than the second value.

Parameters:

  • a: The first value.
  • b: The second value.

Returns:

  • bool: `true` if the first value is greater than the second value, `false` otherwise.

func NearlyLess

func NearlyLess(a, b float64) bool

NearlyLess reports whether the first value is nearly less than the second value, with respect to the given epsilon value.

Parameters:

  • a: The first value.
  • b: The second value.

Returns:

  • bool: `true` if the first value is less than the second value, `false` otherwise.

func NearlyMax

func NearlyMax(a, b float64) float64

NearlyMax returns the maximum of the two given values.

Parameters:

  • a: The first value.
  • b: The second value.

Returns:

  • float64: The maximum value.

func NearlyMin

func NearlyMin(a, b float64) float64

NearlyMin returns the minimum of the two given values.

Parameters:

  • a: The first value.
  • b: The second value.

Returns:

  • float64: The minimum value.

func NearlyNormal

func NearlyNormal(value float64) bool

NearlyNormal reports whether the given value is nearly normalized.

A normalized value is between 0 and 1, inclusive.

Parameters:

  • value: The value to check.

Returns:

  • bool: `true` if the value is normalized, `false` otherwise.

func NearlyZero

func NearlyZero(a float64) bool

NearlyZero is a convenience function to check if a float64 is close to zero.

Parameters:

  • a: The value to check.

Returns:

  • bool: `true` if the value is close to zero, `false` otherwise.

func NormalizeAngle

func NormalizeAngle(angle float64) float64

NormalizeAngle normalizes an angle to the range 0 to 2π, in radians.

Parameters:

  • angle: The angle to normalize, in radians.

Returns:

  • float64: The normalized angle.

func NotZero added in v0.1.9

func NotZero(value float64) float64

NotZero returns the given value if it is not zero. If it is, it returns the epsilon value.

Parameters:

  • value: The value to check.

Returns:

  • float64: The value if it is not zero, the epsilon value otherwise.

func PolygonArea2D

func PolygonArea2D(poly []*Vec3) float64

func Radians

func Radians(degrees float64) float64

Radians converts degrees to radians.

Parameters:

  • degrees: The degrees to convert.

Returns:

  • float64: The converted radians.

func SubtractPoints

func SubtractPoints(ax, ay, az float64, bx, by, bz float64) (float64, float64, float64)

SubtractPoints is an optimization function that returns the difference between two points.

Parameters:

  • ax: The X coordinate of the first point.
  • ay: The Y coordinate of the first point.
  • az: The Z coordinate of the first point.
  • bx: The X coordinate of the second point.
  • by: The Y coordinate of the second point.
  • bz: The Z coordinate of the second point.

Returns:

  • float64: The resulting X coordinate.
  • float64: The resulting Y coordinate.
  • float64: The resulting Z coordinate.

Types

type Plane

type Plane struct {
	// contains filtered or unexported fields
}

Plane represents a plane in 3D space.

func NewPlane

func NewPlane(x, y, z float64, d float64) Plane

NewPlane creates a new plane with the given normal and distance.

Parameters:

  • x: The X component of the normal vector.
  • y: The Y component of the normal vector.
  • z: The Z component of the normal vector.
  • d: The distance from the origin to the plane.

Returns:

  • Plane: The newly created plane.

func (*Plane) IntersectLine

func (p *Plane) IntersectLine(a, b *Vec3) *Vec3

IntersectLine returns the intersection point of the plane and the line defined by the two given points.

Parameters:

  • a: The first point of the line.
  • b: The second point of the line.

Returns:

  • *Vec3: The intersection point. It is never `nil`.

type Quaternion

type Quaternion struct {
	// C is the scalar component.
	C float64

	// X is the i component.
	X float64

	// Y is the j component.
	Y float64

	// Z is the k component.
	Z float64
}

Quaternion represents a quaternion in the form W + X*i + Y*j + Z*k.

It uses the RSG cordinate system.

func AxisAngle

func AxisAngle(axis *Vec3, angle float64) Quaternion

AxisAngle returns the quaternion corresponding to the axis-angle rotation.

Parameters:

  • axis: The axis of rotation.
  • angle: The angle of rotation.

Returns:

  • Quaternion: The quaternion corresponding to the axis-angle rotation.

func FromAxisAngle

func FromAxisAngle(axis *Vec3, angle float64) Quaternion

FromAxisAngle returns a Quaternion corresponding to the axis-angle rotation.

Parameters:

  • axis: The axis of rotation.
  • angle: The angle of rotation.

Returns:

  • Quaternion: The newly created quaternion.

func FromEuler

func FromEuler(phi, theta, psi float64) Quaternion

FromEuler returns a Quaternion corresponding to Euler angles phi, theta, psi.

Parameters:

  • phi: The phi angle.
  • theta: The theta angle.
  • psi: The psi angle.

Returns:

  • Quaternion: The newly created quaternion.

func FromToRotation

func FromToRotation(from, to *Vec3) Quaternion

FromToRotation returns the quaternion corresponding to the rotation from one vector to another.

Parameters:

  • from: The vector to rotate from.
  • to: The vector to rotate to.

Returns:

  • Quaternion: The quaternion corresponding to the rotation from one vector to another.

func LookRotation

func LookRotation(forward, up *Vec3) Quaternion

LookRotation returns the quaternion corresponding to the rotation from one vector to another.

Parameters:

  • forward: The forward vector.
  • up: The up vector.

Returns:

  • Quaternion: The quaternion corresponding to the rotation from one vector to another.

func NewQuaternion

func NewQuaternion(c float64, x, y, z float64) Quaternion

NewQuaternion creates a new quaternion with the given components.

Parameters:

  • c: The scalar component.
  • x: The i component.
  • y: The j component.
  • z: The k component.

Returns:

  • Quaternion: The newly created quaternion.

func QProd

func QProd(qs ...*Quaternion) Quaternion

QProd returns the non-commutative product of quaternions.

Quaternions that are `nil` are ignored.

Parameters:

  • qs: The quaternions to multiply.

Returns:

  • Quaternion: The product of the quaternions.

func QSum

func QSum(qs ...*Quaternion) Quaternion

QSum returns the vectorial summation of quaternions.

Quaternions that are `nil` are ignored.

Parameters:

  • qs: The quaternions to sum.

Returns:

  • Quaternion: The sum of the quaternions.

func (*Quaternion) Conjugate

func (q *Quaternion) Conjugate() Quaternion

Conjugate returns the conjugate of a Quaternion. If the receiver is `nil`, the zero value is returned.

Returns:

  • Quaternion: The conjugate of the receiver.

func (*Quaternion) Equals

func (q *Quaternion) Equals(other *Quaternion) bool

Equals reports whether the two Quaternions are equal. If either one of them is `nil`, the two Quaternions are not equal.

Parameters:

  • other: The other Quaternion.

Returns:

  • bool: `true` if the Quaternions are equal, `false` otherwise.

func (*Quaternion) Euler

func (q *Quaternion) Euler() (float64, float64, float64)

Euler returns the Eulerian angles phi, theta, psi corresponding to a Quaternion, in radians. If the receiver is `nil`, `(0.0, 0.0, 0.0)` is returned.

Returns:

  • float64: The phi angle.
  • float64: The theta angle.
  • float64: The psi angle.

func (*Quaternion) Inverse

func (q *Quaternion) Inverse() Quaternion

Inverse returns the inverse of a Quaternion. If the receiver is `nil`, the zero value is returned.

Returns:

  • Quaternion: The inverse of the receiver.

func (*Quaternion) Negative

func (q *Quaternion) Negative() Quaternion

Negative returns a quaternion with all components negated. If the receiver is `nil`, the zero value is returned.

Returns:

  • Quaternion: The negative of the receiver.

func (*Quaternion) Norm

func (q *Quaternion) Norm() float64

Norm returns the L1-Norm of a Quaternion. If the receiver is `nil`, 0.0 is returned.

Returns:

  • float64: The L1-Norm of the receiver.

func (*Quaternion) Norm2

func (q *Quaternion) Norm2() float64

Norm2 returns the L2-Norm of a Quaternion. If the receiver is `nil`, 0.0 is returned.

Returns:

  • float64: The L2-Norm of the receiver.

func (*Quaternion) RotMat

func (q *Quaternion) RotMat() [3][3]float64

RotMat returns the rotation matrix corresponding to a Quaternion. If the receiver is `nil`, the identity matrix is returned.

Returns:

  • [3][3]float64: The rotation matrix.

func (*Quaternion) Unit

func (q *Quaternion) Unit() Quaternion

Unit returns the Quaternion rescaled to unit-L1-norm. If the receiver is `nil`, the zero value is returned.

Returns:

  • Quaternion: The unit quaternion of the receiver.

type Triangle

type Triangle struct {
	// A is the first vector of the triangle.
	A *Vec3

	// B is the second vector of the triangle.
	B *Vec3

	// C is the third vector of the triangle.
	C *Vec3
}

Triangle is a triangle in 3D space.

func NewTriangle

func NewTriangle(a, b, c *Vec3) *Triangle

NewTriangle returns a new 3D triangle with the given vectors. No triangle is created if any of the vectors is `nil`.

Parameters:

  • a: The first vector of the triangle.
  • b: The second vector of the triangle.
  • c: The third vector of the triangle.

Returns:

  • *Triangle: The newly created triangle.

func (*Triangle) ContainsVector

func (t *Triangle) ContainsVector(v *Vec3) bool

ContainsVector reports whether the given vector is inside the triangle. If the receiver is `nil`, the triangle contains no vectors.

If the vector is `nil`, it is not inside the triangle.

Parameters:

  • v: The vector to check.

Returns:

  • bool: `true` if the vector is inside the triangle, `false` otherwise.

type Vec2

type Vec2 struct {
	// X is the x component of the vector.
	X float64

	// Y is the y component of the vector.
	Y float64
}

Vec2 represents a vector in 2D space.

It uses the RSG cordinate system.

func NewVec2

func NewVec2(x, y float64) Vec2

NewVec2 creates a new vector with the given components.

Parameters:

  • x: The x component of the vector.
  • y: The y component of the vector.

Returns:

  • Vec2: The newly created vector.

func (*Vec2) Add

func (v1 *Vec2) Add(v2 *Vec2) Vec2

Add returns the sum of the two vectors. If the receiver is `nil`, it is treated as the zero vector.

If the other vector is `nil`, it is treated as the zero vector.

Parameters:

  • v2: The other vector to perform the addition with.

Returns:

  • Vec2: The sum of the two vectors.

func (*Vec2) Copy

func (v1 *Vec2) Copy() Vec2

Copy returns a copy of the vector. If the receiver is `nil`, the zero vector is returned.

Returns:

  • Vec2: The copy of the vector.

func (*Vec2) Cross

func (v1 *Vec2) Cross(v2 *Vec2) float64

Cross performs the left-handed cross product between the two vectors. If either one of them is `nil`, the result is 0.0.

Parameters:

  • v2: The other vector to perform the cross product with.

Returns:

  • float64: The cross product of the two vectors.

func (*Vec2) Distance

func (v1 *Vec2) Distance(v2 *Vec2) float64

Distance returns the distance between the two vectors. If the receiver is `nil`, it is treated as the zero vector.

If the other vector is `nil`, it is treated as the zero vector.

Parameters:

  • v2: The other vector to compare with.

Returns:

  • float64: The distance between the two vectors.

func (*Vec2) Dot

func (v1 *Vec2) Dot(v2 *Vec2) float64

Dot returns the dot product between the two vectors. If either one of them is `nil`, the dot product is 0.0.

Parameters:

  • v2: The other vector to perform the dot product with.

Returns:

  • float64: The dot product of the two vectors.

func (*Vec2) Equals

func (v1 *Vec2) Equals(v2 *Vec2) bool

Equals reports whether the given vector is equal to the receiver. If either one of them is `nil`, the two vectors are not equal.

Parameters:

  • v2: The other vector to compare with.

Returns:

  • bool: `true` if the vectors are equal, `false` otherwise.

func (*Vec2) Length

func (v1 *Vec2) Length() float64

Length returns the length of the vector. If the receiver is `nil`, 0.0 is returned.

Returns:

  • float64: The length of the vector.

func (*Vec2) Max

func (v1 *Vec2) Max(v2 *Vec2) Vec2

Max returns the maximum of the two vectors. If the receiver is `nil`, it is treated as the zero vector.

If the other vector is `nil`, it is treated as the zero vector.

Parameters:

  • v2: The other vector to compare with.

Returns:

  • Vec2: The maximum vector.

func (*Vec2) Min

func (v1 *Vec2) Min(v2 *Vec2) Vec2

Min returns the minimum of the two vectors. If the receiver is `nil`, it is treated as the zero vector.

If the other vector is `nil`, it is treated as the zero vector.

Parameters:

  • v2: The other vector to compare with.

Returns:

  • Vec2: The minimum vector.

func (*Vec2) MulScalar

func (v1 *Vec2) MulScalar(s float64) Vec2

MulScalar returns the product of the vector and the scalar. If the receiver is `nil`, the zero vector is returned.

Parameters:

  • s: The scalar to perform the multiplication with.

Returns:

  • Vec2: The product of the vector and the scalar.

func (*Vec2) Multiply

func (v1 *Vec2) Multiply(v2 *Vec2) Vec2

Multiply returns the product of the two vectors. If either one of them is `nil`, the zero vector is returned.

Parameters:

  • v2: The other vector to perform the multiplication with.

Returns:

  • Vec2: The product of the two vectors.

func (Vec2) String

func (v1 Vec2) String() string

String implements fmt.Stringer.

func (*Vec2) Sub

func (v1 *Vec2) Sub(v2 *Vec2) Vec2

Sub returns the difference of the two vectors. If the receiver is `nil`, it is treated as the zero vector.

If the other vector is `nil`, it is treated as the zero vector.

Parameters:

  • v2: The other vector to perform the subtraction with.

Returns:

  • Vec2: The difference of the two vectors.

func (*Vec2) Unit

func (v1 *Vec2) Unit() Vec2

Unit returns the unit vector of the vector. If the receiver is `nil`, the zero vector is returned.

Returns:

  • Vec2: The unit vector of the vector.

type Vec3

type Vec3 struct {
	// X is the x component of the vector.
	X float64

	// Y is the y component of the vector.
	Y float64

	// Z is the z component of the vector.
	Z float64
}

Vec3 represents a vector in 3D space.

It uses the RSG cordinate system.

var (
	// CanonicalRight is the right canonical vector in 3D space (i.e. the
	// positive X axis).
	CanonicalRight Vec3 = NewVec3(1.0, 0.0, 0.0)

	// CanonicalLeft is the left canonical vector in 3D space (i.e. the
	// negative X axis).
	CanonicalLeft Vec3 = NewVec3(-1.0, 0.0, 0.0)

	// CanonicalDown is the down canonical vector in 3D space (i.e. the
	// positive Y axis).
	CanonicalDown Vec3 = NewVec3(0.0, 1.0, 0.0)

	// CanonicalUp is the up canonical vector in 3D space (i.e. the
	// negative Y axis).
	CanonicalUp Vec3 = NewVec3(0.0, -1.0, 0.0)

	// CanonicalToward is the toward canonical vector in 3D space (i.e.
	// the positive Z axis).
	CanonicalToward Vec3 = NewVec3(0.0, 0.0, 1.0)

	// CanonicalAway is the away canonical vector in 3D space (i.e. the
	// negative Z axis).
	CanonicalAway Vec3 = NewVec3(0.0, 0.0, -1.0)
)

func DirectionFromAngles

func DirectionFromAngles(yaw, pitch float64) Vec3

DirectionFromAngles returns the derection vector corresponding to the provided yaw and pitch angles, in radians.

Parameters:

  • yaw: The yaw angle, in radians.
  • pitch: The pitch angle, in radians.

Returns:

  • Vec3: The direction vector corresponding to the provided angles.

func LineIntersection3D

func LineIntersection3D(a, b, c, d *Vec3) (Vec3, bool)

LineIntersection3D returns the intersection point of two lines in 3D.

If any of the points is `nil`, it is treated as the zero vector.

Parameters:

  • a: The first point of the first line.
  • b: The second point of the first line.
  • c: The first point of the second line.
  • d: The second point of the second line.

Returns:

  • Vec3: The intersection point. It is never `nil`.
  • bool: `true` if the lines intersect, `false` otherwise.

func NewVec3

func NewVec3(x, y, z float64) Vec3

NewVec3 creates a new vector with the given components.

Parameters:

  • x: The x component of the vector.
  • y: The y component of the vector.
  • z: The z component of the vector.

Returns:

  • Vec3: The newly created vector.

func OffsetPolygon

func OffsetPolygon(poly []*Vec3, distance float64) []*Vec3

Returns outward offset polygon. distance > 0 expands distance < 0 contracts

func PointTowards

func PointTowards(v1, v2 *Vec3, distance float64) Vec3

PointTowards returns a vector that is `distance` units towards the other vector.

If either vector is `nil`, it is treated as the zero vector.

Parameters:

  • v1: The first vector.
  • v2: The second vector.
  • distance: The distance to move the vector.

Returns:

  • Vec3: The new vector.

func PolygonNormal

func PolygonNormal(poly []*Vec3) Vec3

Estimate polygon plane normal from vertices.

func RotateVec3

func RotateVec3(q *Quaternion, vec *Vec3) Vec3

RotateVec3 returns the vector rotated by the quaternion.

If the vector is `nil`, it is treated as the zero vector. If the quaternion is `nil`, no rotation is performed.

Parameters:

  • q: The quaternion to rotate.
  • vec: The vector to rotate.

Returns:

  • Vec3: The rotated vector.

func (*Vec3) Add

func (v1 *Vec3) Add(v2 *Vec3) Vec3

Add returns the sum of the two vectors. If the receiver is `nil`, it is treated as the zero vector.

If the other vector is `nil`, it is treated as the zero vector.

Parameters:

  • v2: The other vector to perform the addition with.

Returns:

  • Vec3: The sum of the two vectors.

func (*Vec3) Copy

func (v1 *Vec3) Copy() Vec3

Copy returns a copy of the vector. If the receiver is `nil`, the zero vector is returned.

Returns:

  • Vec3: The copy of the vector.

func (*Vec3) Cross

func (v1 *Vec3) Cross(v2 *Vec3) Vec3

Cross performs the left-handed cross product between the two vectors. If either one of them is `nil`, the zero vector is returned.

Parameters:

  • v2: The other vector to perform the cross product with.

Returns:

  • Vec3: The cross product of the two vectors.

func (*Vec3) Distance

func (v1 *Vec3) Distance(v2 *Vec3) float64

Distance returns the distance between the two vectors. If the receiver is `nil`, it is treated as the zero vector.

If the other vector is `nil`, it is treated as the zero vector.

Parameters:

  • v2: The other vector to compare with.

Returns:

  • float64: The distance between the two vectors.

func (*Vec3) Dot

func (v1 *Vec3) Dot(v2 *Vec3) float64

Dot returns the dot product between the two vectors. If either one of them is `nil`, the dot product is 0.0.

Parameters:

  • v2: The other vector to perform the dot product with.

Returns:

  • float64: The dot product of the two vectors.

func (*Vec3) Equals

func (v1 *Vec3) Equals(v2 *Vec3) bool

Equals reports whether the given vector is equal to the receiver. If either one of them is `nil`, the two vectors are not equal.

Parameters:

  • v2: The other vector to compare with.

Returns:

  • bool: `true` if the vectors are equal, `false` otherwise.

func (*Vec3) Length

func (v1 *Vec3) Length() float64

Length returns the length of the vector. If the receiver is `nil`, 0.0 is returned.

Returns:

  • float64: The length of the vector.

func (*Vec3) Max

func (v1 *Vec3) Max(v2 *Vec3) Vec3

Max returns the maximum of the two vectors. If the receiver is `nil`, it is treated as the zero vector.

If the other vector is `nil`, it is treated as the zero vector.

Parameters:

  • v2: The other vector to compare with.

Returns:

  • Vec3: The maximum vector.

func (*Vec3) Min

func (v1 *Vec3) Min(v2 *Vec3) Vec3

Min returns the minimum of the two vectors. If the receiver is `nil`, it is treated as the zero vector.

If the other vector is `nil`, it is treated as the zero vector.

Parameters:

  • v2: The other vector to compare with.

Returns:

  • Vec3: The minimum vector.

func (*Vec3) MulScalar

func (v1 *Vec3) MulScalar(s float64) Vec3

MulScalar returns the product of the vector and the scalar. If the receiver is `nil`, the zero vector is returned.

Parameters:

  • s: The scalar to perform the multiplication with.

Returns:

  • Vec3: The product of the vector and the scalar.

func (*Vec3) Multiply

func (v1 *Vec3) Multiply(v2 *Vec3) Vec3

Multiply returns the product of the two vectors. If either one of them is `nil`, the zero vector is returned.

Parameters:

  • v2: The other vector to perform the multiplication with.

Returns:

  • Vec3: The product of the two vectors.

func (Vec3) String

func (v1 Vec3) String() string

String implements fmt.Stringer.

func (*Vec3) Sub

func (v1 *Vec3) Sub(v2 *Vec3) Vec3

Sub returns the difference of the two vectors. If the receiver is `nil`, it is treated as the zero vector.

If the other vector is `nil`, it is treated as the zero vector.

Parameters:

  • v2: The other vector to perform the subtraction with.

Returns:

  • Vec3: The difference of the two vectors.

func (*Vec3) Unit

func (v1 *Vec3) Unit() Vec3

Unit returns the unit vector of the vector. If the receiver is `nil`, the zero vector is returned.

Returns:

  • Vec3: The unit vector of the vector.

func (*Vec3) ZDiff

func (v1 *Vec3) ZDiff(v2 *Vec3) float64

ZDiff returns the difference between the Z components of the two vectors. If the receiver is `nil`, it is treated as the zero vector.

If the other vector is `nil`, it is treated as the zero vector.

Parameters:

  • v2: The other vector to compare with.

Returns:

  • float64: The difference between the Z components of the two vectors.

type Vec4

type Vec4 struct {
	// X is the x component of the vector.
	X float64

	// Y is the y component of the vector.
	Y float64

	// Z is the z component of the vector.
	Z float64

	// W is the w component of the vector.
	W float64
}

Vec4 represents a vector in 4D space.

It uses the RSG cordinate system.

func NewVec4

func NewVec4(x, y, z, w float64) Vec4

NewVec4 creates a new vector with the given components.

Parameters:

  • x: The x component of the vector.
  • y: The y component of the vector.
  • z: The z component of the vector.
  • w: The w component of the vector.

Returns:

  • Vec4: The newly created vector.

func (*Vec4) Add

func (v1 *Vec4) Add(v2 *Vec4) Vec4

Add returns the sum of the two vectors. If the receiver is `nil`, it is treated as the zero vector.

If the other vector is `nil`, it is treated as the zero vector.

Parameters:

  • v2: The other vector to perform the addition with.

Returns:

  • Vec4: The sum of the two vectors.

func (*Vec4) Copy

func (v1 *Vec4) Copy() Vec4

Copy returns a copy of the vector. If the receiver is `nil`, the zero vector is returned.

Returns:

  • Vec4: The copy of the vector.

func (*Vec4) Distance

func (v1 *Vec4) Distance(v2 *Vec4) float64

Distance returns the distance between the two vectors. If the receiver is `nil`, it is treated as the zero vector.

If the other vector is `nil`, it is treated as the zero vector.

Parameters:

  • v2: The other vector to compare with.

Returns:

  • float64: The distance between the two vectors.

func (*Vec4) Dot

func (v1 *Vec4) Dot(v2 *Vec4) float64

Dot returns the dot product between the two vectors. If either one of them is `nil`, the dot product is 0.0.

Parameters:

  • v2: The other vector to perform the dot product with.

Returns:

  • float64: The dot product of the two vectors.

func (*Vec4) Equals

func (v1 *Vec4) Equals(v2 *Vec4) bool

Equals reports whether the given vector is equal to the receiver. If either one of them is `nil`, the two vectors are not equal.

Parameters:

  • v2: The other vector to compare with.

Returns:

  • bool: `true` if the vectors are equal, `false` otherwise.

func (*Vec4) Length

func (v1 *Vec4) Length() float64

Length returns the length of the vector. If the receiver is `nil`, 0.0 is returned.

Returns:

  • float64: The length of the vector.

func (*Vec4) Max

func (v1 *Vec4) Max(v2 *Vec4) Vec4

Max returns the maximum of the two vectors. If the receiver is `nil`, it is treated as the zero vector.

If the other vector is `nil`, it is treated as the zero vector.

Parameters:

  • v2: The other vector to compare with.

Returns:

  • Vec4: The maximum vector.

func (*Vec4) Min

func (v1 *Vec4) Min(v2 *Vec4) Vec4

Min returns the minimum of the two vectors. If the receiver is `nil`, it is treated as the zero vector.

If the other vector is `nil`, it is treated as the zero vector.

Parameters:

  • v2: The other vector to compare with.

Returns:

  • Vec4: The minimum vector.

func (*Vec4) MulScalar

func (v1 *Vec4) MulScalar(s float64) Vec4

MulScalar returns the product of the vector and the scalar. If the receiver is `nil`, the zero vector is returned.

Parameters:

  • s: The scalar to perform the multiplication with.

Returns:

  • Vec4: The product of the vector and the scalar.

func (*Vec4) Multiply

func (v1 *Vec4) Multiply(v2 *Vec4) Vec4

Multiply returns the product of the two vectors. If either one of them is `nil`, the zero vector is returned.

Parameters:

  • v2: The other vector to perform the multiplication with.

Returns:

  • Vec4: The product of the two vectors.

func (Vec4) String

func (v1 Vec4) String() string

String implements fmt.Stringer.

func (*Vec4) Sub

func (v1 *Vec4) Sub(v2 *Vec4) Vec4

Sub returns the difference of the two vectors. If the receiver is `nil`, it is treated as the zero vector.

If the other vector is `nil`, it is treated as the zero vector.

Parameters:

  • v2: The other vector to perform the subtraction with.

Returns:

  • Vec4: The difference of the two vectors.

func (*Vec4) Unit

func (v1 *Vec4) Unit() Vec4

Unit returns the unit vector of the vector. If the receiver is `nil`, the zero vector is returned.

Returns:

  • Vec4: The unit vector of the vector.

func (*Vec4) ZDiff

func (v1 *Vec4) ZDiff(v2 *Vec4) float64

ZDiff returns the difference between the Z components of the two vectors. If the receiver is `nil`, it is treated as the zero vector.

If the other vector is `nil`, it is treated as the zero vector.

Parameters:

  • v2: The other vector to compare with.

Returns:

  • float64: The difference between the Z components of the two vectors.

Jump to

Keyboard shortcuts

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