Skip to content

Commit 0050a18

Browse files
committed
vector space
1 parent 6736da7 commit 0050a18

File tree

7 files changed

+187
-0
lines changed

7 files changed

+187
-0
lines changed

number.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,8 @@ package vector
33
type Number interface {
44
int8 | int16 | int | int32 | int64 | float32 | float64
55
}
6+
7+
type Vector interface {
8+
Length() float64
9+
LengthSquared() float64
10+
}

space.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package vector
2+
3+
type Space[T any] interface {
4+
Distance(a, b T) float64
5+
Add(a, b T) T
6+
Sub(a, b T) T
7+
Scale(a T, amount float64) T
8+
Dot(a, b T) float64
9+
Length(a T) float64
10+
Normalized(a T) T
11+
Lerp(a, b T, time float64) T
12+
}

vector1/space.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package vector1
2+
3+
import (
4+
"math"
5+
6+
"github.com/EliCDavis/vector"
7+
)
8+
9+
type Space[T vector.Number] struct{}
10+
11+
func (Space[T]) Distance(a, b T) float64 {
12+
return math.Abs(float64(b - a))
13+
}
14+
15+
func (Space[T]) Add(a, b T) T {
16+
return a + b
17+
}
18+
19+
func (Space[T]) Sub(a, b T) T {
20+
return a - b
21+
}
22+
23+
func (Space[T]) Scale(a T, amount float64) T {
24+
return T(float64(a) * amount)
25+
}
26+
27+
func (Space[T]) Dot(a, b T) float64 {
28+
return float64(a * b)
29+
}
30+
31+
func (Space[T]) Length(a T) float64 {
32+
return math.Abs(float64(a))
33+
}
34+
35+
func (Space[T]) Normalized(a T) T {
36+
if a < 0 {
37+
return -1
38+
}
39+
if a == 0 {
40+
return 0
41+
}
42+
return 1
43+
}
44+
45+
func (Space[T]) Lerp(a, b T, time float64) T {
46+
return T(float64(a+(b-a)) * time)
47+
}

vector2/space.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package vector2
2+
3+
import "github.com/EliCDavis/vector"
4+
5+
type Space[T vector.Number] struct{}
6+
7+
func (Space[T]) Distance(a, b Vector[T]) float64 {
8+
return a.Distance(b)
9+
}
10+
11+
func (Space[T]) Add(a, b Vector[T]) Vector[T] {
12+
return a.Add(b)
13+
}
14+
15+
func (Space[T]) Sub(a, b Vector[T]) Vector[T] {
16+
return a.Sub(b)
17+
}
18+
19+
func (Space[T]) Scale(a Vector[T], amount float64) Vector[T] {
20+
return a.Scale(amount)
21+
}
22+
23+
func (Space[T]) Dot(a, b Vector[T]) float64 {
24+
return a.Dot(b)
25+
}
26+
27+
func (Space[T]) Length(a Vector[T]) float64 {
28+
return a.Length()
29+
}
30+
31+
func (Space[T]) Normalized(a Vector[T]) Vector[T] {
32+
return a.Normalized()
33+
}
34+
35+
func (Space[T]) Lerp(a, b Vector[T], time float64) Vector[T] {
36+
return Lerp(a, b, time)
37+
}

vector3/space.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package vector3
2+
3+
import "github.com/EliCDavis/vector"
4+
5+
type Space[T vector.Number] struct{}
6+
7+
func (Space[T]) Distance(a, b Vector[T]) float64 {
8+
return a.Distance(b)
9+
}
10+
11+
func (Space[T]) Add(a, b Vector[T]) Vector[T] {
12+
return a.Add(b)
13+
}
14+
15+
func (Space[T]) Sub(a, b Vector[T]) Vector[T] {
16+
return a.Sub(b)
17+
}
18+
19+
func (Space[T]) Scale(a Vector[T], amount float64) Vector[T] {
20+
return a.Scale(amount)
21+
}
22+
23+
func (Space[T]) Dot(a, b Vector[T]) float64 {
24+
return a.Dot(b)
25+
}
26+
27+
func (Space[T]) Length(a Vector[T]) float64 {
28+
return a.Length()
29+
}
30+
31+
func (Space[T]) Normalized(a Vector[T]) Vector[T] {
32+
return a.Normalized()
33+
}
34+
35+
func (Space[T]) Lerp(a, b Vector[T], time float64) Vector[T] {
36+
return Lerp(a, b, time)
37+
}

vector4/space.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package vector4
2+
3+
import "github.com/EliCDavis/vector"
4+
5+
type Space[T vector.Number] struct{}
6+
7+
func (Space[T]) Distance(a, b Vector[T]) float64 {
8+
return a.Distance(b)
9+
}
10+
11+
func (Space[T]) Add(a, b Vector[T]) Vector[T] {
12+
return a.Add(b)
13+
}
14+
15+
func (Space[T]) Sub(a, b Vector[T]) Vector[T] {
16+
return a.Sub(b)
17+
}
18+
19+
func (Space[T]) Scale(a Vector[T], amount float64) Vector[T] {
20+
return a.Scale(amount)
21+
}
22+
23+
func (Space[T]) Dot(a, b Vector[T]) float64 {
24+
return a.Dot(b)
25+
}
26+
27+
func (Space[T]) Length(a Vector[T]) float64 {
28+
return a.Length()
29+
}
30+
31+
func (Space[T]) Normalized(a Vector[T]) Vector[T] {
32+
return a.Normalized()
33+
}
34+
35+
func (Space[T]) Lerp(a, b Vector[T], time float64) Vector[T] {
36+
return Lerp(a, b, time)
37+
}

vector4/vector4.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -714,3 +714,15 @@ func (v Vector[T]) Component(index int) T {
714714
panic(fmt.Errorf("invalid index: %d", index))
715715
}
716716
}
717+
718+
func (v Vector[T]) DistanceSquared(other Vector[T]) float64 {
719+
xDist := other.x - v.x
720+
yDist := other.y - v.y
721+
zDist := other.z - v.z
722+
wDist := other.w - v.w
723+
return float64((xDist * xDist) + (yDist * yDist) + (zDist * zDist) + (wDist * wDist))
724+
}
725+
726+
func (v Vector[T]) Distance(other Vector[T]) float64 {
727+
return math.Sqrt(v.DistanceSquared(other))
728+
}

0 commit comments

Comments
 (0)