mirror of
https://github.com/HandmadeMath/HandmadeMath.git
synced 2026-01-02 17:52:27 +00:00
Handle zero-vector normalization. (#63)
* Handle zero-vector normalization. When normalizing a vectors, we have to check whether vector length is not zero, to avoid dividing by zero when normalizing zero-vectors. * Test for normalization of zero vectors
This commit is contained in:
committed by
Ben Visness
parent
09524f72ed
commit
98f535aeec
@@ -923,8 +923,12 @@ HMM_NormalizeVec2(hmm_vec2 A)
|
||||
|
||||
float VectorLength = HMM_LengthVec2(A);
|
||||
|
||||
Result.X = A.X * (1.0f / VectorLength);
|
||||
Result.Y = A.Y * (1.0f / VectorLength);
|
||||
/* NOTE(kiljacken): We need a zero check to not divide-by-zero */
|
||||
if (VectorLength != 0.0f)
|
||||
{
|
||||
Result.X = A.X * (1.0f / VectorLength);
|
||||
Result.Y = A.Y * (1.0f / VectorLength);
|
||||
}
|
||||
|
||||
return (Result);
|
||||
}
|
||||
@@ -936,9 +940,13 @@ HMM_NormalizeVec3(hmm_vec3 A)
|
||||
|
||||
float VectorLength = HMM_LengthVec3(A);
|
||||
|
||||
Result.X = A.X * (1.0f / VectorLength);
|
||||
Result.Y = A.Y * (1.0f / VectorLength);
|
||||
Result.Z = A.Z * (1.0f / VectorLength);
|
||||
/* NOTE(kiljacken): We need a zero check to not divide-by-zero */
|
||||
if (VectorLength != 0.0f)
|
||||
{
|
||||
Result.X = A.X * (1.0f / VectorLength);
|
||||
Result.Y = A.Y * (1.0f / VectorLength);
|
||||
Result.Z = A.Z * (1.0f / VectorLength);
|
||||
}
|
||||
|
||||
return (Result);
|
||||
}
|
||||
@@ -950,10 +958,14 @@ HMM_NormalizeVec4(hmm_vec4 A)
|
||||
|
||||
float VectorLength = HMM_LengthVec4(A);
|
||||
|
||||
Result.X = A.X * (1.0f / VectorLength);
|
||||
Result.Y = A.Y * (1.0f / VectorLength);
|
||||
Result.Z = A.Z * (1.0f / VectorLength);
|
||||
Result.W = A.W * (1.0f / VectorLength);
|
||||
/* NOTE(kiljacken): We need a zero check to not divide-by-zero */
|
||||
if (VectorLength != 0.0f)
|
||||
{
|
||||
Result.X = A.X * (1.0f / VectorLength);
|
||||
Result.Y = A.Y * (1.0f / VectorLength);
|
||||
Result.Z = A.Z * (1.0f / VectorLength);
|
||||
Result.W = A.W * (1.0f / VectorLength);
|
||||
}
|
||||
|
||||
return (Result);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user