Use .lengthSqr() for compare operations (#4827)

- Optimisations to some of Create's vector math shortcuts
This commit is contained in:
Timo van Veen 2023-07-03 15:38:16 +02:00 committed by GitHub
parent 440d7e0e39
commit 2bcc12b096
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -187,7 +187,7 @@ public class VecHelper {
}
public static Vec3 clamp(Vec3 vec, float maxLength) {
return vec.length() > maxLength ? vec.normalize()
return vec.lengthSqr() > maxLength * maxLength ? vec.normalize()
.scale(maxLength) : vec;
}
@ -226,7 +226,7 @@ public class VecHelper {
public static Vec3 intersectSphere(Vec3 origin, Vec3 lineDirection, Vec3 sphereCenter, double radius) {
if (lineDirection.equals(Vec3.ZERO))
return null;
if (lineDirection.length() != 1)
if (lineDirection.lengthSqr() != 1)
lineDirection = lineDirection.normalize();
Vec3 diff = origin.subtract(sphereCenter);
@ -319,7 +319,7 @@ public class VecHelper {
return null;
if (intersect[0] < 0 || intersect[1] < 0)
return null;
if (intersect[0] > pDiff.length() || intersect[1] > qDiff.length())
if (intersect[0] * intersect[0] > pDiff.lengthSqr() || intersect[1] * intersect[1] > qDiff.lengthSqr())
return null;
return intersect;
}