sub and mag
This commit is contained in:
parent
2749cf8461
commit
62635d35e4
29
govector_test.go
Normal file
29
govector_test.go
Normal file
@ -0,0 +1,29 @@
|
||||
package govector
|
||||
|
||||
import (
|
||||
// "fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestSub(t *testing.T) {
|
||||
p1 := point2d{10, 10}
|
||||
p2 := point2d{5, 5}
|
||||
|
||||
v := p1.sub(p2)
|
||||
|
||||
if v != (vector2d{5, 5}) {
|
||||
t.Errorf("Sub Error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestMag(t *testing.T) {
|
||||
p1 := point2d{10, 10}
|
||||
p2 := point2d{7, 6}
|
||||
|
||||
v := p1.sub(p2)
|
||||
m := v.mag()
|
||||
|
||||
if m != 5 {
|
||||
t.Errorf("Mag Error")
|
||||
}
|
||||
}
|
26
vectorpoint.go
Normal file
26
vectorpoint.go
Normal file
@ -0,0 +1,26 @@
|
||||
package govector
|
||||
|
||||
import (
|
||||
"math"
|
||||
)
|
||||
|
||||
type vector2d struct {
|
||||
X float64 `json:"x"`
|
||||
Y float64 `json:"y"`
|
||||
}
|
||||
|
||||
type point2d struct {
|
||||
X float64 `json:"x"`
|
||||
Y float64 `json:"y"`
|
||||
}
|
||||
|
||||
const epsilon = 1e-7
|
||||
|
||||
func (p1 point2d) sub(p2 point2d) vector2d {
|
||||
v := vector2d{p1.X - p2.X, p1.Y - p2.Y}
|
||||
return v
|
||||
}
|
||||
|
||||
func (v vector2d) mag() float64 {
|
||||
return math.Abs(math.Sqrt(v.X*v.X + v.Y*v.Y))
|
||||
}
|
Loading…
Reference in New Issue
Block a user