Code: Select all
function getDistance(x1, y1, x2, y2)
-- this is real distance in pixels or some other unit
-- receives two coordinate pairs (not vectors)
-- returns a single number without any rounding
-- Euclidean distance
if (x1 == nil) or (y1 == nil) or (x2 == nil) or (y2 == nil) then return 0 end
local horizontal_distance = x1 - x2
local vertical_distance = y1 - y2
-- both of these next two lines work the same
local a = horizontal_distance * horizontal_distance
local b = vertical_distance ^2
local c = a + b
local distance = math.sqrt(c)
return distance
end
I think I'm getting stuck on receiving an unknown number of parameters, understanding how many were actually received, and then calculating the distance between those unknown pairs. Just to be clear - I understand how to calculate the distance between 3, 4 or 5 pairs - I just don't know how many pairs there will be!
___________________________
AB=√(x2−x1)^2+(y2−y1)^2+(z2−z1)^2
So, restating the problem - how to adjust the function so it can receive n co-ordinates in space that has n axis?