faiface / pixel

A hand-crafted 2D game library in Go
MIT License
4.46k stars 245 forks source link

feat(Vec): add squared len method #313

Closed marc921 closed 1 year ago

marc921 commented 1 year ago

The Vec struct already has a Len method, but the squared root could be a performance hit. When we just want to compare the length of two Vec, it is better to use the squared length. I suggest adding this in vector.go, under the Len method:

// SqLen returns the squared length of the vector u (faster to compute than Len).
func (u Vec) SqLen() float64 {
    return u.X*u.X + u.Y*u.Y
}

I love your work on this repo BTW, it's great and simple to use!