Skip to content

Commit

Permalink
Prep for release 0.2.2 (#35)
Browse files Browse the repository at this point in the history
- Properly expose `Discard`, `Extend`, and `Insert` operations
- Document all remaining functions - now everything should have some
description in docs
- Update `README.md` with newly implemented operations
- Simplify operations table in `README.md`
- Add `Remainder` operation to the *planned* list
  • Loading branch information
Xkonti authored Oct 5, 2023
1 parent 27ae3cb commit f74bc26
Show file tree
Hide file tree
Showing 41 changed files with 490 additions and 118 deletions.
161 changes: 81 additions & 80 deletions README.md

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions abs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
12 changes: 12 additions & 0 deletions array.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions ceil.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,25 @@ 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)))
}

// 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)))
Expand Down
16 changes: 16 additions & 0 deletions clampComp.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,48 @@ 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)
}

// 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)
Expand All @@ -44,40 +52,48 @@ 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)
}

// 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)
Expand Down
14 changes: 14 additions & 0 deletions clampLen.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down
8 changes: 8 additions & 0 deletions cross.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -10,13 +11,15 @@ 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
z := v.X*v2.Y - v.Y*v2.X
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,
Expand All @@ -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
Expand All @@ -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,
Expand All @@ -42,13 +47,15 @@ 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
z := v.X*v2.Y - v.Y*v2.X
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,
Expand All @@ -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
Expand Down
18 changes: 12 additions & 6 deletions discard.go
Original file line number Diff line number Diff line change
@@ -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}
}
Loading

0 comments on commit f74bc26

Please sign in to comment.