Skip to content

Commit

Permalink
Component function
Browse files Browse the repository at this point in the history
  • Loading branch information
EliCDavis committed Nov 16, 2024
1 parent 34127c9 commit ad87d1d
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 0 deletions.
13 changes: 13 additions & 0 deletions vector2/vector2.go
Original file line number Diff line number Diff line change
Expand Up @@ -498,3 +498,16 @@ func (v Vector[T]) Expm1() Vector[T] {
func (v Vector[T]) Values() (T, T) {
return v.x, v.y
}

func (v Vector[T]) Component(index int) T {
switch index {
case 0:
return v.x

case 1:
return v.y

default:
panic(fmt.Errorf("invalid index: %d", index))
}
}
22 changes: 22 additions & 0 deletions vector2/vector2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -458,3 +458,25 @@ func TestValues(t *testing.T) {
assert.Equal(t, x, 1)
assert.Equal(t, y, 2)
}

func TestComponent(t *testing.T) {

v := vector2.New(1., 2.)
tests := map[string]struct {
component int
want float64
}{
"0": {component: 0, want: 1.},
"1": {component: 1, want: 2.},
}

for name, tc := range tests {
t.Run(name, func(t *testing.T) {
assert.Equal(t, tc.want, v.Component(tc.component))
})
}

assert.PanicsWithError(t, "invalid index: -1", func() {
v.Component(-1)
})
}
16 changes: 16 additions & 0 deletions vector3/vector3.go
Original file line number Diff line number Diff line change
Expand Up @@ -741,3 +741,19 @@ func (v Vector[T]) Reciprocal() Vector[float64] {
z: 1.0 / float64(v.z),
}
}

func (v Vector[T]) Component(index int) T {
switch index {
case 0:
return v.x

case 1:
return v.y

case 2:
return v.z

default:
panic(fmt.Errorf("invalid index: %d", index))
}
}
23 changes: 23 additions & 0 deletions vector3/vector3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -598,3 +598,26 @@ func TestValues(t *testing.T) {
assert.Equal(t, y, 2)
assert.Equal(t, z, 3)
}

func TestComponent(t *testing.T) {

v := vector3.New(1., 2., 3.)
tests := map[string]struct {
component int
want float64
}{
"0": {component: 0, want: 1.},
"1": {component: 1, want: 2.},
"2": {component: 2, want: 3.},
}

for name, tc := range tests {
t.Run(name, func(t *testing.T) {
assert.Equal(t, tc.want, v.Component(tc.component))
})
}

assert.PanicsWithError(t, "invalid index: -1", func() {
v.Component(-1)
})
}
19 changes: 19 additions & 0 deletions vector4/vector4.go
Original file line number Diff line number Diff line change
Expand Up @@ -659,3 +659,22 @@ func (v Vector[T]) Expm1() Vector[T] {
func (v Vector[T]) Values() (T, T, T, T) {
return v.x, v.y, v.z, v.w
}

func (v Vector[T]) Component(index int) T {
switch index {
case 0:
return v.x

case 1:
return v.y

case 2:
return v.z

case 3:
return v.w

default:
panic(fmt.Errorf("invalid index: %d", index))
}
}
24 changes: 24 additions & 0 deletions vector4/vector4_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,30 @@ func TestValues(t *testing.T) {
assert.Equal(t, w, 4)
}

func TestComponent(t *testing.T) {

v := vector4.New(1., 2., 3., 4.)
tests := map[string]struct {
component int
want float64
}{
"0": {component: 0, want: 1.},
"1": {component: 1, want: 2.},
"2": {component: 2, want: 3.},
"3": {component: 3, want: 4.},
}

for name, tc := range tests {
t.Run(name, func(t *testing.T) {
assert.Equal(t, tc.want, v.Component(tc.component))
})
}

assert.PanicsWithError(t, "invalid index: -1", func() {
v.Component(-1)
})
}

var result vector4.Float64

func BenchmarkLerp(b *testing.B) {
Expand Down

0 comments on commit ad87d1d

Please sign in to comment.