add a check to Vec3#unit to avoid unnecessary sqrt and division

This commit is contained in:
jbb01 2024-08-08 21:58:51 +02:00
parent 9e79333e1e
commit 6599c41b14

View File

@ -270,7 +270,9 @@ public record Vec3(double x, double y, double z) {
* {@return a unit vector with the same direction as this vector}
*/
public @NotNull Vec3 unit() {
return div(length());
var squared = squared();
if (squared == 1) return this;
return div(Math.sqrt(squared));
}
/**