From 1c683f50a4622a60870ebd787e6339699a1225ba Mon Sep 17 00:00:00 2001 From: a_m0d Date: Wed, 18 Apr 2012 22:57:47 -0400 Subject: [PATCH] Fix divide-by-zero error in vector:normalize() and vector:normalize_in_place() --- vector.lua | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/vector.lua b/vector.lua index 8bde160..5ad2850 100644 --- a/vector.lua +++ b/vector.lua @@ -116,12 +116,14 @@ end function vector:normalize_inplace() local l = self:len() - self.x, self.y = self.x / l, self.y / l + if l > 0 then self.x, self.y = self.x / l, self.y / l end return self end function vector:normalized() - return self / self:len() + local l = self:len() + if l == 0 then return self end + return self / l end function vector:rotate_inplace(phi)