Forráskód Böngészése

Vector: correctly normalize zero vector

SG 1 éve
szülő
commit
209bb5af5e
1 módosított fájl, 4 hozzáadás és 0 törlés
  1. 4 0
      vector.c

+ 4 - 0
vector.c

@@ -1,5 +1,6 @@
 #include "vector.h"
 #include <math.h>
+#include <float.h>
 
 #undef vector_add
 #undef vector_sub
@@ -44,6 +45,9 @@ float vector_length(Vector v) {
 
 Vector vector_normalize(Vector v) {
     float length = vector_length(v);
+    if(length < FLT_EPSILON) {
+        return (Vector){0, 0};
+    }
     return (Vector){v.x / length, v.y / length};
 }