Skip to content

Commit

Permalink
more swizzle commands
Browse files Browse the repository at this point in the history
  • Loading branch information
EliCDavis committed Dec 3, 2023
1 parent 7da17ad commit 2d786d9
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 0 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ Collection of **generic, immutable** vector math functions I've written overtime
| Y |||| Returns the y component of the vector |
| Z | ||| Returns the z component of the vector |
| W | | || Returns the w component of the vector |
| XY | ||| Equivalent to vector2.New[T](v.x, v.y) |
| YZ | ||| Equivalent to vector2.New[T](v.y, v.z) |
| XZ | ||| Equivalent to vector2.New[T](v.x, v.z) |
| YX | ||| Equivalent to vector2.New[T](v.y, v.x) |
| ZY | ||| Equivalent to vector2.New[T](v.z, v.y) |
| ZX | ||| Equivalent to vector2.New[T](v.z, v.x) |


## Example
Expand Down
15 changes: 15 additions & 0 deletions vector3/vector3.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,21 @@ func (v Vector[T]) YZ() vector2.Vector[T] {
return vector2.New(v.y, v.z)
}

// YX returns vector2 with the y and x components
func (v Vector[T]) YX() vector2.Vector[T] {
return vector2.New(v.y, v.x)
}

// ZX returns vector2 with the z and x components
func (v Vector[T]) ZX() vector2.Vector[T] {
return vector2.New(v.z, v.x)
}

// ZY returns vector2 with the z and y components
func (v Vector[T]) ZY() vector2.Vector[T] {
return vector2.New(v.z, v.y)
}

// Midpoint returns the midpoint between this vector and the vector passed in.
func (v Vector[T]) Midpoint(o Vector[T]) Vector[T] {
return o.Add(v).Scale(0.5)
Expand Down
3 changes: 3 additions & 0 deletions vector3/vector3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ func TestToVector2(t *testing.T) {
"xy": {got: start.XY(), want: vector2.New(1.2, -2.4)},
"yz": {got: start.YZ(), want: vector2.New(-2.4, 3.7)},
"xz": {got: start.XZ(), want: vector2.New(1.2, 3.7)},
"yx": {got: start.YX(), want: vector2.New(-2.4, 1.2)},
"zy": {got: start.ZY(), want: vector2.New(3.7, -2.4)},
"zx": {got: start.ZX(), want: vector2.New(3.7, 1.2)},
}

for name, tc := range tests {
Expand Down
36 changes: 36 additions & 0 deletions vector4/vector4.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"math"

"github.com/EliCDavis/vector"
"github.com/EliCDavis/vector/vector2"
"github.com/EliCDavis/vector/vector3"
)

// Vector contains 4 components
Expand Down Expand Up @@ -495,3 +497,37 @@ func (v Vector[T]) FlipW() Vector[T] {
w: v.w * -1,
}
}

func (v Vector[T]) XYZ() vector3.Vector[T] {
return vector3.New[T](v.x, v.y, v.z)
}

// XY returns vector2 with the x and y components
func (v Vector[T]) XY() vector2.Vector[T] {
return vector2.New(v.x, v.y)
}

// XZ returns vector2 with the x and z components
func (v Vector[T]) XZ() vector2.Vector[T] {
return vector2.New(v.x, v.z)
}

// YZ returns vector2 with the y and z components
func (v Vector[T]) YZ() vector2.Vector[T] {
return vector2.New(v.y, v.z)
}

// YX returns vector2 with the y and x components
func (v Vector[T]) YX() vector2.Vector[T] {
return vector2.New(v.y, v.x)
}

// ZX returns vector2 with the z and x components
func (v Vector[T]) ZX() vector2.Vector[T] {
return vector2.New(v.z, v.x)
}

// ZY returns vector2 with the z and y components
func (v Vector[T]) ZY() vector2.Vector[T] {
return vector2.New(v.z, v.y)
}
44 changes: 44 additions & 0 deletions vector4/vector4_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"math"
"testing"

"github.com/EliCDavis/vector/vector2"
"github.com/EliCDavis/vector/vector3"
"github.com/EliCDavis/vector/vector4"
"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -117,6 +119,48 @@ func TestScaleVecFloat(t *testing.T) {
}
}

func TestSwizzle_Vector3(t *testing.T) {
in := vector4.New(1., 2., 3., 4.)

tests := map[string]struct {
expected vector3.Float64
got vector3.Float64
}{
"XYZ": {expected: vector3.New(1., 2., 3.), got: in.XYZ()},
}

for name, tc := range tests {
t.Run(name, func(t *testing.T) {
assert.Equal(t, tc.expected.X(), tc.got.X())
assert.Equal(t, tc.expected.Y(), tc.got.Y())
assert.Equal(t, tc.expected.Z(), tc.got.Z())
})
}
}

func TestSwizzle_Vector2(t *testing.T) {
start := vector3.New(1.2, -2.4, 3.7)

tests := map[string]struct {
got vector2.Float64
want vector2.Float64
}{
"xy": {got: start.XY(), want: vector2.New(1.2, -2.4)},
"yz": {got: start.YZ(), want: vector2.New(-2.4, 3.7)},
"xz": {got: start.XZ(), want: vector2.New(1.2, 3.7)},
"yx": {got: start.YX(), want: vector2.New(-2.4, 1.2)},
"zy": {got: start.ZY(), want: vector2.New(3.7, -2.4)},
"zx": {got: start.ZX(), want: vector2.New(3.7, 1.2)},
}

for name, tc := range tests {
t.Run(name, func(t *testing.T) {
assert.Equal(t, tc.want.X(), tc.got.X())
assert.Equal(t, tc.want.Y(), tc.got.Y())
})
}
}

func TestJSON(t *testing.T) {
in := vector4.New(1.2, 2.3, 3.4, 5.6)
out := vector4.New(0., 0., 0., 0.)
Expand Down

0 comments on commit 2d786d9

Please sign in to comment.