From 6599c41b14022a40d0aaafa2f87236cf06735aec Mon Sep 17 00:00:00 2001 From: jbb01 <32650546+jbb01@users.noreply.github.com> Date: Thu, 8 Aug 2024 21:58:51 +0200 Subject: [PATCH] add a check to Vec3#unit to avoid unnecessary sqrt and division --- src/main/java/eu/jonahbauer/raytracing/math/Vec3.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/java/eu/jonahbauer/raytracing/math/Vec3.java b/src/main/java/eu/jonahbauer/raytracing/math/Vec3.java index 7641756..7213c96 100644 --- a/src/main/java/eu/jonahbauer/raytracing/math/Vec3.java +++ b/src/main/java/eu/jonahbauer/raytracing/math/Vec3.java @@ -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)); } /**