From 4afcac5b36009005bebf4ac1ff27b76649bf3939 Mon Sep 17 00:00:00 2001 From: out-of-pixel Date: Sat, 29 Jun 2013 17:39:03 +0200 Subject: [PATCH] add function angle() returns the angle between a vector and the default rotation --- vector.lua | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/vector.lua b/vector.lua index 9b028f5..3c9ed6b 100644 --- a/vector.lua +++ b/vector.lua @@ -135,6 +135,16 @@ function vector:rotated(phi) return new(c * self.x - s * self.y, s * self.x + c * self.y) end +function vector:angle() + local a = new(1,0) + local angle = math.acos((a*self)/(a:len()*self:len())) + if self.y < 0 then + return -angle + else + return angle + end +end + function vector:perpendicular() return new(-self.y, self.x) end