servo / euclid

Geometry primitives (basic linear algebra) for Rust
Other
462 stars 102 forks source link

Add `Vector2D::try_normalize` and `Vector3D::try_normalize`. #431

Closed kyren closed 4 years ago

kyren commented 4 years ago

These methods check that the length of the vector is not zero and return None in that case. It is slightly cleaner than either checking that the vector is not equal to zero or checking that the length is not zero before calling the regular normalize method.

If the vector has very small component values, length may be calculated as zero, and thus the length of the normalized vector will be infinity.

Another possibility here is to copy nalgebra and have a min_length parameter, rather than assuming that minimum length is always zero.

nical commented 4 years ago

Looks good to me, thanks!

@bors-servo r+

bors-servo commented 4 years ago

:pushpin: Commit b60359a has been approved by nical

bors-servo commented 4 years ago

:hourglass: Testing commit b60359a3162e54a567dbe665ec0cc8b07aa7a118 with merge b37c5048a7a6b15cf1a4d40923a820a3ec328b3e...

bors-servo commented 4 years ago

:sunny: Test successful - checks-travis Approved by: nical Pushing b37c5048a7a6b15cf1a4d40923a820a3ec328b3e to master...