Skip to content

Commit

Permalink
Vector4 normalized
Browse files Browse the repository at this point in the history
  • Loading branch information
EliCDavis committed Dec 3, 2023
1 parent 3f84534 commit 6db2b8b
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

Collection of **generic, immutable** vector math functions I've written overtime for different hobby projects.

Has support for both Vector2 (x, y), Vector3 (x, y, z), and Vector4 (x, y, z, w) functions.

## API

| Function | Vector2 | Vector3 | Vector4 | Description |
Expand All @@ -27,7 +25,7 @@ Has support for both Vector2 (x, y), Vector3 (x, y, z), and Vector4 (x, y, z, w)
| MaxComponent |||| Returns the vectors largest component |
| Midpoint |||| Finds the mid point between two vectors |
| MinComponent |||| Returns the vectors smallest component |
| Normalize |||| Returns the normalized vector |
| Normalized |||| Returns the normalized vector |
| Round |||| Rounds each vectors component to the nearest integer |
| Scale |||| Scales the vector by some constant |
| Sqrt |||| Returns a vector with each component's square root |
Expand Down
12 changes: 12 additions & 0 deletions vector4/vector4.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,18 @@ func (v Vector[T]) Dot(other Vector[T]) float64 {
return float64((v.x * other.x) + (v.y * other.y) + (v.z * other.z) + (v.w * other.w))
}

func (v Vector[T]) Normalized() Vector[T] {
return v.DivByConstant(v.Length())
}

func (v Vector[T]) Length() float64 {
return math.Sqrt(v.LengthSquared())
}

func (v Vector[T]) LengthSquared() float64 {
return float64((v.x * v.x) + (v.y * v.y) + (v.z * v.z) + (v.w * v.w))
}

// Sqrt applies the math.Sqrt to each component of the vector
func (v Vector[T]) Sqrt() Vector[T] {
return New(
Expand Down
1 change: 1 addition & 0 deletions vector4/vector4_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ func TestVectorOperations(t *testing.T) {
"flipY": {got: start.FlipY(), want: vector4.New(1.2, 2.4, 3.7, 4.9)},
"flipZ": {got: start.FlipZ(), want: vector4.New(1.2, -2.4, -3.7, 4.9)},
"flipW": {got: start.FlipW(), want: vector4.New(1.2, -2.4, 3.7, -4.9)},
"normalize": {got: start.Normalized(), want: vector4.New(0.1790845316, -0.35816906, 0.5521773, 0.73126183)},
}

for name, tc := range tests {
Expand Down

0 comments on commit 6db2b8b

Please sign in to comment.