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