From f1449275425c1e80189d954cc628e3987ae96d38 Mon Sep 17 00:00:00 2001 From: rxi Date: Tue, 4 Mar 2014 22:53:31 +0000 Subject: [PATCH] Added optional arg `squared` to lume.distance() --- README.md | 6 ++++-- lume.lua | 5 +++-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index ca31e78..a54cdb8 100644 --- a/README.md +++ b/README.md @@ -44,8 +44,10 @@ interpolation. ### lume.pingpong(x) Ping-pongs the number `x` between 0 and 1. -### lume.distance(x1, y1, x2, y2) -Returns the distance between the two points. +### lume.distance(x1, y1, x2, y2 [, squared]) +Returns the distance between the two points. If `squared` is true then the +squared distance is returned -- this is faster to calculate and can still be +used when comparing distances. ### lume.angle(x1, y1, x2, y2) Returns the angle between the two points. diff --git a/lume.lua b/lume.lua index c9512ec..aeca0c6 100644 --- a/lume.lua +++ b/lume.lua @@ -42,8 +42,9 @@ function lume.pingpong(x) end -function lume.distance(x1, y1, x2, y2) - return math.sqrt((x1 - x2) ^ 2 + (y1 - y2) ^ 2) +function lume.distance(x1, y1, x2, y2, squared) + local s = (x1 - x2) ^ 2 + (y1 - y2) ^ 2 + return squared and s or math.sqrt(s) end