Skip to content

Commit

Permalink
Initial upload of the package
Browse files Browse the repository at this point in the history
  • Loading branch information
Xkonti committed Sep 14, 2023
1 parent dfdd0c7 commit edfa301
Show file tree
Hide file tree
Showing 45 changed files with 2,056 additions and 2 deletions.
30 changes: 30 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Allowlisting gitignore template for GO projects prevents us
# from adding various unwanted local files, such as generated
# files, developer configurations or IDE-specific files etc.
#
# Recommended: Go.AllowList.gitignore

# Ignore everything
*

# But not these files...
!/.gitignore

!*.go
!go.sum
!go.mod

!README.md
!LICENSE

# Configuration files
!*.yaml
!*.json
!*.toml
!*.sh

# ...even if they are in subdirectories
!*/

# Allow everything in assets folder
!/assets/**
99 changes: 97 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,97 @@
# govec
Go library providing 2D and 3D vector operations
# GoVec
A library providing vector operations for Go.

The library supports both 2D and 3D vectors in two formats:
- Floating Point Vectors: `V2F[T]` and `V3F[T]`
- Integer Vectors: `V2I[T]` and `V3I[T]`

## Installation

```bash
go get github.com/xkonti/govec
```

## Available Operations

| Operation | Float | Integer | Description |
|-----------------|-------|------|--------------------------------------------------------------------------------------------|
| **Creation** | | | |
| `...FromArray` | ✔️ | ✔️ | Initializes a vector from an array of components. |
| `...FromSlice` | ✔️ | ✔️ | Initializes a vector from a slice of components. |
| `Fill` | ✔️ | ✔️ | Creates a vector where all components are set to a specified value. |
| `New` | ✔️ | ✔️ | Creates a new vector using the provided components. |
| `One` | ✔️ | ✔️ | Generates a vector with all components set to 1. |
| `Zero` | ✔️ | ✔️ | Generates a vector with all components set to 0. |
| **Conversions** | | | |
| `Apply` | ✔️ | ✔️ | Applies a given function to each component in the vector. |
| `ApplyToArray` | ✔️ | ✔️ | Modifies an array by assigning values from the vector's corresponding components. |
| `ApplyToSlice` | ✔️ | ✔️ | Modifies a slice by assigning values from the vector's corresponding components. |
| `Discard` | ✔️ | ✔️ | Removes a component from the vector at a specified index. |
| `Extend` | ✔️ | ✔️ | Extends the dimensionality of a vector by adding additional components. |
| `Insert` | ✔️ | ✔️ | Inserts a value into the vector at a specified index. |
| `Split` | ✔️ | ✔️ | Splits a vector into its individual components. |
| `ToArray` | ✔️ | ✔️ | Converts the vector to a new array. |
| `ToSlice` | ✔️ | ✔️ | Converts the vector to a new slice. |
| `To...` | ✔️ | ✔️ | Transforms the vector into another type. |
| **Mathematics** | | | |
| `Abs` | ✔️ | ✔️ | Computes the absolute value of each component in the vector. |
| `Add` | ✔️ | ✔️ | Performs vector addition. |
| `AddScalar` | ✔️ | ✔️ | Adds a scalar value to each component of the vector. |
| `Ceil` | ✔️ | ✔️ | Rounds each component of the vector up to the nearest integer. |
| `Cross` | ✔️ | ✔️ | Computes the cross product of two vectors. (Not applicable for 2D vectors.) |
| `Div` | ✔️ | ✔️ | Performs vector division. |
| `DivScalar` | ✔️ | ✔️ | Divides each component of the vector by a scalar. |
| `Dot` | ✔️ | ✔️ | Computes the dot product of two vectors. |
| `Floor` | ✔️ | ✔️ | Rounds each component of the vector down to the nearest integer. |
| `Inv` | ✔️ | ✔️ | Computes the multiplicative inverse of each component in the vector. |
| `Len` | ✔️ | ✔️ | Calculates the length of the vector. Returns `float64` for integer vectors. |
| `LenSqr` | ✔️ | ✔️ | Calculates the squared length of the vector. Returns `float64` for integer vectors. |
| `Mod` | ✔️ | ✔️ | Computes the modulus of each component in the vector. |
| `Mul` | ✔️ | ✔️ | Performs vector multiplication. |
| `MulScalar` | ✔️ | ✔️ | Multiplies each component of the vector by a scalar. |
| `Neg` | ✔️ | ✔️ | Negates each component of the vector. |
| `Norm` | ✔️ | ✔️ | Normalizes the vector. Returns a `float64` vector for integer vectors. |
| `Pow` | ✔️ | ✔️ | Raises each component of the vector to the power of the corresponding component in another vector.|
| `Pow2` | ✔️ | ✔️ | Squares each component of the vector. |
| `Pow3` | ✔️ | ✔️ | Cubes each component of the vector. |
| `PowN` | ✔️ | ✔️ | Raises each component of the vector to a specified integer power. |
| `PowNFloat` | ✔️ | ✔️ | Raises each component of the vector to a specified floating-point power. |
| `Round` | ✔️ | ✔️ | Rounds each component of the vector to the nearest integer. |
| `Sqrt` | ✔️ | ✔️ | Computes the square root of each component in the vector. |
| `Sub` | ✔️ | ✔️ | Performs vector subtraction. |
| `SubScalar` | ✔️ | ✔️ | 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. |
| `ClampComponents` | Yes (v1.0) | Clamps the components of this vector to the given range. |
| `ClampMagnitude` | Yes (v1.0) | Clamps the magnitude of this vector to the given value. |
| `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. |
| `Max` | Yes (v1.0) | Returns the maximum component values from two vectors. |
| `Min` | Yes (v1.0) | Returns the minimum component values from two vectors. |
| `Orthogonalize` | | Generates an orthogonal (or orthonormal) vector set. |
| `Project` | | Projects a 3D vector onto a plane. |
| `Random` | Yes (v1.0) | Generates a normal vector with random components. |
| `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. |
107 changes: 107 additions & 0 deletions abs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package govec

// V2F

func (v V2F[T]) Abs() V2F[T] {
absX := v.X
absY := v.Y
if absX < 0 {
absX = -absX
}
if absY < 0 {
absY = -absY
}
return V2F[T]{X: absX, Y: absY}
}

func (v *V2F[T]) AbsInPlace() {
if v.X < 0 {
v.X = -v.X
}
if v.Y < 0 {
v.Y = -v.Y
}
}

// V3F

func (v V3F[T]) Abs() V3F[T] {
absX := v.X
absY := v.Y
absZ := v.Z
if absX < 0 {
absX = -absX
}
if absY < 0 {
absY = -absY
}
if absZ < 0 {
absZ = -absZ
}
return V3F[T]{X: absX, Y: absY, Z: absZ}
}

func (v *V3F[T]) AbsInPlace() {
if v.X < 0 {
v.X = -v.X
}
if v.Y < 0 {
v.Y = -v.Y
}
if v.Z < 0 {
v.Z = -v.Z
}
}

// V2I

func (v V2I[T]) Abs() V2I[T] {
absX := v.X
absY := v.Y
if absX < 0 {
absX = -absX
}
if absY < 0 {
absY = -absY
}
return V2I[T]{X: absX, Y: absY}
}

func (v *V2I[T]) AbsInPlace() {
if v.X < 0 {
v.X = -v.X
}
if v.Y < 0 {
v.Y = -v.Y
}
}

// V3I

func (v V3I[T]) Abs() V3I[T] {
absX := v.X
absY := v.Y
absZ := v.Z
if absX < 0 {
absX = -absX
}
if absY < 0 {
absY = -absY
}
if absZ < 0 {
absZ = -absZ
}
return V3I[T]{X: absX, Y: absY, Z: absZ}
}

func (v *V3I[T]) AbsInPlace() {
if v.X < 0 {
v.X = -v.X
}
if v.Y < 0 {
v.Y = -v.Y
}
if v.Z < 0 {
v.Z = -v.Z
}
}
85 changes: 85 additions & 0 deletions add.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package govec

// V2F

func (v V2F[T]) Add(v2 V2F[T]) V2F[T] {
return V2F[T]{X: v.X + v2.X, Y: v.Y + v2.Y}
}

func (v *V2F[T]) AddInPlace(v2 V2F[T]) {
v.X += v2.X
v.Y += v2.Y
}

func (v V2F[T]) AddComp(x T, y T) V2F[T] {
return V2F[T]{X: v.X + x, Y: v.Y + y}
}

func (v *V2F[T]) AddCompInPlace(x T, y T) {
v.X += x
v.Y += y
}

// V3F

func (v V3F[T]) Add(v2 V3F[T]) V3F[T] {
return V3F[T]{X: v.X + v2.X, Y: v.Y + v2.Y, Z: v.Z + v2.Z}
}

func (v *V3F[T]) AddInPlace(v2 V3F[T]) {
v.X += v2.X
v.Y += v2.Y
v.Z += v2.Z
}

func (v V3F[T]) AddComp(x T, y T, z T) V3F[T] {
return V3F[T]{X: v.X + x, Y: v.Y + y, Z: v.Z + z}
}

func (v *V3F[T]) AddCompInPlace(x T, y T, z T) {
v.X += x
v.Y += y
v.Z += z
}

// V2I

func (v V2I[T]) Add(v2 V2I[T]) V2I[T] {
return V2I[T]{X: v.X + v2.X, Y: v.Y + v2.Y}
}

func (v *V2I[T]) AddInPlace(v2 V2I[T]) {
v.X += v2.X
v.Y += v2.Y
}

func (v V2I[T]) AddComp(x T, y T) V2I[T] {
return V2I[T]{X: v.X + x, Y: v.Y + y}
}

func (v *V2I[T]) AddCompInPlace(x T, y T) {
v.X += x
v.Y += y
}

// V3I

func (v V3I[T]) Add(v2 V3I[T]) V3I[T] {
return V3I[T]{X: v.X + v2.X, Y: v.Y + v2.Y, Z: v.Z + v2.Z}
}

func (v *V3I[T]) AddInPlace(v2 V3I[T]) {
v.X += v2.X
v.Y += v2.Y
v.Z += v2.Z
}

func (v V3I[T]) AddComp(x T, y T, z T) V3I[T] {
return V3I[T]{X: v.X + x, Y: v.Y + y, Z: v.Z + z}
}

func (v *V3I[T]) AddCompInPlace(x T, y T, z T) {
v.X += x
v.Y += y
v.Z += z
}
47 changes: 47 additions & 0 deletions addScalar.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package govec

// V2F

func (v V2F[T]) AddScalar(scalar T) V2F[T] {
return V2F[T]{X: v.X + scalar, Y: v.Y + scalar}
}

func (v *V2F[T]) AddScalarInPlace(scalar T) {
v.X += scalar
v.Y += scalar
}

// V3F

func (v V3F[T]) AddScalar(scalar T) V3F[T] {
return V3F[T]{X: v.X + scalar, Y: v.Y + scalar, Z: v.Z + scalar}
}

func (v *V3F[T]) AddScalarInPlace(scalar T) {
v.X += scalar
v.Y += scalar
v.Z += scalar
}

// V2I

func (v V2I[T]) AddScalar(scalar T) V2I[T] {
return V2I[T]{X: v.X + scalar, Y: v.Y + scalar}
}

func (v *V2I[T]) AddScalarInPlace(scalar T) {
v.X += scalar
v.Y += scalar
}

// V3I

func (v V3I[T]) AddScalar(scalar T) V3I[T] {
return V3I[T]{X: v.X + scalar, Y: v.Y + scalar, Z: v.Z + scalar}
}

func (v *V3I[T]) AddScalarInPlace(scalar T) {
v.X += scalar
v.Y += scalar
v.Z += scalar
}
Loading

0 comments on commit edfa301

Please sign in to comment.