Skip to content

Commit

Permalink
Merge pull request #9 from joshlarsen/add-2d-cross-product
Browse files Browse the repository at this point in the history
add 2D cross product
  • Loading branch information
deeean authored Dec 10, 2024
2 parents 44b5366 + 8ff5f3e commit d72c947
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ func (v *Vector2) DivScalars(x float64, y float64) *Vector2

func (v *Vector2) Distance(other *Vector2) float64

func (v *Vector2) Cross(other *Vector2) float64

func (v *Vector2) Dot(other *Vector2) float64

func (v *Vector2) Lerp(other *Vector2, t float64) *Vector2
Expand Down
4 changes: 4 additions & 0 deletions vector2/vector2.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ func (v *Vector2) Distance(other *Vector2) float64 {
return math.Sqrt(dx*dx + dy*dy)
}

func (v *Vector2) Cross(other *Vector2) float64 {
return v.X*other.Y - v.Y*other.X
}

func (v *Vector2) Dot(other *Vector2) float64 {
return v.X*other.X + v.Y*other.Y
}
Expand Down
11 changes: 10 additions & 1 deletion vector2/vector2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ package vector2

import (
"fmt"
"github.com/stretchr/testify/assert"
"testing"

"github.com/stretchr/testify/assert"
)

func TestNew(t *testing.T) {
Expand Down Expand Up @@ -139,6 +140,14 @@ func TestVector2_Distance(t *testing.T) {
assert.Equal(t, res, 3.1622776601683795)
}

func TestVector2_Cross(t *testing.T) {
a := New(1, 2)
b := New(4, 3)
res := a.Cross(b)

assert.Equal(t, res, -5.0)
}

func TestVector2_Dot(t *testing.T) {
a := New(1, 2)
b := New(4, 3)
Expand Down

0 comments on commit d72c947

Please sign in to comment.