diff --git a/README.md b/README.md index 52a65d8..6b3078b 100644 --- a/README.md +++ b/README.md @@ -134,89 +134,90 @@ in some cases the return type is different. Examples: ## Available Operations -| Operation | Float | Integer | Description | -|-----------------------------|-------|---------|-------------------------------------------------------------------------------------------------------------| -| **Creation** | | | | -| [`...FromArray`](array.go) | ✔️ | ✔️ | Initializes a vector from an array of components. | -| [`...FromSlice`](slice.go) | ✔️ | ✔️ | Initializes a vector from a slice of components. | -| [`Fill`](new.go) | ✔️ | ✔️ | Creates a vector where all components are set to a specified value. | -| [`New`](new.go) | ✔️ | ✔️ | Creates a new vector using the provided components. | -| [`One`](new.go) | ✔️ | ✔️ | Generates a vector with all components set to 1. | -| [`Zero`](new.go) | ✔️ | ✔️ | Generates a vector with all components set to 0. | -| **Conversions** | | | | -| [`Apply`](apply.go) | ✔️ | ✔️ | Applies a given function to each component in the vector. | -| [`ApplyToArray`](array.go) | ✔️ | ✔️ | Modifies an array by assigning values from the vector's corresponding components. | -| [`ApplyToSlice`](slice.go) | ✔️ | ✔️ | Modifies a slice by assigning values from the vector's corresponding components. | -| [`Discard`](discard.go) | ✔️ | ✔️ | Removes a component from the vector at a specified index. | -| [`Extend`](extend.go) | ✔️ | ✔️ | Extends the dimensionality of a vector by adding additional components. | -| [`Insert`](insert.go) | ✔️ | ✔️ | Inserts a value into the vector at a specified index. | -| [`Split`](split.go) | ✔️ | ✔️ | Splits a vector into its individual components. | -| [`Swizzle...`](swizzle.go) | ✔️ | ✔️ | Swaps components of a vector using a selected pattern. | -| [`ToArray`](array.go) | ✔️ | ✔️ | Converts the vector to a new array. | -| [`ToSlice`](slice.go) | ✔️ | ✔️ | Converts the vector to a new slice. | -| `To...` | ✔️ | ✔️ | Transforms the vector into another type. [V2F](vec2f.go), [V3F](vec3f.go), [V2I](vec2i.go), [V3I](vec3i.go) | -| **Mathematics** | | | | -| [`Abs`](abs.go) | ✔️ | ✔️ | Computes the absolute value of each component in the vector. | -| [`Add`](add.go) | ✔️ | ✔️ | Performs vector addition. | -| [`AddScalar`](addScalar.go) | ✔️ | ✔️ | Adds a scalar value to each component of the vector. | -| [`Ceil`](ceil.go) | ✔️ | ✔️ | Rounds each component of the vector up to the nearest integer. | -| [`ClampComp`](clampComp.go) | ✔️ | ✔️ | Clamps the components of this vector to the given range. | -| [`ClampLen`](clampLen.go) | ✔️ | ✔️ | Scales the length of the vector so that it fits within provided range. | -| [`Cross`](cross.go) | ✔️ | ✔️ | Computes the cross product of two vectors. (Not applicable for 2D vectors.) | -| [`Div`](div.go) | ✔️ | ✔️ | Performs vector division. | -| [`DivScalar`](dicScalar.go) | ✔️ | ✔️ | Divides each component of the vector by a scalar. | -| [`Dot`](dot.go) | ✔️ | ✔️ | Computes the dot product of two vectors. | -| [`Floor`](floor.go) | ✔️ | ✔️ | Rounds each component of the vector down to the nearest integer. | -| [`Inv`](inv.go) | ✔️ | ✔️ | Computes the multiplicative inverse of each component in the vector. | -| [`Len`](len.go) | ✔️ | ✔️ | Calculates the length of the vector. Returns `float64` for integer vectors. | -| [`LenSqrt`](lenSqrt.go) | ✔️ | ✔️ | Calculates the squared length of the vector. Returns `float64` for integer vectors. | -| [`Max`](max.go) | ✔️ | ✔️ | Returns the maximum component values from two vectors. | -| [`Min`](min.go) | ✔️ | ✔️ | Returns the minimum component values from two vectors. | -| [`Mod`](mod.go) | ✔️ | ✔️ | Computes the modulus of each component in the vector against another vector components. | -| [`ModScalar`](modScalar.go) | ✔️ | ✔️ | Computes the modulus of each component in the vector against a single scalar. | -| [`Mul`](mul.go) | ✔️ | ✔️ | Performs vector multiplication. | -| [`MulScalar`](mulScalar.go) | ✔️ | ✔️ | Multiplies each component of the vector by a scalar. | -| [`Neg`](neg.go) | ✔️ | ✔️ | Negates each component of the vector. | -| [`Norm`](norm.go) | ✔️ | ✔️ | Normalizes the vector. Returns a `float64` vector for integer vectors. | -| [`Pow`](pow.go) | ✔️ | ✔️ | Raises each component of the vector to the power of the corresponding component in another vector. | -| [`Pow2`](pow2.go) | ✔️ | ✔️ | Squares each component of the vector. | -| [`Pow3`](pow3.go) | ✔️ | ✔️ | Cubes each component of the vector. | -| [`PowN`](powN.go) | ✔️ | ✔️ | Raises each component of the vector to a specified integer power. | -| [`PowNFloat`](powNFloat.go) | ✔️ | ✔️ | Raises each component of the vector to a specified floating-point power. | -| [`Round`](round.go) | ✔️ | ✔️ | Rounds each component of the vector to the nearest integer. | -| [`Sqrt`](sqrt.go) | ✔️ | ✔️ | Computes the square root of each component in the vector. | -| [`Sub`](sub.go) | ✔️ | ✔️ | Performs vector subtraction. | -| [`SubScalar`](subScalar.go) | ✔️ | ✔️ | Subtracts a scalar from each component of the vector. | +| Operation | Description | +|-----------------------------------------|-------------------------------------------------------------------------------------------------------------| +| **Creation** | | +| [`...FromArray`](array.go) | Initializes a vector from an array of components. | +| [`...FromSlice`](slice.go) | Initializes a vector from a slice of components. | +| [`Fill`](new.go) | Creates a vector where all components are set to a specified value. | +| [`New`](new.go) | Creates a new vector using the provided components. | +| [`One`](new.go) | Generates a vector with all components set to 1. | +| [`Zero`](new.go) | Generates a vector with all components set to 0. | +| **Conversions** | | +| [`Apply`](apply.go) | Applies a given function to each component in the vector. | +| [`ApplyToArray`](array.go) | Modifies an array by assigning values from the vector's corresponding components. | +| [`ApplyToSlice`](slice.go) | Modifies a slice by assigning values from the vector's corresponding components. | +| [`Discard`](discard.go) | Removes a component from the vector at a specified index. | +| [`Extend`](extend.go) | Extends the dimensionality of a vector by adding additional components. | +| [`Insert`](insert.go) | Inserts a value into the vector at a specified index. | +| [`Split`](split.go) | Splits a vector into its individual components. | +| [`Swizzle...`](swizzle.go) | Swaps components of a vector using a selected pattern. | +| [`ToArray`](array.go) | Converts the vector to a new array. | +| [`ToSlice`](slice.go) | Converts the vector to a new slice. | +| `To...` | Transforms the vector into another type. [V2F](vec2f.go), [V3F](vec3f.go), [V2I](vec2i.go), [V3I](vec3i.go) | +| **Mathematics** | | +| [`Abs`](abs.go) | Computes the absolute value of each component in the vector. | +| [`Add`](add.go) | Performs vector addition. | +| [`AddScalar`](addScalar.go) | Adds a scalar value to each component of the vector. | +| [`AngleBetweenDeg`](angleBetweenDeg.go) | Calculates the angle between two vectors in degrees. | +| [`AngleBetweenRad`](angleBetweenRad.go) | Calculates the angle between two vectors in radians. | +| [`Ceil`](ceil.go) | Rounds each component of the vector up to the nearest integer. | +| [`ClampComp`](clampComp.go) | Clamps the components of this vector to the given range. | +| [`ClampLen`](clampLen.go) | Scales the length of the vector so that it fits within provided range. | +| [`Cross`](cross.go) | Computes the cross product of two vectors. (Not applicable for 2D vectors.) | +| [`Distance`](distance.go) | Calculates the distance between two vectors. | +| [`Div`](div.go) | Performs vector division. | +| [`DivScalar`](dicScalar.go) | Divides each component of the vector by a scalar. | +| [`Dot`](dot.go) | Computes the dot product of two vectors. | +| [`Floor`](floor.go) | Rounds each component of the vector down to the nearest integer. | +| [`Inv`](inv.go) | Computes the multiplicative inverse of each component in the vector. | +| [`IsZero`](isZero.go) | Checks if the vector is a zero vector. | +| [`Len`](len.go) | Calculates the length of the vector. Returns `float64` for integer vectors. | +| [`LenSqrt`](lenSqrt.go) | Calculates the squared length of the vector. Returns `float64` for integer vectors. | +| [`Max`](max.go) | Returns the maximum component values from two vectors. | +| [`Min`](min.go) | Returns the minimum component values from two vectors. | +| [`Mod`](mod.go) | Computes the modulus of each component in the vector against another vector components. | +| [`ModScalar`](modScalar.go) | Computes the modulus of each component in the vector against a single scalar. | +| [`Mul`](mul.go) | Performs vector multiplication. | +| [`MulScalar`](mulScalar.go) | Multiplies each component of the vector by a scalar. | +| [`Neg`](neg.go) | Negates each component of the vector. | +| [`Norm`](norm.go) | Normalizes the vector. Returns a `float64` vector for integer vectors. | +| [`Pow`](pow.go) | Raises each component of the vector to the power of the corresponding component in another vector. | +| [`Pow2`](pow2.go) | Squares each component of the vector. | +| [`Pow3`](pow3.go) | Cubes each component of the vector. | +| [`PowN`](powN.go) | Raises each component of the vector to a specified integer power. | +| [`PowNFloat`](powNFloat.go) | Raises each component of the vector to a specified floating-point power. | +| [`Round`](round.go) | Rounds each component of the vector to the nearest integer. | +| [`Sqrt`](sqrt.go) | Computes the square root of each component in the vector. | +| [`Sub`](sub.go) | Performs vector subtraction. | +| [`SubScalar`](subScalar.go) | Subtracts a scalar from each component of the vector. | ## Not implemented yet The following list of operations are not implemented yet. Feel free to open an issue if you would like to see any of these implemented or if you'd like to propose a different operation. -| Operation | Planned | Description | -|-------------------|------------|-----------------------------------------------------------------------------------| -| `AngleBetweenDeg` | Yes (v1.0) | Calculates the angle between two vectors in degrees. | -| `AngleBetweenRad` | Yes (v1.0) | Calculates the angle between two vectors in radians. | -| `AngleDeg` | Yes (v1.0) | Creates a normalized vector from an angle in degrees. | -| `AngleRad` | Yes (v1.0) | Creates a normalized vector from an angle in radians. | -| `Average` | Yes (v1.0) | Calculates the average of the vector's components. | -| `Cos` | | Applies the cosine function to all components. | -| `Distance` | Yes (v1.0) | Calculates the distance between two vectors. | -| `FromQuaternion` | | Initializes the vector from a quaternion. | -| `Hash` | | Produces a hash code for the vector for use in data structures. | -| `IsOrthogonalTo` | | Checks if the vector is orthogonal to another vector. | -| `IsUnit` | | Checks if the vector is a unit vector. | -| `IsZero` | Yes (v1.0) | Checks if the vector is a zero vector. | -| `Lerp` | Yes (v1.0) | Linearly interpolates between two vectors. | -| `Orthogonalize` | | Generates an orthogonal (or orthonormal) vector set. | -| `Project` | | Projects a 3D vector onto a plane. | -| `Rand` | Yes (v1.0) | Generates a normal vector with random components. | -| `RandIn` | Yes (v1.0) | Generates a vector with random components within a zero and the specified vector. | -| `RandBetween` | Yes (v1.0) | Generates a vector with random components between two specified vectors. | -| `Reflect` | | Reflects a vector off the plane defined by a normal. | -| `RotateDeg` | Yes (v1.0) | Rotates a vector by an angle in degrees. | -| `RotateRad` | Yes (v1.0) | Rotates a vector by an angle in radians. | -| `Sin` | | Applies the sine function to all components. | -| `Slerp` | | Spherically interpolates between two vectors. | -| `Tan` | | Applies the tangent function to all components. | -| `ToQuaternion` | | Converts the vector to a quaternion. | \ No newline at end of file +| Operation | Planned | Description | +|------------------|------------|-----------------------------------------------------------------------------------| +| `AngleDeg` | Yes (v1.0) | Creates a normalized vector from an angle in degrees. | +| `AngleRad` | Yes (v1.0) | Creates a normalized vector from an angle in radians. | +| `Average` | Yes (v1.0) | Calculates the average of the vector's components. | +| `Cos` | | Applies the cosine function to all components. | +| `FromQuaternion` | | Initializes the vector from a quaternion. | +| `Hash` | | Produces a hash code for the vector for use in data structures. | +| `IsOrthogonalTo` | | Checks if the vector is orthogonal to another vector. | +| `IsUnit` | | Checks if the vector is a unit vector. | +| `Lerp` | Yes (v1.0) | Linearly interpolates between two vectors. | +| `Orthogonalize` | | Generates an orthogonal (or orthonormal) vector set. | +| `Project` | | Projects a 3D vector onto a plane. | +| `Rand` | Yes (v1.0) | Generates a normal vector with random components. | +| `RandIn` | Yes (v1.0) | Generates a vector with random components within a zero and the specified vector. | +| `RandBetween` | Yes (v1.0) | Generates a vector with random components between two specified vectors. | +| `Reflect` | | Reflects a vector off the plane defined by a normal. | +| `Remainder` | | Computes the remainder of each component in the vector against another vector. | +| `RotateDeg` | Yes (v1.0) | Rotates a vector by an angle in degrees. | +| `RotateRad` | Yes (v1.0) | Rotates a vector by an angle in radians. | +| `Sin` | | Applies the sine function to all components. | +| `Slerp` | | Spherically interpolates between two vectors. | +| `Tan` | | Applies the tangent function to all components. | +| `ToQuaternion` | | Converts the vector to a quaternion. | \ No newline at end of file diff --git a/abs.go b/abs.go index d02033f..907d7fc 100644 --- a/abs.go +++ b/abs.go @@ -25,6 +25,7 @@ func (v *V2F[T]) AbsInPlace() { // V3F +// Abs computes the absolute value of each component in the vector. func (v V3F[T]) Abs() V3F[T] { absX := v.X absY := v.Y @@ -41,6 +42,7 @@ func (v V3F[T]) Abs() V3F[T] { return V3F[T]{X: absX, Y: absY, Z: absZ} } +// AbsInPlace computes the absolute value of each component in the vector and updates the vector in place. func (v *V3F[T]) AbsInPlace() { if v.X < 0 { v.X = -v.X @@ -55,6 +57,7 @@ func (v *V3F[T]) AbsInPlace() { // V2I +// Abs computes the absolute value of each component in the vector. func (v V2I[T]) Abs() V2I[T] { absX := v.X absY := v.Y @@ -67,6 +70,7 @@ func (v V2I[T]) Abs() V2I[T] { return V2I[T]{X: absX, Y: absY} } +// AbsInPlace computes the absolute value of each component in the vector and updates the vector in place. func (v *V2I[T]) AbsInPlace() { if v.X < 0 { v.X = -v.X @@ -78,6 +82,7 @@ func (v *V2I[T]) AbsInPlace() { // V3I +// Abs computes the absolute value of each component in the vector. func (v V3I[T]) Abs() V3I[T] { absX := v.X absY := v.Y @@ -94,6 +99,7 @@ func (v V3I[T]) Abs() V3I[T] { return V3I[T]{X: absX, Y: absY, Z: absZ} } +// AbsInPlace computes the absolute value of each component in the vector and updates the vector in place. func (v *V3I[T]) AbsInPlace() { if v.X < 0 { v.X = -v.X diff --git a/array.go b/array.go index 17820c7..741009a 100644 --- a/array.go +++ b/array.go @@ -2,54 +2,66 @@ package govec import "golang.org/x/exp/constraints" +// V2FFromArray returns a new V2F[T] from the specified array. func V2FFromArray[T constraints.Float](a [2]T) V2F[T] { return V2F[T]{X: a[0], Y: a[1]} } +// V3FFromArray returns a new V3F[T] from the specified array. func V3FFromArray[T constraints.Float](a [3]T) V3F[T] { return V3F[T]{X: a[0], Y: a[1], Z: a[2]} } +// V2IFromArray returns a new V2I[T] from the specified array. func V2IFromArray[T constraints.Integer](a [2]T) V2I[T] { return V2I[T]{X: a[0], Y: a[1]} } +// V3IFromArray returns a new V3I[T] from the specified array. func V3IFromArray[T constraints.Integer](a [3]T) V3I[T] { return V3I[T]{X: a[0], Y: a[1], Z: a[2]} } +// ToArray returns an array of the components of the vector. func (v V2F[T]) ToArray() [2]T { return [2]T{v.X, v.Y} } +// ToArray returns an array of the components of the vector. func (v V3F[T]) ToArray() [3]T { return [3]T{v.X, v.Y, v.Z} } +// ToArray returns an array of the components of the vector. func (v V2I[T]) ToArray() [2]T { return [2]T{v.X, v.Y} } +// ToArray returns an array of the components of the vector. func (v V3I[T]) ToArray() [3]T { return [3]T{v.X, v.Y, v.Z} } +// ApplyToArray applies the components of the vector to the specified array. func (v V2F[T]) ApplyToArray(a *[2]T) { a[0] = v.X a[1] = v.Y } +// ApplyToArray applies the components of the vector to the specified array. func (v V3F[T]) ApplyToArray(a *[3]T) { a[0] = v.X a[1] = v.Y a[2] = v.Z } +// ApplyToArray applies the components of the vector to the specified array. func (v V2I[T]) ApplyToArray(a *[2]T) { a[0] = v.X a[1] = v.Y } +// ApplyToArray applies the components of the vector to the specified array. func (v V3I[T]) ApplyToArray(a *[3]T) { a[0] = v.X a[1] = v.Y diff --git a/ceil.go b/ceil.go index 95a6bbb..291334c 100644 --- a/ceil.go +++ b/ceil.go @@ -4,10 +4,12 @@ import "math" // V2F +// Ceil returns a new V2F[T] with the ceil (round up) of each component. func (v V2F[T]) Ceil() V2F[T] { return V2F[T]{X: T(math.Ceil(float64(v.X))), Y: T(math.Ceil(float64(v.Y)))} } +// CeilInPlace modifies vector by setting each component to its ceil (round up). func (v *V2F[T]) CeilInPlace() { v.X = T(math.Ceil(float64(v.X))) v.Y = T(math.Ceil(float64(v.Y))) @@ -15,10 +17,12 @@ func (v *V2F[T]) CeilInPlace() { // V3F +// Ceil returns a new V3F[T] with the ceil (round up) of each component. func (v V3F[T]) Ceil() V3F[T] { return V3F[T]{X: T(math.Ceil(float64(v.X))), Y: T(math.Ceil(float64(v.Y))), Z: T(math.Ceil(float64(v.Z)))} } +// CeilInPlace modifies vector by setting each component to its ceil (round up). func (v *V3F[T]) CeilInPlace() { v.X = T(math.Ceil(float64(v.X))) v.Y = T(math.Ceil(float64(v.Y))) diff --git a/clampComp.go b/clampComp.go index f31f450..78fc97e 100644 --- a/clampComp.go +++ b/clampComp.go @@ -2,19 +2,23 @@ package govec // V2F +// ClampComp returns a new V2F[T] with each component clamped between min and max. func (v V2F[T]) ClampComp(min V2F[T], max V2F[T]) V2F[T] { return V2F[T]{X: clamp(v.X, min.X, max.X), Y: clamp(v.Y, min.Y, max.Y)} } +// ClampCompComp returns a new V2F[T] with each component clamped between the respective min and max. func (v V2F[T]) ClampCompComp(minX T, maxX T, minY T, maxY T) V2F[T] { return V2F[T]{X: clamp(v.X, minX, maxX), Y: clamp(v.Y, minY, maxY)} } +// ClampCompInPlace modifies vector by clamping each component between min and max. func (v *V2F[T]) ClampCompInPlace(min V2F[T], max V2F[T]) { v.X = clamp(v.X, min.X, max.X) v.Y = clamp(v.Y, min.Y, max.Y) } +// ClampCompCompInPlace modifies vector by clamping each component between the respective min and max. func (v *V2F[T]) ClampCompCompInPlace(minX T, maxX T, minY T, maxY T) { v.X = clamp(v.X, minX, maxX) v.Y = clamp(v.Y, minY, maxY) @@ -22,20 +26,24 @@ func (v *V2F[T]) ClampCompCompInPlace(minX T, maxX T, minY T, maxY T) { // V3F +// ClampComp returns a new V3F[T] with each component clamped between min and max. func (v V3F[T]) ClampComp(min V3F[T], max V3F[T]) V3F[T] { return V3F[T]{X: clamp(v.X, min.X, max.X), Y: clamp(v.Y, min.Y, max.Y), Z: clamp(v.Z, min.Z, max.Z)} } +// ClampCompComp returns a new V3F[T] with each component clamped between the respective min and max. func (v V3F[T]) ClampCompComp(minX T, maxX T, minY T, maxY T, minZ T, maxZ T) V3F[T] { return V3F[T]{X: clamp(v.X, minX, maxX), Y: clamp(v.Y, minY, maxY), Z: clamp(v.Z, minZ, maxZ)} } +// ClampCompInPlace modifies vector by clamping each component between min and max. func (v *V3F[T]) ClampCompInPlace(min V3F[T], max V3F[T]) { v.X = clamp(v.X, min.X, max.X) v.Y = clamp(v.Y, min.Y, max.Y) v.Z = clamp(v.Z, min.Z, max.Z) } +// ClampCompCompInPlace modifies vector by clamping each component between the respective min and max. func (v *V3F[T]) ClampCompCompInPlace(minX T, maxX T, minY T, maxY T, minZ T, maxZ T) { v.X = clamp(v.X, minX, maxX) v.Y = clamp(v.Y, minY, maxY) @@ -44,19 +52,23 @@ func (v *V3F[T]) ClampCompCompInPlace(minX T, maxX T, minY T, maxY T, minZ T, ma // V2I +// ClampComp returns a new V2I[T] with each component clamped between min and max. func (v V2I[T]) ClampComp(min V2I[T], max V2I[T]) V2I[T] { return V2I[T]{X: clamp(v.X, min.X, max.X), Y: clamp(v.Y, min.Y, max.Y)} } +// ClampCompComp returns a new V2I[T] with each component clamped between the respective min and max. func (v V2I[T]) ClampCompComp(minX T, maxX T, minY T, maxY T) V2I[T] { return V2I[T]{X: clamp(v.X, minX, maxX), Y: clamp(v.Y, minY, maxY)} } +// ClampCompInPlace modifies vector by clamping each component between min and max. func (v *V2I[T]) ClampCompInPlace(min V2I[T], max V2I[T]) { v.X = clamp(v.X, min.X, max.X) v.Y = clamp(v.Y, min.Y, max.Y) } +// ClampCompCompInPlace modifies vector by clamping each component between the respective min and max. func (v *V2I[T]) ClampCompCompInPlace(minX T, maxX T, minY T, maxY T) { v.X = clamp(v.X, minX, maxX) v.Y = clamp(v.Y, minY, maxY) @@ -64,20 +76,24 @@ func (v *V2I[T]) ClampCompCompInPlace(minX T, maxX T, minY T, maxY T) { // V3I +// ClampComp returns a new V3I[T] with each component clamped between min and max. func (v V3I[T]) ClampComp(min V3I[T], max V3I[T]) V3I[T] { return V3I[T]{X: clamp(v.X, min.X, max.X), Y: clamp(v.Y, min.Y, max.Y), Z: clamp(v.Z, min.Z, max.Z)} } +// ClampCompComp returns a new V3I[T] with each component clamped between the respective min and max. func (v V3I[T]) ClampCompComp(minX T, maxX T, minY T, maxY T, minZ T, maxZ T) V3I[T] { return V3I[T]{X: clamp(v.X, minX, maxX), Y: clamp(v.Y, minY, maxY), Z: clamp(v.Z, minZ, maxZ)} } +// ClampCompInPlace modifies vector by clamping each component between min and max. func (v *V3I[T]) ClampCompInPlace(min V3I[T], max V3I[T]) { v.X = clamp(v.X, min.X, max.X) v.Y = clamp(v.Y, min.Y, max.Y) v.Z = clamp(v.Z, min.Z, max.Z) } +// ClampCompCompInPlace modifies vector by clamping each component between the respective min and max. func (v *V3I[T]) ClampCompCompInPlace(minX T, maxX T, minY T, maxY T, minZ T, maxZ T) { v.X = clamp(v.X, minX, maxX) v.Y = clamp(v.Y, minY, maxY) diff --git a/clampLen.go b/clampLen.go index 498aa7a..4df71bc 100644 --- a/clampLen.go +++ b/clampLen.go @@ -2,6 +2,8 @@ package govec // V2F +// ClampLen returns a new V2F[T] with the length of vector clamped to min and max. +// When the length of the vector is outside the range min and max, the vector is scaled to fit. func (v V2F[T]) ClampLen(min float64, max float64) V2F[T] { length := v.Len() if length == 0 { @@ -17,6 +19,8 @@ func (v V2F[T]) ClampLen(min float64, max float64) V2F[T] { return v } +// ClampLenInPlace modifies vector by clamping the length of the vector to min and max. +// When the length of the vector is outside the range min and max, the vector is scaled to fit. func (v *V2F[T]) ClampLenInPlace(min float64, max float64) { length := v.Len() if length == 0 { @@ -36,6 +40,8 @@ func (v *V2F[T]) ClampLenInPlace(min float64, max float64) { // V3F +// ClampLen returns a new V3F[T] with the length of vector clamped to min and max. +// When the length of the vector is outside the range min and max, the vector is scaled to fit. func (v V3F[T]) ClampLen(min float64, max float64) V3F[T] { length := v.Len() if length == 0 { @@ -51,6 +57,8 @@ func (v V3F[T]) ClampLen(min float64, max float64) V3F[T] { return v } +// ClampLenInPlace modifies vector by clamping the length of the vector to min and max. +// When the length of the vector is outside the range min and max, the vector is scaled to fit. func (v *V3F[T]) ClampLenInPlace(min float64, max float64) { length := v.Len() if length == 0 { @@ -73,6 +81,9 @@ func (v *V3F[T]) ClampLenInPlace(min float64, max float64) { // V2I +// ClampLen returns a new V2F[T] with the length of vector clamped to min and max. +// This converts the vector to a V2F[float64] as the integer vector cannot be scaled. +// When the length of the vector is outside the range min and max, the vector is scaled to fit. func (v V2I[T]) ClampLen(min float64, max float64) V2F[float64] { length := v.Len() if length == 0 { @@ -90,6 +101,9 @@ func (v V2I[T]) ClampLen(min float64, max float64) V2F[float64] { // V3I +// ClampLen returns a new V3F[T] with the length of vector clamped to min and max. +// This converts the vector to a V3F[float64] as the integer vector cannot be scaled. +// When the length of the vector is outside the range min and max, the vector is scaled to fit. func (v V3I[T]) ClampLen(min float64, max float64) V3F[float64] { length := v.Len() if length == 0 { diff --git a/cross.go b/cross.go index db49191..7f8c38c 100644 --- a/cross.go +++ b/cross.go @@ -2,6 +2,7 @@ package govec // V3F +// Cross returns a new V3F[T] with the cross product of given vectors. func (v V3F[T]) Cross(v2 V3F[T]) V3F[T] { return V3F[T]{ X: v.Y*v2.Z - v.Z*v2.Y, @@ -10,6 +11,7 @@ func (v V3F[T]) Cross(v2 V3F[T]) V3F[T] { } } +// CrossInPlace modifies vector by setting it to the cross product of given vectors. func (v *V3F[T]) CrossInPlace(v2 V3F[T]) { x := v.Y*v2.Z - v.Z*v2.Y y := v.Z*v2.X - v.X*v2.Z @@ -17,6 +19,7 @@ func (v *V3F[T]) CrossInPlace(v2 V3F[T]) { v.X, v.Y, v.Z = x, y, z } +// CrossComp returns a new V3F[T] with the cross product of given vectors. func (v V3F[T]) CrossComp(x T, y T, z T) V3F[T] { return V3F[T]{ X: v.Y*z - v.Z*y, @@ -25,6 +28,7 @@ func (v V3F[T]) CrossComp(x T, y T, z T) V3F[T] { } } +// CrossCompInPlace modifies vector by setting it to the cross product of given vectors. func (v *V3F[T]) CrossCompInPlace(x T, y T, z T) { nx := v.Y*z - v.Z*y ny := v.Z*x - v.X*z @@ -34,6 +38,7 @@ func (v *V3F[T]) CrossCompInPlace(x T, y T, z T) { // V3I +// Cross returns a new V3I[T] with the cross product of given vectors. func (v V3I[T]) Cross(v2 V3I[T]) V3I[T] { return V3I[T]{ X: v.Y*v2.Z - v.Z*v2.Y, @@ -42,6 +47,7 @@ func (v V3I[T]) Cross(v2 V3I[T]) V3I[T] { } } +// CrossInPlace modifies vector by setting it to the cross product of given vectors. func (v *V3I[T]) CrossInPlace(v2 V3I[T]) { x := v.Y*v2.Z - v.Z*v2.Y y := v.Z*v2.X - v.X*v2.Z @@ -49,6 +55,7 @@ func (v *V3I[T]) CrossInPlace(v2 V3I[T]) { v.X, v.Y, v.Z = x, y, z } +// CrossComp returns a new V3I[T] with the cross product of given vectors. func (v V3I[T]) CrossComp(x T, y T, z T) V3I[T] { return V3I[T]{ X: v.Y*z - v.Z*y, @@ -57,6 +64,7 @@ func (v V3I[T]) CrossComp(x T, y T, z T) V3I[T] { } } +// CrossCompInPlace modifies vector by setting it to the cross product of given vectors. func (v *V3I[T]) CrossCompInPlace(x T, y T, z T) { nx := v.Y*z - v.Z*y ny := v.Z*x - v.X*z diff --git a/discard.go b/discard.go index bfd4391..cee8003 100644 --- a/discard.go +++ b/discard.go @@ -1,25 +1,31 @@ package govec -func (v V3F[T]) discardX() V2F[T] { +// DiscardX returns a new V2F[T] with the X component omitted. +func (v V3F[T]) DiscardX() V2F[T] { return V2F[T]{X: v.Y, Y: v.Z} } -func (v V3F[T]) discardY() V2F[T] { +// DiscardY returns a new V2F[T] with the Y component omitted. +func (v V3F[T]) DiscardY() V2F[T] { return V2F[T]{X: v.X, Y: v.Z} } -func (v V3F[T]) discardZ() V2F[T] { +// DiscardZ returns a new V2F[T] with the Z component omitted. +func (v V3F[T]) DiscardZ() V2F[T] { return V2F[T]{X: v.X, Y: v.Y} } -func (v V3I[T]) discardX() V2I[T] { +// DiscardX returns a new V2I[T] with the X component omitted. +func (v V3I[T]) DiscardX() V2I[T] { return V2I[T]{X: v.Y, Y: v.Z} } -func (v V3I[T]) discardY() V2I[T] { +// DiscardY returns a new V2I[T] with the Y component omitted. +func (v V3I[T]) DiscardY() V2I[T] { return V2I[T]{X: v.X, Y: v.Z} } -func (v V3I[T]) discardZ() V2I[T] { +// DiscardZ returns a new V2I[T] with the Z component omitted. +func (v V3I[T]) DiscardZ() V2I[T] { return V2I[T]{X: v.X, Y: v.Y} } diff --git a/div.go b/div.go index 37d847b..85829b4 100644 --- a/div.go +++ b/div.go @@ -2,19 +2,23 @@ package govec // V2F +// Div returns a new V2F[T] with the quotient of given vectors. func (v V2F[T]) Div(v2 V2F[T]) V2F[T] { return V2F[T]{X: v.X / v2.X, Y: v.Y / v2.Y} } +// DivInPlace modifies vector by setting it to the quotient of given vectors. func (v *V2F[T]) DivInPlace(v2 V2F[T]) { v.X /= v2.X v.Y /= v2.Y } +// DivComp returns a new V2F[T] with the quotient of given vectors. func (v V2F[T]) DivComp(x T, y T) V2F[T] { return V2F[T]{X: v.X / x, Y: v.Y / y} } +// DivCompInPlace modifies vector by setting it to the quotient of given vectors. func (v *V2F[T]) DivCompInPlace(x T, y T) { v.X /= x v.Y /= y @@ -22,20 +26,24 @@ func (v *V2F[T]) DivCompInPlace(x T, y T) { // V3F +// Div returns a new V3F[T] with the quotient of given vectors. func (v V3F[T]) Div(v2 V3F[T]) V3F[T] { return V3F[T]{X: v.X / v2.X, Y: v.Y / v2.Y, Z: v.Z / v2.Z} } +// DivInPlace modifies vector by setting it to the quotient of given vectors. func (v *V3F[T]) DivInPlace(v2 V3F[T]) { v.X /= v2.X v.Y /= v2.Y v.Z /= v2.Z } +// DivComp returns a new V3F[T] with the quotient of given vectors. func (v V3F[T]) DivComp(x T, y T, z T) V3F[T] { return V3F[T]{X: v.X / x, Y: v.Y / y, Z: v.Z / z} } +// DivCompInPlace modifies vector by setting it to the quotient of given vectors. func (v *V3F[T]) DivCompInPlace(x T, y T, z T) { v.X /= x v.Y /= y @@ -44,19 +52,23 @@ func (v *V3F[T]) DivCompInPlace(x T, y T, z T) { // V2I +// Div returns a new V2I[T] with the integer quotient of given vectors. func (v V2I[T]) Div(v2 V2I[T]) V2I[T] { return V2I[T]{X: v.X / v2.X, Y: v.Y / v2.Y} } +// DivInPlace modifies vector by setting it to the integer quotient of given vectors. func (v *V2I[T]) DivInPlace(v2 V2I[T]) { v.X /= v2.X v.Y /= v2.Y } +// DivComp returns a new V2I[T] with the integer quotient of given vectors. func (v V2I[T]) DivComp(x T, y T) V2I[T] { return V2I[T]{X: v.X / x, Y: v.Y / y} } +// DivCompInPlace modifies vector by setting it to the integer quotient of given vectors. func (v *V2I[T]) DivCompInPlace(x T, y T) { v.X /= x v.Y /= y @@ -64,20 +76,24 @@ func (v *V2I[T]) DivCompInPlace(x T, y T) { // V3I +// Div returns a new V3I[T] with the integer quotient of given vectors. func (v V3I[T]) Div(v2 V3I[T]) V3I[T] { return V3I[T]{X: v.X / v2.X, Y: v.Y / v2.Y, Z: v.Z / v2.Z} } +// DivInPlace modifies vector by setting it to the integer quotient of given vectors. func (v *V3I[T]) DivInPlace(v2 V3I[T]) { v.X /= v2.X v.Y /= v2.Y v.Z /= v2.Z } +// DivComp returns a new V3I[T] with the integer quotient of given vectors. func (v V3I[T]) DivComp(x T, y T, z T) V3I[T] { return V3I[T]{X: v.X / x, Y: v.Y / y, Z: v.Z / z} } +// DivCompInPlace modifies vector by setting it to the integer quotient of given vectors. func (v *V3I[T]) DivCompInPlace(x T, y T, z T) { v.X /= x v.Y /= y diff --git a/divScalar.go b/divScalar.go index 41c2f3d..037fb19 100644 --- a/divScalar.go +++ b/divScalar.go @@ -2,10 +2,12 @@ package govec // V2F +// DivScalar returns a new V2F[T] with components divided by scalar. func (v V2F[T]) DivScalar(scalar T) V2F[T] { return V2F[T]{X: v.X / scalar, Y: v.Y / scalar} } +// DivScalarInPlace modifies vector by setting components to their quotient with scalar. func (v *V2F[T]) DivScalarInPlace(scalar T) { v.X /= scalar v.Y /= scalar @@ -13,10 +15,12 @@ func (v *V2F[T]) DivScalarInPlace(scalar T) { // V3F +// DivScalar returns a new V3F[T] with components divided by scalar. func (v V3F[T]) DivScalar(scalar T) V3F[T] { return V3F[T]{X: v.X / scalar, Y: v.Y / scalar, Z: v.Z / scalar} } +// DivScalarInPlace modifies vector by setting components to their quotient with scalar. func (v *V3F[T]) DivScalarInPlace(scalar T) { v.X /= scalar v.Y /= scalar @@ -25,10 +29,12 @@ func (v *V3F[T]) DivScalarInPlace(scalar T) { // V2I +// DivScalar returns a new V2I[T] with components divided by scalar. func (v V2I[T]) DivScalar(scalar T) V2I[T] { return V2I[T]{X: v.X / scalar, Y: v.Y / scalar} } +// DivScalarInPlace modifies vector by setting components to their quotient with scalar. func (v *V2I[T]) DivScalarInPlace(scalar T) { v.X /= scalar v.Y /= scalar @@ -36,10 +42,12 @@ func (v *V2I[T]) DivScalarInPlace(scalar T) { // V3I +// DivScalar returns a new V3I[T] with components divided by scalar. func (v V3I[T]) DivScalar(scalar T) V3I[T] { return V3I[T]{X: v.X / scalar, Y: v.Y / scalar, Z: v.Z / scalar} } +// DivScalarInPlace modifies vector by setting components to their quotient with scalar. func (v *V3I[T]) DivScalarInPlace(scalar T) { v.X /= scalar v.Y /= scalar diff --git a/dot.go b/dot.go index 84810f3..9fb2055 100644 --- a/dot.go +++ b/dot.go @@ -2,40 +2,48 @@ package govec // V2F +// Dot returns the dot product of given vectors. func (v V2F[T]) Dot(v2 V2F[T]) T { return v.X*v2.X + v.Y*v2.Y } +// DotComp returns the dot product of given vectors. func (v V2F[T]) DotComp(x T, y T) T { return v.X*x + v.Y*y } // V3F +// Dot returns the dot product of given vectors. func (v V3F[T]) Dot(v2 V3F[T]) T { return v.X*v2.X + v.Y*v2.Y + v.Z*v2.Z } +// DotComp returns the dot product of given vectors. func (v V3F[T]) DotComp(x T, y T, z T) T { return v.X*x + v.Y*y + v.Z*z } // V2I +// Dot returns the dot product of given vectors. func (v V2I[T]) Dot(v2 V2I[T]) T { return v.X*v2.X + v.Y*v2.Y } +// DotComp returns the dot product of given vectors. func (v V2I[T]) DotComp(x T, y T) T { return v.X*x + v.Y*y } // V3I +// Dot returns the dot product of given vectors. func (v V3I[T]) Dot(v2 V3I[T]) T { return v.X*v2.X + v.Y*v2.Y + v.Z*v2.Z } +// DotComp returns the dot product of given vectors. func (v V3I[T]) DotComp(x T, y T, z T) T { return v.X*x + v.Y*y + v.Z*z } diff --git a/extend.go b/extend.go index 1bfcddd..d1b4ded 100644 --- a/extend.go +++ b/extend.go @@ -1,9 +1,11 @@ package govec -func (v V2F[T]) extend(z T) V3F[T] { +// Extend returns a new vector with the given component appended as Z. +func (v V2F[T]) Extend(z T) V3F[T] { return V3F[T]{X: v.X, Y: v.Y, Z: z} } -func (v V2I[T]) extend(z T) V3I[T] { +// Extend returns a new vector with the given component appended as Z. +func (v V2I[T]) Extend(z T) V3I[T] { return V3I[T]{X: v.X, Y: v.Y, Z: z} } diff --git a/floor.go b/floor.go index 542a78c..fae72fa 100644 --- a/floor.go +++ b/floor.go @@ -4,10 +4,12 @@ import "math" // V2F +// Floor returns a new V2F[T] with components rounded down to the nearest integer. func (v V2F[T]) Floor() V2F[T] { return V2F[T]{X: T(math.Floor(float64(v.X))), Y: T(math.Floor(float64(v.Y)))} } +// FloorInPlace modifies vector by setting components to their rounded down value. func (v *V2F[T]) FloorInPlace() { v.X = T(math.Floor(float64(v.X))) v.Y = T(math.Floor(float64(v.Y))) @@ -15,10 +17,12 @@ func (v *V2F[T]) FloorInPlace() { // V3F +// Floor returns a new V3F[T] with components rounded down to the nearest integer. func (v V3F[T]) Floor() V3F[T] { return V3F[T]{X: T(math.Floor(float64(v.X))), Y: T(math.Floor(float64(v.Y))), Z: T(math.Floor(float64(v.Z)))} } +// FloorInPlace modifies vector by setting components to their rounded down value. func (v *V3F[T]) FloorInPlace() { v.X = T(math.Floor(float64(v.X))) v.Y = T(math.Floor(float64(v.Y))) diff --git a/insert.go b/insert.go index 000d8d7..2e03e9d 100644 --- a/insert.go +++ b/insert.go @@ -1,25 +1,37 @@ package govec -func (v V2F[T]) insertX(a T) V3F[T] { +// InsertX returns a new vector with the given component inserted as X. +// Existing X and Y components are shifted to Y and Z. +func (v V2F[T]) InsertX(a T) V3F[T] { return V3F[T]{X: a, Y: v.X, Z: v.Y} } -func (v V2F[T]) insertY(a T) V3F[T] { +// InsertY returns a new vector with the given component inserted as Y. +// Existing Y component is shifted to Z. +func (v V2F[T]) InsertY(a T) V3F[T] { return V3F[T]{X: v.X, Y: a, Z: v.Y} } -func (v V2F[T]) insertZ(a T) V3F[T] { +// InsertZ returns a new vector with the given component inserted as Z. +// Equivalent to Extend. +func (v V2F[T]) InsertZ(a T) V3F[T] { return V3F[T]{X: v.X, Y: v.Y, Z: a} } -func (v V2I[T]) insertX(a T) V3I[T] { +// InsertX returns a new vector with the given component inserted as X. +// Existing X and Y components are shifted to Y and Z. +func (v V2I[T]) InsertX(a T) V3I[T] { return V3I[T]{X: a, Y: v.X, Z: v.Y} } -func (v V2I[T]) insertY(a T) V3I[T] { +// InsertY returns a new vector with the given component inserted as Y. +// Existing Y component is shifted to Z. +func (v V2I[T]) InsertY(a T) V3I[T] { return V3I[T]{X: v.X, Y: a, Z: v.Y} } -func (v V2I[T]) insertZ(a T) V3I[T] { +// InsertZ returns a new vector with the given component inserted as Z. +// Equivalent to Extend. +func (v V2I[T]) InsertZ(a T) V3I[T] { return V3I[T]{X: v.X, Y: v.Y, Z: a} } diff --git a/inv.go b/inv.go index 9ebacea..cf958a6 100644 --- a/inv.go +++ b/inv.go @@ -2,10 +2,12 @@ package govec // V2F +// Inv returns a new V2F[T] with components inverted (1/x). func (v V2F[T]) Inv() V2F[T] { return V2F[T]{X: 1 / v.X, Y: 1 / v.Y} } +// InvInPlace modifies vector by setting components to their inverse (1/x). func (v *V2F[T]) InvInPlace() { v.X = 1 / v.X v.Y = 1 / v.Y @@ -13,10 +15,12 @@ func (v *V2F[T]) InvInPlace() { // V3F +// Inv returns a new V3F[T] with components inverted (1/x). func (v V3F[T]) Inv() V3F[T] { return V3F[T]{X: 1 / v.X, Y: 1 / v.Y, Z: 1 / v.Z} } +// InvInPlace modifies vector by setting components to their inverse (1/x). func (v *V3F[T]) InvInPlace() { v.X = 1 / v.X v.Y = 1 / v.Y @@ -25,10 +29,12 @@ func (v *V3F[T]) InvInPlace() { // V2I +// Inv returns a new V2I[T] with components inverted (1/x). func (v V2I[T]) Inv() V2I[T] { return V2I[T]{X: 1 / v.X, Y: 1 / v.Y} } +// InvInPlace modifies vector by setting components to their inverse (1/x). func (v *V2I[T]) InvInPlace() { v.X = 1 / v.X v.Y = 1 / v.Y @@ -36,10 +42,12 @@ func (v *V2I[T]) InvInPlace() { // V3I +// Inv returns a new V3I[T] with components inverted (1/x). func (v V3I[T]) Inv() V3I[T] { return V3I[T]{X: 1 / v.X, Y: 1 / v.Y, Z: 1 / v.Z} } +// InvInPlace modifies vector by setting components to their inverse (1/x). func (v *V3I[T]) InvInPlace() { v.X = 1 / v.X v.Y = 1 / v.Y diff --git a/isZero.go b/isZero.go index 72ea140..aa688e8 100644 --- a/isZero.go +++ b/isZero.go @@ -2,32 +2,28 @@ package govec // V2F -// IsZero returns whether the vector is -// a zero vector or not. +// IsZero returns whether the vector is a zero vector or not. func (v V2F[T]) IsZero() bool { return v.X == 0 && v.Y == 0 } // V3F -// IsZero returns whether the vector is -// a zero vector or not. +// IsZero returns whether the vector is a zero vector or not. func (v V3F[T]) IsZero() bool { return v.X == 0 && v.Y == 0 && v.Z == 0 } // V2I -// IsZero returns whether the vector is -// a zero vector or not. +// IsZero returns whether the vector is a zero vector or not. func (v V2I[T]) IsZero() bool { return v.X == 0 && v.Y == 0 } // V3I -// IsZero returns whether the vector is -// a zero vector or not. +// IsZero returns whether the vector is a zero vector or not. func (v V3I[T]) IsZero() bool { return v.X == 0 && v.Y == 0 && v.Z == 0 } diff --git a/len.go b/len.go index 3e64771..d47af73 100644 --- a/len.go +++ b/len.go @@ -4,24 +4,28 @@ import "math" // V2F +// Len calculates the length of the vector. func (v V2F[T]) Len() float64 { return math.Sqrt(float64(v.X*v.X + v.Y*v.Y)) } // V3F +// Len calculates the length of the vector. func (v V3F[T]) Len() float64 { return math.Sqrt(float64(v.X*v.X + v.Y*v.Y + v.Z*v.Z)) } // V2I +// Len calculates the length of the vector. func (v V2I[T]) Len() float64 { return math.Sqrt(float64(v.X*v.X + v.Y*v.Y)) } // V3I +// Len calculates the length of the vector. func (v V3I[T]) Len() float64 { return math.Sqrt(float64(v.X*v.X + v.Y*v.Y + v.Z*v.Z)) } diff --git a/lenSqrt.go b/lenSqrt.go index 44b2ec8..3eeb6e1 100644 --- a/lenSqrt.go +++ b/lenSqrt.go @@ -1,17 +1,25 @@ package govec +// LenSqrt calculates the length of the vector without the square root. +// This is useful for comparing lengths without the overhead of a square root. func (v V2F[T]) LenSqrt() T { return v.X*v.X + v.Y*v.Y } +// LenSqrt calculates the length of the vector without the square root. +// This is useful for comparing lengths without the overhead of a square root. func (v V3F[T]) LenSqrt() T { return v.X*v.X + v.Y*v.Y + v.Z*v.Z } +// LenSqrt calculates the length of the vector without the square root. +// This is useful for comparing lengths without the overhead of a square root. func (v V2I[T]) LenSqrt() T { return v.X*v.X + v.Y*v.Y } +// LenSqrt calculates the length of the vector without the square root. +// This is useful for comparing lengths without the overhead of a square root. func (v V3I[T]) LenSqrt() T { return v.X*v.X + v.Y*v.Y + v.Z*v.Z } diff --git a/mod.go b/mod.go index 3de45c4..02d0179 100644 --- a/mod.go +++ b/mod.go @@ -2,19 +2,23 @@ package govec // V2F +// Mod returns a new V2F[T] with modulus applied to each component. func (v V2F[T]) Mod(v2 V2F[T]) V2F[T] { return V2F[T]{X: v.X - v2.X*T(int64(v.X/v2.X)), Y: v.Y - v2.Y*T(int64(v.Y/v2.Y))} } +// ModInPlace updates the vector in-place with modulus applied to each component. func (v *V2F[T]) ModInPlace(v2 V2F[T]) { v.X = v.X - v2.X*T(int64(v.X/v2.X)) v.Y = v.Y - v2.Y*T(int64(v.Y/v2.Y)) } +// ModComp returns a new V2F[T] with modulus applied to each component. func (v V2F[T]) ModComp(x T, y T) V2F[T] { return V2F[T]{X: v.X - x*T(int64(v.X/x)), Y: v.Y - y*T(int64(v.Y/y))} } +// ModCompInPlace updates the vector in-place with modulus applied to each component. func (v *V2F[T]) ModCompInPlace(x T, y T) { v.X = v.X - x*T(int64(v.X/x)) v.Y = v.Y - y*T(int64(v.Y/y)) @@ -22,20 +26,24 @@ func (v *V2F[T]) ModCompInPlace(x T, y T) { // V3F +// Mod returns a new V3F[T] with modulus applied to each component. func (v V3F[T]) Mod(v2 V3F[T]) V3F[T] { return V3F[T]{X: v.X - v2.X*T(int64(v.X/v2.X)), Y: v.Y - v2.Y*T(int64(v.Y/v2.Y)), Z: v.Z - v2.Z*T(int64(v.Z/v2.Z))} } +// ModInPlace updates the vector in-place with modulus applied to each component. func (v *V3F[T]) ModInPlace(v2 V3F[T]) { v.X = v.X - v2.X*T(int64(v.X/v2.X)) v.Y = v.Y - v2.Y*T(int64(v.Y/v2.Y)) v.Z = v.Z - v2.Z*T(int64(v.Z/v2.Z)) } +// ModComp returns a new V3F[T] with modulus applied to each component. func (v V3F[T]) ModComp(x T, y T, z T) V3F[T] { return V3F[T]{X: v.X - x*T(int64(v.X/x)), Y: v.Y - y*T(int64(v.Y/y)), Z: v.Z - z*T(int64(v.Z/z))} } +// ModCompInPlace updates the vector in-place with modulus applied to each component. func (v *V3F[T]) ModCompInPlace(x T, y T, z T) { v.X = v.X - x*T(int64(v.X/x)) v.Y = v.Y - y*T(int64(v.Y/y)) @@ -44,19 +52,23 @@ func (v *V3F[T]) ModCompInPlace(x T, y T, z T) { // V2I +// Mod returns a new V2I[T] with modulus applied to each component. func (v V2I[T]) Mod(v2 V2I[T]) V2I[T] { return V2I[T]{X: v.X % v2.X, Y: v.Y % v2.Y} } +// ModInPlace updates the vector in-place with modulus applied to each component. func (v *V2I[T]) ModInPlace(v2 V2I[T]) { v.X %= v2.X v.Y %= v2.Y } +// ModComp returns a new V2I[T] with modulus applied to each component. func (v V2I[T]) ModComp(x T, y T) V2I[T] { return V2I[T]{X: v.X % x, Y: v.Y % y} } +// ModCompInPlace updates the vector in-place with modulus applied to each component. func (v *V2I[T]) ModCompInPlace(x T, y T) { v.X %= x v.Y %= y @@ -64,20 +76,24 @@ func (v *V2I[T]) ModCompInPlace(x T, y T) { // V3I +// Mod returns a new V3I[T] with modulus applied to each component. func (v V3I[T]) Mod(v2 V3I[T]) V3I[T] { return V3I[T]{X: v.X % v2.X, Y: v.Y % v2.Y, Z: v.Z % v2.Z} } +// ModInPlace updates the vector in-place with modulus applied to each component. func (v *V3I[T]) ModInPlace(v2 V3I[T]) { v.X %= v2.X v.Y %= v2.Y v.Z %= v2.Z } +// ModComp returns a new V3I[T] with modulus applied to each component. func (v V3I[T]) ModComp(x T, y T, z T) V3I[T] { return V3I[T]{X: v.X % x, Y: v.Y % y, Z: v.Z % z} } +// ModCompInPlace updates the vector in-place with modulus applied to each component. func (v *V3I[T]) ModCompInPlace(x T, y T, z T) { v.X %= x v.Y %= y diff --git a/modScalar.go b/modScalar.go index 42406ec..7878cc5 100644 --- a/modScalar.go +++ b/modScalar.go @@ -2,10 +2,12 @@ package govec // V2F +// ModScalar returns a new V2F[T] with same modulus applied to each component. func (v V2F[T]) ModScalar(scalar T) V2F[T] { return V2F[T]{X: v.X - scalar*T(int64(v.X/scalar)), Y: v.Y - scalar*T(int64(v.Y/scalar))} } +// ModScalarInPlace updates the vector in-place with same modulus applied to each component. func (v *V2F[T]) ModScalarInPlace(scalar T) { v.X = v.X - scalar*T(int64(v.X/scalar)) v.Y = v.Y - scalar*T(int64(v.Y/scalar)) @@ -13,10 +15,12 @@ func (v *V2F[T]) ModScalarInPlace(scalar T) { // V3F +// ModScalar returns a new V3F[T] with same modulus applied to each component. func (v V3F[T]) ModScalar(scalar T) V3F[T] { return V3F[T]{X: v.X - scalar*T(int64(v.X/scalar)), Y: v.Y - scalar*T(int64(v.Y/scalar)), Z: v.Z - scalar*T(int64(v.Z/scalar))} } +// ModScalarInPlace updates the vector in-place with same modulus applied to each component. func (v *V3F[T]) ModScalarInPlace(scalar T) { v.X = v.X - scalar*T(int64(v.X/scalar)) v.Y = v.Y - scalar*T(int64(v.Y/scalar)) @@ -25,10 +29,12 @@ func (v *V3F[T]) ModScalarInPlace(scalar T) { // V2I +// ModScalar returns a new V2I[T] with same modulus applied to each component. func (v V2I[T]) ModScalar(scalar T) V2I[T] { return V2I[T]{X: v.X % scalar, Y: v.Y % scalar} } +// ModScalarInPlace updates the vector in-place with same modulus applied to each component. func (v *V2I[T]) ModScalarInPlace(scalar T) { v.X %= scalar v.Y %= scalar @@ -36,10 +42,12 @@ func (v *V2I[T]) ModScalarInPlace(scalar T) { // V3I +// ModScalar returns a new V3I[T] with same modulus applied to each component. func (v V3I[T]) ModScalar(scalar T) V3I[T] { return V3I[T]{X: v.X % scalar, Y: v.Y % scalar, Z: v.Z % scalar} } +// ModScalarInPlace updates the vector in-place with same modulus applied to each component. func (v *V3I[T]) ModScalarInPlace(scalar T) { v.X %= scalar v.Y %= scalar diff --git a/mul.go b/mul.go index 48fcdbc..c55a17c 100644 --- a/mul.go +++ b/mul.go @@ -2,19 +2,23 @@ package govec // V2F +// Mul returns the component-wise product of two vectors. func (v V2F[T]) Mul(v2 V2F[T]) V2F[T] { return V2F[T]{X: v.X * v2.X, Y: v.Y * v2.Y} } +// MulInPlace modifies the vector by multiplying it with another vector. func (v *V2F[T]) MulInPlace(v2 V2F[T]) { v.X *= v2.X v.Y *= v2.Y } +// MulComp returns the component-wise product of a vector and three scalars. func (v V2F[T]) MulComp(x T, y T) V2F[T] { return V2F[T]{X: v.X * x, Y: v.Y * y} } +// MulCompInPlace modifies the vector by multiplying it with three scalars. func (v *V2F[T]) MulCompInPlace(x T, y T) { v.X *= x v.Y *= y @@ -22,20 +26,24 @@ func (v *V2F[T]) MulCompInPlace(x T, y T) { // V3F +// Mul returns the component-wise product of two vectors. func (v V3F[T]) Mul(v2 V3F[T]) V3F[T] { return V3F[T]{X: v.X * v2.X, Y: v.Y * v2.Y, Z: v.Z * v2.Z} } +// MulInPlace modifies the vector by multiplying it with another vector. func (v *V3F[T]) MulInPlace(v2 V3F[T]) { v.X *= v2.X v.Y *= v2.Y v.Z *= v2.Z } +// MulComp returns the component-wise product of a vector and three scalars. func (v V3F[T]) MulComp(x T, y T, z T) V3F[T] { return V3F[T]{X: v.X * x, Y: v.Y * y, Z: v.Z * z} } +// MulCompInPlace modifies the vector by multiplying it with three scalars. func (v *V3F[T]) MulCompInPlace(x T, y T, z T) { v.X *= x v.Y *= y @@ -44,19 +52,23 @@ func (v *V3F[T]) MulCompInPlace(x T, y T, z T) { // V2I +// Mul returns the component-wise product of two vectors. func (v V2I[T]) Mul(v2 V2I[T]) V2I[T] { return V2I[T]{X: v.X * v2.X, Y: v.Y * v2.Y} } +// MulInPlace modifies the vector by multiplying it with another vector. func (v *V2I[T]) MulInPlace(v2 V2I[T]) { v.X *= v2.X v.Y *= v2.Y } +// MulComp returns the component-wise product of a vector and three scalars. func (v V2I[T]) MulComp(x T, y T) V2I[T] { return V2I[T]{X: v.X * x, Y: v.Y * y} } +// MulCompInPlace modifies the vector by multiplying it with three scalars. func (v *V2I[T]) MulCompInPlace(x T, y T) { v.X *= x v.Y *= y @@ -64,20 +76,24 @@ func (v *V2I[T]) MulCompInPlace(x T, y T) { // V3I +// Mul returns the component-wise product of two vectors. func (v V3I[T]) Mul(v2 V3I[T]) V3I[T] { return V3I[T]{X: v.X * v2.X, Y: v.Y * v2.Y, Z: v.Z * v2.Z} } +// MulInPlace modifies the vector by multiplying it with another vector. func (v *V3I[T]) MulInPlace(v2 V3I[T]) { v.X *= v2.X v.Y *= v2.Y v.Z *= v2.Z } +// MulComp returns the component-wise product of a vector and three scalars. func (v V3I[T]) MulComp(x T, y T, z T) V3I[T] { return V3I[T]{X: v.X * x, Y: v.Y * y, Z: v.Z * z} } +// MulCompInPlace modifies the vector by multiplying it with three scalars. func (v *V3I[T]) MulCompInPlace(x T, y T, z T) { v.X *= x v.Y *= y diff --git a/mulScalar.go b/mulScalar.go index 206895c..5ef91c7 100644 --- a/mulScalar.go +++ b/mulScalar.go @@ -2,10 +2,12 @@ package govec // V2F +// MulScalar returns the component-wise product of the vector and a scalar. func (v V2F[T]) MulScalar(scalar T) V2F[T] { return V2F[T]{X: v.X * scalar, Y: v.Y * scalar} } +// MulScalarInPlace modifies the vector by multiplying it with a scalar. func (v *V2F[T]) MulScalarInPlace(scalar T) { v.X *= scalar v.Y *= scalar @@ -13,10 +15,12 @@ func (v *V2F[T]) MulScalarInPlace(scalar T) { // V3F +// MulScalar returns the component-wise product of the vector and a scalar. func (v V3F[T]) MulScalar(scalar T) V3F[T] { return V3F[T]{X: v.X * scalar, Y: v.Y * scalar, Z: v.Z * scalar} } +// MulScalarInPlace modifies the vector by multiplying it with a scalar. func (v *V3F[T]) MulScalarInPlace(scalar T) { v.X *= scalar v.Y *= scalar @@ -25,10 +29,12 @@ func (v *V3F[T]) MulScalarInPlace(scalar T) { // V2I +// MulScalar returns the component-wise product of the vector and a scalar. func (v V2I[T]) MulScalar(scalar T) V2I[T] { return V2I[T]{X: v.X * scalar, Y: v.Y * scalar} } +// MulScalarInPlace modifies the vector by multiplying it with a scalar. func (v *V2I[T]) MulScalarInPlace(scalar T) { v.X *= scalar v.Y *= scalar @@ -36,10 +42,12 @@ func (v *V2I[T]) MulScalarInPlace(scalar T) { // V3I +// MulScalar returns the component-wise product of the vector and a scalar. func (v V3I[T]) MulScalar(scalar T) V3I[T] { return V3I[T]{X: v.X * scalar, Y: v.Y * scalar, Z: v.Z * scalar} } +// MulScalarInPlace modifies the vector by multiplying it with a scalar. func (v *V3I[T]) MulScalarInPlace(scalar T) { v.X *= scalar v.Y *= scalar diff --git a/neg.go b/neg.go index b4abf65..9d9ea92 100644 --- a/neg.go +++ b/neg.go @@ -1,41 +1,45 @@ package govec -// Neg for V2F +// Neg returns a new V2F[T] with the negated components. func (v V2F[T]) Neg(v2 V2F[T]) V2F[T] { return V2F[T]{X: v.X - v2.X, Y: v.Y - v2.Y} } +// NegInPlace modifies the vector by negating its components. func (v *V2F[T]) NegInPlace(v2 V2F[T]) { v.X -= v2.X v.Y -= v2.Y } -// Neg for V3F +// Neg returns a new V3F[T] with the negated components. func (v V3F[T]) Neg(v2 V3F[T]) V3F[T] { return V3F[T]{X: v.X - v2.X, Y: v.Y - v2.Y, Z: v.Z - v2.Z} } +// NegInPlace modifies the vector by negating its components. func (v *V3F[T]) NegInPlace(v2 V3F[T]) { v.X -= v2.X v.Y -= v2.Y v.Z -= v2.Z } -// Neg for V2I +// Neg returns a new V2I[T] with the negated components. func (v V2I[T]) Neg(v2 V2I[T]) V2I[T] { return V2I[T]{X: v.X - v2.X, Y: v.Y - v2.Y} } +// NegInPlace modifies the vector by negating its components. func (v *V2I[T]) NegInPlace(v2 V2I[T]) { v.X -= v2.X v.Y -= v2.Y } -// Neg for V3I +// Neg returns a new V3I[T] with the negated components. func (v V3I[T]) Neg(v2 V3I[T]) V3I[T] { return V3I[T]{X: v.X - v2.X, Y: v.Y - v2.Y, Z: v.Z - v2.Z} } +// NegInPlace modifies the vector by negating its components. func (v *V3I[T]) NegInPlace(v2 V3I[T]) { v.X -= v2.X v.Y -= v2.Y diff --git a/new.go b/new.go index 7511471..c201917 100644 --- a/new.go +++ b/new.go @@ -4,72 +4,88 @@ import "golang.org/x/exp/constraints" // NEW +// NewV2F returns a new V2F[T] with the given components. func NewV2F[T constraints.Float](x, y T) V2F[T] { return V2F[T]{X: x, Y: y} } +// NewV3F returns a new V3F[T] with the given components. func NewV3F[T constraints.Float](x, y, z T) V3F[T] { return V3F[T]{X: x, Y: y, Z: z} } +// NewV2I returns a new V2I[T] with the given components. func NewV2I[T constraints.Integer](x, y T) V2I[T] { return V2I[T]{X: x, Y: y} } +// NewV3I returns a new V3I[T] with the given components. func NewV3I[T constraints.Integer](x, y, z T) V3I[T] { return V3I[T]{X: x, Y: y, Z: z} } // Fill +// FillV2F returns a new V2F[T] with the components set to the provided value. func FillV2F[T constraints.Float](value T) V2F[T] { return V2F[T]{X: value, Y: value} } +// FillV3F returns a new V3F[T] with the components set to the provided value. func FillV3F[T constraints.Float](value T) V3F[T] { return V3F[T]{X: value, Y: value, Z: value} } +// FillV2I returns a new V2I[T] with the components set to the provided value. func FillV2I[T constraints.Integer](value T) V2I[T] { return V2I[T]{X: value, Y: value} } +// FillV3I returns a new V3I[T] with the components set to the provided value. func FillV3I[T constraints.Integer](value T) V3I[T] { return V3I[T]{X: value, Y: value, Z: value} } // ZERO +// ZeroV2F returns a new V2F[T] with the components set to zero. func ZeroV2F[T constraints.Float]() V2F[T] { return V2F[T]{X: 0.0, Y: 0.0} } +// ZeroV3F returns a new V3F[T] with the components set to zero. func ZeroV3F[T constraints.Float]() V3F[T] { return V3F[T]{X: 0.0, Y: 0.0, Z: 0.0} } +// ZeroV2I returns a new V2I[T] with the components set to zero. func ZeroV2I[T constraints.Integer]() V2I[T] { return V2I[T]{X: 0, Y: 0} } +// ZeroV3I returns a new V3I[T] with the components set to zero. func ZeroV3I[T constraints.Integer]() V3I[T] { return V3I[T]{X: 0, Y: 0, Z: 0} } // ONE +// OneV2F returns a new V2F[T] with the components set to one. func OneV2F[T constraints.Float]() V2F[T] { return V2F[T]{X: 1.0, Y: 1.0} } +// OneV3F returns a new V3F[T] with the components set to one. func OneV3F[T constraints.Float]() V3F[T] { return V3F[T]{X: 1.0, Y: 1.0, Z: 1.0} } +// OneV2I returns a new V2I[T] with the components set to one. func OneV2I[T constraints.Integer]() V2I[T] { return V2I[T]{X: 1, Y: 1} } +// OneV3I returns a new V3I[T] with the components set to one. func OneV3I[T constraints.Integer]() V3I[T] { return V3I[T]{X: 1, Y: 1, Z: 1} } diff --git a/norm.go b/norm.go index 477f2b4..a0335a5 100644 --- a/norm.go +++ b/norm.go @@ -6,11 +6,13 @@ import ( // V2F +// Norm returns a new vector with the same direction and magnitude 1. func (v V2F[T]) Norm() V2F[T] { magnitude := math.Sqrt(float64(v.X*v.X + v.Y*v.Y)) return V2F[T]{X: v.X / T(magnitude), Y: v.Y / T(magnitude)} } +// NormInPlace modifies the vector by normalizing it. func (v *V2F[T]) NormInPlace() { magnitude := math.Sqrt(float64(v.X*v.X + v.Y*v.Y)) v.X /= T(magnitude) @@ -19,11 +21,13 @@ func (v *V2F[T]) NormInPlace() { // V3F +// Norm returns a new vector with the same direction and magnitude 1. func (v V3F[T]) Norm() V3F[T] { magnitude := math.Sqrt(float64(v.X*v.X + v.Y*v.Y + v.Z*v.Z)) return V3F[T]{X: v.X / T(magnitude), Y: v.Y / T(magnitude), Z: v.Z / T(magnitude)} } +// NormInPlace modifies the vector by normalizing it. func (v *V3F[T]) NormInPlace() { magnitude := math.Sqrt(float64(v.X*v.X + v.Y*v.Y + v.Z*v.Z)) v.X /= T(magnitude) @@ -33,6 +37,7 @@ func (v *V3F[T]) NormInPlace() { // V2I +// Norm returns a new float vector with the same direction and magnitude 1. func (v V2I[T]) Norm() V2F[float64] { magnitude := math.Sqrt(float64(v.X*v.X + v.Y*v.Y)) return V2F[float64]{X: float64(v.X) / magnitude, Y: float64(v.Y) / magnitude} @@ -40,6 +45,7 @@ func (v V2I[T]) Norm() V2F[float64] { // V3I +// Norm returns a new float vector with the same direction and magnitude 1. func (v V3I[T]) Norm() V3F[float64] { magnitude := math.Sqrt(float64(v.X*v.X + v.Y*v.Y + v.Z*v.Z)) return V3F[float64]{X: float64(v.X) / magnitude, Y: float64(v.Y) / magnitude, Z: float64(v.Z) / magnitude} diff --git a/pow.go b/pow.go index 50c08ec..e465d55 100644 --- a/pow.go +++ b/pow.go @@ -4,19 +4,27 @@ import "math" // V2F +// Pow returns a new vector with each component raised to the power of the corresponding component in v2. +// Supports fractional exponents. func (v V2F[T]) Pow(v2 V2F[T]) V2F[T] { return V2F[T]{X: T(math.Pow(float64(v.X), float64(v2.X))), Y: T(math.Pow(float64(v.Y), float64(v2.Y)))} } +// PowInPlace modifies vector by setting value of each component to the power of the corresponding component in v2. +// Supports fractional exponents. func (v *V2F[T]) PowInPlace(v2 V2F[T]) { v.X = T(math.Pow(float64(v.X), float64(v2.X))) v.Y = T(math.Pow(float64(v.Y), float64(v2.Y))) } +// PowComp returns a new vector with each component raised to the power of the corresponding values. +// Supports fractional exponents. func (v V2F[T]) PowComp(x T, y T) V2F[T] { return V2F[T]{X: T(math.Pow(float64(v.X), float64(x))), Y: T(math.Pow(float64(v.Y), float64(y)))} } +// PowCompInPlace modifies vector by setting value of each component to the power of the corresponding values. +// Supports fractional exponents. func (v *V2F[T]) PowCompInPlace(x T, y T) { v.X = T(math.Pow(float64(v.X), float64(x))) v.Y = T(math.Pow(float64(v.Y), float64(y))) @@ -24,20 +32,28 @@ func (v *V2F[T]) PowCompInPlace(x T, y T) { // V3F +// Pow returns a new vector with each component raised to the power of the corresponding component in v2. +// Supports fractional exponents. func (v V3F[T]) Pow(v2 V3F[T]) V3F[T] { return V3F[T]{X: T(math.Pow(float64(v.X), float64(v2.X))), Y: T(math.Pow(float64(v.Y), float64(v2.Y))), Z: T(math.Pow(float64(v.Z), float64(v2.Z)))} } +// PowInPlace modifies vector by setting value of each component to the power of the corresponding component in v2. +// Supports fractional exponents. func (v *V3F[T]) PowInPlace(v2 V3F[T]) { v.X = T(math.Pow(float64(v.X), float64(v2.X))) v.Y = T(math.Pow(float64(v.Y), float64(v2.Y))) v.Z = T(math.Pow(float64(v.Z), float64(v2.Z))) } +// PowComp returns a new vector with each component raised to the power of the corresponding values. +// Supports fractional exponents. func (v V3F[T]) PowComp(x T, y T, z T) V3F[T] { return V3F[T]{X: T(math.Pow(float64(v.X), float64(x))), Y: T(math.Pow(float64(v.Y), float64(y))), Z: T(math.Pow(float64(v.Z), float64(z)))} } +// PowCompInPlace modifies vector by setting value of each component to the power of the corresponding values. +// Supports fractional exponents. func (v *V3F[T]) PowCompInPlace(x T, y T, z T) { v.X = T(math.Pow(float64(v.X), float64(x))) v.Y = T(math.Pow(float64(v.Y), float64(y))) @@ -46,19 +62,27 @@ func (v *V3F[T]) PowCompInPlace(x T, y T, z T) { // V2I +// Pow returns a new vector with each component raised to the power of the corresponding component in v2. +// Doesn't support fractional exponents. func (v V2I[T]) Pow(v2 V2I[T]) V2I[T] { return V2I[T]{X: pow(v.X, int16(v2.X)), Y: pow(v.Y, int16(v2.Y))} } +// PowInPlace modifies vector by setting value of each component to the power of the corresponding component in v2. +// Doesn't support fractional exponents. func (v *V2I[T]) PowInPlace(v2 V2I[T]) { v.X = pow(v.X, int16(v2.X)) v.Y = pow(v.Y, int16(v2.Y)) } +// PowComp returns a new vector with each component raised to the power of the corresponding values. +// Doesn't support fractional exponents. func (v V2I[T]) PowComp(x T, y T) V2I[T] { return V2I[T]{X: pow(v.X, int16(x)), Y: pow(v.Y, int16(y))} } +// PowCompInPlace modifies vector by setting value of each component to the power of the corresponding values. +// Doesn't support fractional exponents. func (v *V2I[T]) PowCompInPlace(x T, y T) { v.X = pow(v.X, int16(x)) v.Y = pow(v.Y, int16(y)) @@ -66,20 +90,28 @@ func (v *V2I[T]) PowCompInPlace(x T, y T) { // V3I +// Pow returns a new vector with each component raised to the power of the corresponding component in v2. +// Doesn't support fractional exponents. func (v V3I[T]) Pow(v2 V3I[T]) V3I[T] { return V3I[T]{X: pow(v.X, int16(v2.X)), Y: pow(v.Y, int16(v2.Y)), Z: pow(v.Z, int16(v2.Z))} } +// PowInPlace modifies vector by setting value of each component to the power of the corresponding component in v2. +// Doesn't support fractional exponents. func (v *V3I[T]) PowInPlace(v2 V3I[T]) { v.X = pow(v.X, int16(v2.X)) v.Y = pow(v.Y, int16(v2.Y)) v.Z = pow(v.Z, int16(v2.Z)) } +// PowComp returns a new vector with each component raised to the power of the corresponding values. +// Doesn't support fractional exponents. func (v V3I[T]) PowComp(x T, y T, z T) V3I[T] { return V3I[T]{X: pow(v.X, int16(x)), Y: pow(v.Y, int16(y)), Z: pow(v.Z, int16(z))} } +// PowCompInPlace modifies vector by setting value of each component to the power of the corresponding values. +// Doesn't support fractional exponents. func (v *V3I[T]) PowCompInPlace(x T, y T, z T) { v.X = pow(v.X, int16(x)) v.Y = pow(v.Y, int16(y)) diff --git a/pow2.go b/pow2.go index 2faef86..f4248af 100644 --- a/pow2.go +++ b/pow2.go @@ -1,41 +1,53 @@ package govec -// Pow2 for V2F +// Pow2 returns a new vector with each component raised to the power of 2. +// Doesn't support fractional exponents. func (v V2F[T]) Pow2() V2F[T] { return V2F[T]{X: v.X * v.X, Y: v.Y * v.Y} } +// Pow2InPlace modifies vector by setting value of each component to the power of 2. +// Doesn't support fractional exponents. func (v *V2F[T]) Pow2InPlace() { v.X = v.X * v.X v.Y = v.Y * v.Y } -// Pow2 for V3F +// Pow2 returns a new vector with each component raised to the power of 2. +// Doesn't support fractional exponents. func (v V3F[T]) Pow2() V3F[T] { return V3F[T]{X: v.X * v.X, Y: v.Y * v.Y, Z: v.Z * v.Z} } +// Pow2InPlace modifies vector by setting value of each component to the power of 2. +// Doesn't support fractional exponents. func (v *V3F[T]) Pow2InPlace() { v.X = v.X * v.X v.Y = v.Y * v.Y v.Z = v.Z * v.Z } -// Pow2 for V2I +// Pow2 returns a new vector with each component raised to the power of 2. +// Doesn't support fractional exponents. func (v V2I[T]) Pow2() V2I[T] { return V2I[T]{X: v.X * v.X, Y: v.Y * v.Y} } +// Pow2InPlace modifies vector by setting value of each component to the power of 2. +// Doesn't support fractional exponents. func (v *V2I[T]) Pow2InPlace() { v.X = v.X * v.X v.Y = v.Y * v.Y } -// Pow2 for V3I +// Pow2 returns a new vector with each component raised to the power of 2. +// Doesn't support fractional exponents. func (v V3I[T]) Pow2() V3I[T] { return V3I[T]{X: v.X * v.X, Y: v.Y * v.Y, Z: v.Z * v.Z} } +// Pow2InPlace modifies vector by setting value of each component to the power of 2. +// Doesn't support fractional exponents. func (v *V3I[T]) Pow2InPlace() { v.X = v.X * v.X v.Y = v.Y * v.Y diff --git a/pow3.go b/pow3.go index 2cc13b1..68f6aef 100644 --- a/pow3.go +++ b/pow3.go @@ -1,41 +1,53 @@ package govec -// Pow3 for V2F +// Pow3 returns a new vector with each component raised to the power of 3. +// Doesn't support fractional exponents. func (v V2F[T]) Pow3() V2F[T] { return V2F[T]{X: v.X * v.X * v.X, Y: v.Y * v.Y * v.Y} } +// Pow3InPlace modifies vector by setting value of each component to the power of 3. +// Doesn't support fractional exponents. func (v *V2F[T]) Pow3InPlace() { v.X = v.X * v.X * v.X v.Y = v.Y * v.Y * v.Y } -// Pow3 for V3F +// Pow3 returns a new vector with each component raised to the power of 3. +// Doesn't support fractional exponents. func (v V3F[T]) Pow3() V3F[T] { return V3F[T]{X: v.X * v.X * v.X, Y: v.Y * v.Y * v.Y, Z: v.Z * v.Z * v.Z} } +// Pow3InPlace modifies vector by setting value of each component to the power of 3. +// Doesn't support fractional exponents. func (v *V3F[T]) Pow3InPlace() { v.X = v.X * v.X * v.X v.Y = v.Y * v.Y * v.Y v.Z = v.Z * v.Z * v.Z } -// Pow3 for V2I +// Pow3 returns a new vector with each component raised to the power of 3. +// Doesn't support fractional exponents. func (v V2I[T]) Pow3() V2I[T] { return V2I[T]{X: v.X * v.X * v.X, Y: v.Y * v.Y * v.Y} } +// Pow3InPlace modifies vector by setting value of each component to the power of 3. +// Doesn't support fractional exponents. func (v *V2I[T]) Pow3InPlace() { v.X = v.X * v.X * v.X v.Y = v.Y * v.Y * v.Y } -// Pow3 for V3I +// Pow3 returns a new vector with each component raised to the power of 3. +// Doesn't support fractional exponents. func (v V3I[T]) Pow3() V3I[T] { return V3I[T]{X: v.X * v.X * v.X, Y: v.Y * v.Y * v.Y, Z: v.Z * v.Z * v.Z} } +// Pow3InPlace modifies vector by setting value of each component to the power of 3. +// Doesn't support fractional exponents. func (v *V3I[T]) Pow3InPlace() { v.X = v.X * v.X * v.X v.Y = v.Y * v.Y * v.Y diff --git a/powN.go b/powN.go index 5ef09a0..fd4bab1 100644 --- a/powN.go +++ b/powN.go @@ -2,10 +2,14 @@ package govec // V2F +// PowN returns a new vector with each component raised to the power of n. +// Doesn't support fractional exponents. func (v V2F[T]) PowN(n int16) V2F[T] { return V2F[T]{X: pow(v.X, n), Y: pow(v.Y, n)} } +// PowNInPlace modifies vector by setting value of each component to the power of n. +// Doesn't support fractional exponents. func (v *V2F[T]) PowNInPlace(n int16) { v.X = pow(v.X, n) v.Y = pow(v.Y, n) @@ -13,10 +17,14 @@ func (v *V2F[T]) PowNInPlace(n int16) { // V3F +// PowN returns a new vector with each component raised to the power of n. +// Doesn't support fractional exponents. func (v V3F[T]) PowN(n int16) V3F[T] { return V3F[T]{X: pow(v.X, n), Y: pow(v.Y, n), Z: pow(v.Z, n)} } +// PowNInPlace modifies vector by setting value of each component to the power of n. +// Doesn't support fractional exponents. func (v *V3F[T]) PowNInPlace(n int16) { v.X = pow(v.X, n) v.Y = pow(v.Y, n) @@ -25,10 +33,14 @@ func (v *V3F[T]) PowNInPlace(n int16) { // V2I +// PowN returns a new vector with each component raised to the power of n. +// Doesn't support fractional exponents. func (v V2I[T]) PowN(n int16) V2I[T] { return V2I[T]{X: pow(v.X, n), Y: pow(v.Y, n)} } +// PowNInPlace modifies vector by setting value of each component to the power of n. +// Doesn't support fractional exponents. func (v *V2I[T]) PowNInPlace(n int16) { v.X = pow(v.X, n) v.Y = pow(v.Y, n) @@ -36,10 +48,14 @@ func (v *V2I[T]) PowNInPlace(n int16) { // V3I +// PowN returns a new vector with each component raised to the power of n. +// Doesn't support fractional exponents. func (v V3I[T]) PowN(n int16) V3I[T] { return V3I[T]{X: pow(v.X, n), Y: pow(v.Y, n), Z: pow(v.Z, n)} } +// PowNInPlace modifies vector by setting value of each component to the power of n. +// Doesn't support fractional exponents. func (v *V3I[T]) PowNInPlace(n int16) { v.X = pow(v.X, n) v.Y = pow(v.Y, n) diff --git a/powNFloat.go b/powNFloat.go index 220ebca..e03f844 100644 --- a/powNFloat.go +++ b/powNFloat.go @@ -2,42 +2,52 @@ package govec import "math" -// PowNFloat for V2F +// PowNFloat returns a new vector with each component raised to the power of n. +// Supports fractional exponents. func (v V2F[T]) PowNFloat(n float64) V2F[T] { return V2F[T]{X: T(math.Pow(float64(v.X), n)), Y: T(math.Pow(float64(v.Y), n))} } +// PowNFloatInPlace modifies vector by setting value of each component to the power of n. +// Supports fractional exponents. func (v *V2F[T]) PowNFloatInPlace(n float64) { v.X = T(math.Pow(float64(v.X), n)) v.Y = T(math.Pow(float64(v.Y), n)) } -// PowNFloat for V3F +// PowNFloat returns a new vector with each component raised to the power of n. +// Supports fractional exponents. func (v V3F[T]) PowNFloat(n float64) V3F[T] { return V3F[T]{X: T(math.Pow(float64(v.X), n)), Y: T(math.Pow(float64(v.Y), n)), Z: T(math.Pow(float64(v.Z), n))} } +// PowNFloatInPlace modifies vector by setting value of each component to the power of n. +// Supports fractional exponents. func (v *V3F[T]) PowNFloatInPlace(n float64) { v.X = T(math.Pow(float64(v.X), n)) v.Y = T(math.Pow(float64(v.Y), n)) v.Z = T(math.Pow(float64(v.Z), n)) } -// PowNFloat for V2I +// PowNFloat returns a new vector with each component raised to the power of n. +// Supports fractional exponents. func (v V2I[T]) PowNFloat(n float64) V2I[T] { return V2I[T]{X: T(math.Pow(float64(v.X), n)), Y: T(math.Pow(float64(v.Y), n))} } +// PowNFloatInPlace modifies vector by setting value of each component to the power of n. +// Supports fractional exponents. func (v *V2I[T]) PowNFloatInPlace(n float64) { v.X = T(math.Pow(float64(v.X), n)) v.Y = T(math.Pow(float64(v.Y), n)) } -// PowNFloat for V3I +// PowNFloat returns a new vector with each component raised to the power of n. func (v V3I[T]) PowNFloat(n float64) V3I[T] { return V3I[T]{X: T(math.Pow(float64(v.X), n)), Y: T(math.Pow(float64(v.Y), n)), Z: T(math.Pow(float64(v.Z), n))} } +// PowNFloatInPlace modifies vector by setting value of each component to the power of n. func (v *V3I[T]) PowNFloatInPlace(n float64) { v.X = T(math.Pow(float64(v.X), n)) v.Y = T(math.Pow(float64(v.Y), n)) diff --git a/round.go b/round.go index 6d7842f..a260106 100644 --- a/round.go +++ b/round.go @@ -4,10 +4,12 @@ import "math" // V2F +// Round returns a new vector with each component rounded to the nearest integer. func (v V2F[T]) Round() V2F[T] { return V2F[T]{X: T(math.Round(float64(v.X))), Y: T(math.Round(float64(v.Y)))} } +// RoundInPlace modifies vector by setting value of each component to the nearest integer. func (v *V2F[T]) RoundInPlace() { v.X = T(math.Round(float64(v.X))) v.Y = T(math.Round(float64(v.Y))) @@ -15,10 +17,12 @@ func (v *V2F[T]) RoundInPlace() { // V3F +// Round returns a new vector with each component rounded to the nearest integer. func (v V3F[T]) Round() V3F[T] { return V3F[T]{X: T(math.Round(float64(v.X))), Y: T(math.Round(float64(v.Y))), Z: T(math.Round(float64(v.Z)))} } +// RoundInPlace modifies vector by setting value of each component to the nearest integer. func (v *V3F[T]) RoundInPlace() { v.X = T(math.Round(float64(v.X))) v.Y = T(math.Round(float64(v.Y))) diff --git a/slice.go b/slice.go index 45b67ca..2676366 100644 --- a/slice.go +++ b/slice.go @@ -2,6 +2,7 @@ package govec import "golang.org/x/exp/constraints" +// V2FFromSlice returns a new V2F[T] with the given slice as components. func V2FFromSlice[T constraints.Float](a []T) V2F[T] { if len(a) < 2 { panic("vector.V2FFromSlice: slice length < 2") @@ -9,6 +10,7 @@ func V2FFromSlice[T constraints.Float](a []T) V2F[T] { return V2F[T]{X: a[0], Y: a[1]} } +// V3FFromSlice returns a new V3F with the given slice as components. func V3FFromSlice[T constraints.Float](a []T) V3F[T] { if len(a) < 3 { panic("vector.V3FFromSlice: slice length < 3") @@ -16,6 +18,7 @@ func V3FFromSlice[T constraints.Float](a []T) V3F[T] { return V3F[T]{X: a[0], Y: a[1], Z: a[2]} } +// V2IFromSlice returns a new V2I with the given slice as components. func V2IFromSlice[T constraints.Integer](a []T) V2I[T] { if len(a) < 2 { panic("vector.V2IFromSlice: slice length < 2") @@ -23,6 +26,7 @@ func V2IFromSlice[T constraints.Integer](a []T) V2I[T] { return V2I[T]{X: a[0], Y: a[1]} } +// V3IFromSlice returns a new V3I with the given slice as components. func V3IFromSlice[T constraints.Integer](a []T) V3I[T] { if len(a) < 3 { panic("vector.V3IFromSlice: slice length < 3") @@ -30,22 +34,27 @@ func V3IFromSlice[T constraints.Integer](a []T) V3I[T] { return V3I[T]{X: a[0], Y: a[1], Z: a[2]} } +// ToSlice returns a new slice with the components of the vector. func (v V2F[T]) ToSlice() []T { return []T{v.X, v.Y} } +// ToSlice returns a new slice with the components of the vector. func (v V3F[T]) ToSlice() []T { return []T{v.X, v.Y, v.Z} } +// ToSlice returns a new slice with the components of the vector. func (v V2I[T]) ToSlice() []T { return []T{v.X, v.Y} } +// ToSlice returns a new slice with the components of the vector. func (v V3I[T]) ToSlice() []T { return []T{v.X, v.Y, v.Z} } +// ApplyToSlice sets slice values to the components of the vector. func (v V2F[T]) ApplyToSlice(a []T) { if len(a) < 2 { panic("vector.V2F.ApplyToSlice: slice length < 2") @@ -54,6 +63,7 @@ func (v V2F[T]) ApplyToSlice(a []T) { a[1] = v.Y } +// ApplyToSlice sets slice values to the components of the vector. func (v V3F[T]) ApplyToSlice(a []T) { if len(a) < 3 { panic("vector.V3F.ApplyToSlice: slice length < 3") @@ -63,6 +73,7 @@ func (v V3F[T]) ApplyToSlice(a []T) { a[2] = v.Z } +// ApplyToSlice sets slice values to the components of the vector. func (v V2I[T]) ApplyToSlice(a []T) { if len(a) < 2 { panic("vector.V2I.ApplyToSlice: slice length < 2") @@ -71,6 +82,7 @@ func (v V2I[T]) ApplyToSlice(a []T) { a[1] = v.Y } +// ApplyToSlice sets slice values to the components of the vector. func (v V3I[T]) ApplyToSlice(a []T) { if len(a) < 3 { panic("vector.V3I.ApplyToSlice: slice length < 3") diff --git a/split.go b/split.go index d30bb3b..4d89d5e 100644 --- a/split.go +++ b/split.go @@ -1,17 +1,21 @@ package govec +// Split returns the components of the vector as separate values. func (v V2F[T]) split() (T, T) { return v.X, v.Y } +// Split returns the components of the vector as separate values. func (v V3F[T]) split() (T, T, T) { return v.X, v.Y, v.Z } +// Split returns the components of the vector as separate values. func (v V2I[T]) split() (T, T) { return v.X, v.Y } +// Split returns the components of the vector as separate values. func (v V3I[T]) split() (T, T, T) { return v.X, v.Y, v.Z } diff --git a/sqrt.go b/sqrt.go index e7f15ca..25cc01e 100644 --- a/sqrt.go +++ b/sqrt.go @@ -4,10 +4,12 @@ import "math" // V2F +// Sqrt returns a new vector with each component square rooted. func (v V2F[T]) Sqrt() V2F[T] { return V2F[T]{X: T(math.Sqrt(float64(v.X))), Y: T(math.Sqrt(float64(v.Y)))} } +// SqrtInPlace modifies vector by setting value of each component to the square root. func (v *V2F[T]) SqrtInPlace() { v.X = T(math.Sqrt(float64(v.X))) v.Y = T(math.Sqrt(float64(v.Y))) @@ -15,10 +17,12 @@ func (v *V2F[T]) SqrtInPlace() { // V3F +// Sqrt returns a new vector with each component square rooted. func (v V3F[T]) Sqrt() V3F[T] { return V3F[T]{X: T(math.Sqrt(float64(v.X))), Y: T(math.Sqrt(float64(v.Y))), Z: T(math.Sqrt(float64(v.Z)))} } +// SqrtInPlace modifies vector by setting value of each component to the square root. func (v *V3F[T]) SqrtInPlace() { v.X = T(math.Sqrt(float64(v.X))) v.Y = T(math.Sqrt(float64(v.Y))) @@ -27,12 +31,14 @@ func (v *V3F[T]) SqrtInPlace() { // V2I +// Sqrt returns a new V2F with each component square rooted. func (v V2I[T]) Sqrt() V2F[float64] { return V2F[float64]{X: math.Sqrt(float64(v.X)), Y: math.Sqrt(float64(v.Y))} } // V3I +// Sqrt returns a new V3F with each component square rooted. func (v V3I[T]) Sqrt() V3F[float64] { return V3F[float64]{X: math.Sqrt(float64(v.X)), Y: math.Sqrt(float64(v.Y)), Z: math.Sqrt(float64(v.Z))} } diff --git a/sub.go b/sub.go index 30f6518..af11f80 100644 --- a/sub.go +++ b/sub.go @@ -2,19 +2,23 @@ package govec // V2F +// Sub returns a new vector with each component subtracted from the corresponding component of v2. func (v V2F[T]) Sub(v2 V2F[T]) V2F[T] { return V2F[T]{X: v.X - v2.X, Y: v.Y - v2.Y} } +// SubInPlace modifies vector by subtracting each component from the corresponding component of v2. func (v *V2F[T]) SubInPlace(v2 V2F[T]) { v.X -= v2.X v.Y -= v2.Y } +// SubComp returns a new vector with each component subtracted from the corresponding values. func (v V2F[T]) SubComp(x T, y T) V2F[T] { return V2F[T]{X: v.X - x, Y: v.Y - y} } +// SubCompInPlace modifies vector by subtracting each component from the corresponding values. func (v *V2F[T]) SubCompInPlace(x T, y T) { v.X -= x v.Y -= y @@ -22,20 +26,24 @@ func (v *V2F[T]) SubCompInPlace(x T, y T) { // V3F +// Sub returns a new vector with each component subtracted from the corresponding component of v2. func (v V3F[T]) Sub(v2 V3F[T]) V3F[T] { return V3F[T]{X: v.X - v2.X, Y: v.Y - v2.Y, Z: v.Z - v2.Z} } +// SubInPlace modifies vector by subtracting each component from the corresponding component of v2. func (v *V3F[T]) SubInPlace(v2 V3F[T]) { v.X -= v2.X v.Y -= v2.Y v.Z -= v2.Z } +// SubComp returns a new vector with each component subtracted from the corresponding values. func (v V3F[T]) SubComp(x T, y T, z T) V3F[T] { return V3F[T]{X: v.X - x, Y: v.Y - y, Z: v.Z - z} } +// SubCompInPlace modifies vector by subtracting each component from the corresponding values. func (v *V3F[T]) SubCompInPlace(x T, y T, z T) { v.X -= x v.Y -= y @@ -44,19 +52,23 @@ func (v *V3F[T]) SubCompInPlace(x T, y T, z T) { // V2I +// Sub returns a new vector with each component subtracted from the corresponding component of v2. func (v V2I[T]) Sub(v2 V2I[T]) V2I[T] { return V2I[T]{X: v.X - v2.X, Y: v.Y - v2.Y} } +// SubInPlace modifies vector by subtracting each component from the corresponding component of v2. func (v *V2I[T]) SubInPlace(v2 V2I[T]) { v.X -= v2.X v.Y -= v2.Y } +// SubComp returns a new vector with each component subtracted from the corresponding values. func (v V2I[T]) SubComp(x T, y T) V2I[T] { return V2I[T]{X: v.X - x, Y: v.Y - y} } +// SubCompInPlace modifies vector by subtracting each component from the corresponding values. func (v *V2I[T]) SubCompInPlace(x T, y T) { v.X -= x v.Y -= y @@ -64,20 +76,24 @@ func (v *V2I[T]) SubCompInPlace(x T, y T) { // V3I +// Sub returns a new vector with each component subtracted from the corresponding component of v2. func (v V3I[T]) Sub(v2 V3I[T]) V3I[T] { return V3I[T]{X: v.X - v2.X, Y: v.Y - v2.Y, Z: v.Z - v2.Z} } +// SubInPlace modifies vector by subtracting each component from the corresponding component of v2. func (v *V3I[T]) SubInPlace(v2 V3I[T]) { v.X -= v2.X v.Y -= v2.Y v.Z -= v2.Z } +// SubComp returns a new vector with each component subtracted from the corresponding values. func (v V3I[T]) SubComp(x T, y T, z T) V3I[T] { return V3I[T]{X: v.X - x, Y: v.Y - y, Z: v.Z - z} } +// SubCompInPlace modifies vector by subtracting each component from the corresponding values. func (v *V3I[T]) SubCompInPlace(x T, y T, z T) { v.X -= x v.Y -= y diff --git a/subScalar.go b/subScalar.go index 101828b..ca12829 100644 --- a/subScalar.go +++ b/subScalar.go @@ -2,10 +2,12 @@ package govec // V2F +// SubScalar returns a new vector with each component subtracted by scalar. func (v V2F[T]) SubScalar(scalar T) V2F[T] { return V2F[T]{X: v.X - scalar, Y: v.Y - scalar} } +// SubScalarInPlace modifies vector by subtracting each component by scalar. func (v *V2F[T]) SubScalarInPlace(scalar T) { v.X -= scalar v.Y -= scalar @@ -13,10 +15,12 @@ func (v *V2F[T]) SubScalarInPlace(scalar T) { // V3F +// SubScalar returns a new vector with each component subtracted by scalar. func (v V3F[T]) SubScalar(scalar T) V3F[T] { return V3F[T]{X: v.X - scalar, Y: v.Y - scalar, Z: v.Z - scalar} } +// SubScalarInPlace modifies vector by subtracting each component by scalar. func (v *V3F[T]) SubScalarInPlace(scalar T) { v.X -= scalar v.Y -= scalar @@ -25,10 +29,12 @@ func (v *V3F[T]) SubScalarInPlace(scalar T) { // V2I +// SubScalar returns a new vector with each component subtracted by scalar. func (v V2I[T]) SubScalar(scalar T) V2I[T] { return V2I[T]{X: v.X - scalar, Y: v.Y - scalar} } +// SubScalarInPlace modifies vector by subtracting each component by scalar. func (v *V2I[T]) SubScalarInPlace(scalar T) { v.X -= scalar v.Y -= scalar @@ -36,10 +42,12 @@ func (v *V2I[T]) SubScalarInPlace(scalar T) { // V3I +// SubScalar returns a new vector with each component subtracted by scalar. func (v V3I[T]) SubScalar(scalar T) V3I[T] { return V3I[T]{X: v.X - scalar, Y: v.Y - scalar, Z: v.Z - scalar} } +// SubScalarInPlace modifies vector by subtracting each component by scalar. func (v *V3I[T]) SubScalarInPlace(scalar T) { v.X -= scalar v.Y -= scalar diff --git a/testUtils.go b/testUtils.go index 5640115..a73ef15 100644 --- a/testUtils.go +++ b/testUtils.go @@ -5,6 +5,7 @@ import ( "math" ) +// almostEqual returns true if the difference between a and b is less than epsilon. func almostEqual[T constraints.Float](a, b, epsilon T) bool { return math.Abs(float64(a-b)) <= float64(epsilon) } diff --git a/vec2f.go b/vec2f.go index af83cbd..d4f1ddb 100644 --- a/vec2f.go +++ b/vec2f.go @@ -2,35 +2,43 @@ package govec import "golang.org/x/exp/constraints" +// V2F is a 2-dimensional vector with components of type T, where T is a float type. type V2F[T constraints.Float] struct { X T Y T } +// ToV2F64 returns a new V2F with the components converted to float64. func (v V2F[T]) ToV2F64() V2F[float64] { return V2F[float64]{X: float64(v.X), Y: float64(v.Y)} } +// ToV2F32 returns a new V2F with the components converted to float32. func (v V2F[T]) ToV2F32() V2F[float32] { return V2F[float32]{X: float32(v.X), Y: float32(v.Y)} } +// ToV2I64 returns a new V2I with the components converted to int64. func (v V2F[T]) ToV2I64() V2I[int64] { return V2I[int64]{X: int64(v.X), Y: int64(v.Y)} } +// ToV2I32 returns a new V2I with the components converted to int32. func (v V2F[T]) ToV2I32() V2I[int32] { return V2I[int32]{X: int32(v.X), Y: int32(v.Y)} } +// ToV2I16 returns a new V2I with the components converted to int16. func (v V2F[T]) ToV2I16() V2I[int16] { return V2I[int16]{X: int16(v.X), Y: int16(v.Y)} } +// ToV2I8 returns a new V2I with the components converted to int8. func (v V2F[T]) ToV2I8() V2I[int8] { return V2I[int8]{X: int8(v.X), Y: int8(v.Y)} } +// ToV2I returns a new V2I with the components converted to int. func (v V2F[T]) ToV2I() V2I[int] { return V2I[int]{X: int(v.X), Y: int(v.Y)} } diff --git a/vec2i.go b/vec2i.go index 594373a..9d6befb 100644 --- a/vec2i.go +++ b/vec2i.go @@ -2,35 +2,43 @@ package govec import "golang.org/x/exp/constraints" +// V2I is a 2-dimensional vector with components of type T, where T is an integer type. type V2I[T constraints.Integer] struct { X T Y T } +// ToV2F64 returns a new V2F with the components converted to float64. func (v V2I[T]) ToV2F64() V2F[float64] { return V2F[float64]{X: float64(v.X), Y: float64(v.Y)} } +// ToV2F32 returns a new V2F with the components converted to float32. func (v V2I[T]) ToV2F32() V2F[float32] { return V2F[float32]{X: float32(v.X), Y: float32(v.Y)} } +// ToV2I64 returns a new V2I with the components converted to int64. func (v V2I[T]) ToV2I64() V2I[int64] { return V2I[int64]{X: int64(v.X), Y: int64(v.Y)} } +// ToV2I32 returns a new V2I with the components converted to int32. func (v V2I[T]) ToV2I32() V2I[int32] { return V2I[int32]{X: int32(v.X), Y: int32(v.Y)} } +// ToV2I16 returns a new V2I with the components converted to int16. func (v V2I[T]) ToV2I16() V2I[int16] { return V2I[int16]{X: int16(v.X), Y: int16(v.Y)} } +// ToV2I8 returns a new V2I with the components converted to int8. func (v V2I[T]) ToV2I8() V2I[int8] { return V2I[int8]{X: int8(v.X), Y: int8(v.Y)} } +// ToV2I returns a new V2I with the components converted to int. func (v V2I[T]) ToV2I() V2I[int] { return V2I[int]{X: int(v.X), Y: int(v.Y)} } diff --git a/vec3f.go b/vec3f.go index d260a5a..54011bb 100644 --- a/vec3f.go +++ b/vec3f.go @@ -2,36 +2,44 @@ package govec import "golang.org/x/exp/constraints" +// V3F is a 3-dimensional vector with components of type T, where T is a float type. type V3F[T constraints.Float] struct { X T Y T Z T } +// ToV3F64 returns a new V3F with the components converted to float64. func (v V3F[T]) ToV3F64() V3F[float64] { return V3F[float64]{X: float64(v.X), Y: float64(v.Y), Z: float64(v.Z)} } +// ToV3F32 returns a new V3F with the components converted to float32. func (v V3F[T]) ToV3F32() V3F[float32] { return V3F[float32]{X: float32(v.X), Y: float32(v.Y), Z: float32(v.Z)} } +// ToV3I64 returns a new V3I with the components converted to int64. func (v V3F[T]) ToV3I64() V3I[int64] { return V3I[int64]{X: int64(v.X), Y: int64(v.Y), Z: int64(v.Z)} } +// ToV3I32 returns a new V3I with the components converted to int32. func (v V3F[T]) ToV3I32() V3I[int32] { return V3I[int32]{X: int32(v.X), Y: int32(v.Y), Z: int32(v.Z)} } +// ToV3I16 returns a new V3I with the components converted to int16. func (v V3F[T]) ToV3I16() V3I[int16] { return V3I[int16]{X: int16(v.X), Y: int16(v.Y), Z: int16(v.Z)} } +// ToV3I8 returns a new V3I with the components converted to int8. func (v V3F[T]) ToV3I8() V3I[int8] { return V3I[int8]{X: int8(v.X), Y: int8(v.Y), Z: int8(v.Z)} } +// ToV3I returns a new V3I with the components converted to int. func (v V3F[T]) ToV3I() V3I[int] { return V3I[int]{X: int(v.X), Y: int(v.Y), Z: int(v.Z)} } diff --git a/vec3i.go b/vec3i.go index 74f4d56..ec805d7 100644 --- a/vec3i.go +++ b/vec3i.go @@ -2,36 +2,44 @@ package govec import "golang.org/x/exp/constraints" +// V3I is a 3-dimensional vector with components of type T, where T is an integer type. type V3I[T constraints.Integer] struct { X T Y T Z T } +// ToV3F64 returns a new V3F with the components converted to float64. func (v V3I[T]) ToV3F64() V3F[float64] { return V3F[float64]{X: float64(v.X), Y: float64(v.Y), Z: float64(v.Z)} } +// ToV3F32 returns a new V3F with the components converted to float32. func (v V3I[T]) ToV3F32() V3F[float32] { return V3F[float32]{X: float32(v.X), Y: float32(v.Y), Z: float32(v.Z)} } +// ToV3I64 returns a new V3I with the components converted to int64. func (v V3I[T]) ToV3I64() V3I[int64] { return V3I[int64]{X: int64(v.X), Y: int64(v.Y), Z: int64(v.Z)} } +// ToV3I32 returns a new V3I with the components converted to int32. func (v V3I[T]) ToV3I32() V3I[int32] { return V3I[int32]{X: int32(v.X), Y: int32(v.Y), Z: int32(v.Z)} } +// ToV3I16 returns a new V3I with the components converted to int16. func (v V3I[T]) ToV3I16() V3I[int16] { return V3I[int16]{X: int16(v.X), Y: int16(v.Y), Z: int16(v.Z)} } +// ToV3I8 returns a new V3I with the components converted to int8. func (v V3I[T]) ToV3I8() V3I[int8] { return V3I[int8]{X: int8(v.X), Y: int8(v.Y), Z: int8(v.Z)} } +// ToV3I returns a new V3I with the components converted to int. func (v V3I[T]) ToV3I() V3I[int] { return V3I[int]{X: int(v.X), Y: int(v.Y), Z: int(v.Z)} }