Correct normalization of 0 vector
This commit is contained in:
parent
a1c37b9235
commit
68842de56e
@ -58,4 +58,12 @@ func TestNormalize(t *testing.T) {
|
||||
if vn.Mag() != 1 {
|
||||
t.Errorf("Normalize Error")
|
||||
}
|
||||
|
||||
v0 := Vector2d{0, 0}
|
||||
vn0 := v0.Normalize()
|
||||
|
||||
if vn0.Mag() != 0 {
|
||||
t.Errorf("Normalize Error")
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -44,6 +44,10 @@ func (v Vector2d) Scale(s float64) Vector2d {
|
||||
}
|
||||
|
||||
func (v Vector2d) Normalize() Vector2d {
|
||||
if v.X == 0 && v.Y == 0 {
|
||||
return v
|
||||
}
|
||||
|
||||
m := v.Mag()
|
||||
return Vector2d{v.X / m, v.Y / m}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user