Added optional arg squared to lume.distance()

This commit is contained in:
rxi 2014-03-04 22:53:31 +00:00
parent be71dbda78
commit f144927542
2 changed files with 7 additions and 4 deletions

View File

@ -44,8 +44,10 @@ interpolation.
### lume.pingpong(x) ### lume.pingpong(x)
Ping-pongs the number `x` between 0 and 1. Ping-pongs the number `x` between 0 and 1.
### lume.distance(x1, y1, x2, y2) ### lume.distance(x1, y1, x2, y2 [, squared])
Returns the distance between the two points. 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) ### lume.angle(x1, y1, x2, y2)
Returns the angle between the two points. Returns the angle between the two points.

View File

@ -42,8 +42,9 @@ function lume.pingpong(x)
end end
function lume.distance(x1, y1, x2, y2) function lume.distance(x1, y1, x2, y2, squared)
return math.sqrt((x1 - x2) ^ 2 + (y1 - y2) ^ 2) local s = (x1 - x2) ^ 2 + (y1 - y2) ^ 2
return squared and s or math.sqrt(s)
end end